Skip to content

Commit

Permalink
Linter maybe?
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberAustin committed Mar 9, 2024
1 parent 9248944 commit 2197058
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
65 changes: 38 additions & 27 deletions AFILinkerBot/AFILinkerBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

print_and_log("Starting script")

#funtion to check for comments that may have already been replied to
# funtion to check for comments that may have already been replied to


def checkForReplies(comment_list, rAirForceComments):
for comment in comment_list:
if rAirForceComments.id in comment.body:
print_and_log("Already processed comment: " + permlink + ", skipping")
print_and_log("Already processed comment: " +
permlink + ", skipping")
return True
return False

#Try to login or sleep/wait until logged in, or exit if user/pass wrong

# Try to login or sleep/wait until logged in, or exit if user/pass wrong
NotLoggedIn = True
while NotLoggedIn:
try:
Expand Down Expand Up @@ -75,8 +79,8 @@ def checkForReplies(comment_list, rAirForceComments):

print_and_log("Starting processing loop for subreddit: " + subreddit)

#System to keep track of how many 404 errors we get from ePubs, for data research purposes
#All the .close() statements
# System to keep track of how many 404 errors we get from ePubs, for data research purposes
# All the .close() statements
ePubs404Error = 0
log404("Start")

Expand All @@ -85,7 +89,8 @@ def checkForReplies(comment_list, rAirForceComments):
# stream all comments from /r/AirForce
for rAirForceComments in rAirForce.stream.comments():
globalCount += 1
print_and_log("\nComments processed since start of script: " + str(globalCount))
print_and_log(
"\nComments processed since start of script: " + str(globalCount))
print_and_log("Processing comment: " + rAirForceComments.id)

# prints a link to the comment.
Expand All @@ -111,7 +116,8 @@ def checkForReplies(comment_list, rAirForceComments):
if "afform" in formattedComment or "aftoform" in formattedComment:
formattedComment = formattedComment.replace("form", "")
else:
formattedComment = formattedComment.replace("form", "af")
formattedComment = formattedComment.replace(
"form", "af")
formattedComment = formattedComment.replace("vol", "v")
print("Formatted Comment: " + formattedComment)

Expand Down Expand Up @@ -139,26 +145,31 @@ def checkForReplies(comment_list, rAirForceComments):
with open("smarmycomments.txt", "r") as f:
dalist = f.read().splitlines()
print_and_log("Dropping a smarmy comment on the mention of: " + individualMention.group() +
" by " + str(rAirForceComments.author) + ". Comment ID: " + rAirForceComments.id)
smarmyReply = SmarmyReplyTemplate + (dalist[random.randint(0, len(dalist) - 1)])
" by " + str(rAirForceComments.author) + ". Comment ID: " + rAirForceComments.id)
smarmyReply = SmarmyReplyTemplate + \
(dalist[random.randint(0, len(dalist) - 1)])
smarmyReply += "**\n\nI am a bot, this was an automatic reply."
smarmyReply += " ^^^^^^" + rAirForceComments.id
rAirForceComments.reply(smarmyReply)
continue

#polls the epubs website for a search
# polls the epubs website for a search
session = requests.Session()
session.head("https://www.e-publishing.af.mil/")
reqParams = {"keyword": individualMention.group(), "obsolete": "false"}
reqParams = {
"keyword": individualMention.group(), "obsolete": "false"}

epubsReturn = session.get("http://www.e-publishing.af.mil/DesktopModules/MVC/EPUBS/EPUB/GetPubsSearchView/", params=reqParams)
epubsReturn = session.get(
"http://www.e-publishing.af.mil/DesktopModules/MVC/EPUBS/EPUB/GetPubsSearchView/", params=reqParams)

if epubsReturn.status_code == requests.codes.not_found:
log404(f"404 Error received, retrying in 10 seconds: {permlink}")
log404(
f"404 Error received, retrying in 10 seconds: {permlink}")
for i in range(5):
time.sleep(10)
ePubs404Error += 1
log404(f"Try #{str(i+1)} for {epubsReturn.url}, total failure this lifecycle: {ePubs404Error}")
log404(
f"Try #{str(i+1)} for {epubsReturn.url}, total failure this lifecycle: {ePubs404Error}")
epubsReturn = requests.get(
"http://www.e-publishing.af.mil/DesktopModules/MVC/EPUBS/EPUB/GetPubsSearchView/",
params=reqParams)
Expand All @@ -167,17 +178,18 @@ def checkForReplies(comment_list, rAirForceComments):
break
log404("Never worked")


# Scrub the epubsReturn and find all http or https links
listOfLinks = re.findall("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", epubsReturn.text)
listOfLinks = re.findall(
"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", epubsReturn.text)
listOfMatchedLinks = []

# regex to match exactly the afi typed in the comment, but with a ".pdf" at the end
individualMentionRegex = "(?<=/)(" + individualMention.group() + ")(?=\.pdf)"
individualMentionRegex = "(?<=/)(" + \
individualMention.group() + ")(?=\.pdf)"
regObject = re.compile(individualMentionRegex)

#iterate through the list of link to find exact matches
for i in range(0,len(listOfLinks)):
# iterate through the list of link to find exact matches
for i in range(0, len(listOfLinks)):
listItem = regObject.findall(str(listOfLinks[i]))
if listItem:
listOfMatchedLinks.append(listOfLinks[i])
Expand All @@ -188,14 +200,15 @@ def checkForReplies(comment_list, rAirForceComments):
print_and_log("Garbage return, skipping link")
continue
if link in formattedComment:
print(str(individualMention.group()).upper() + " link already posted by OP, skipping")
print(str(individualMention.group()).upper(
) + " link already posted by OP, skipping")
else:
# Add the matched comment into the master list
ListOfMatchedComments.append(individualMention.group())
ListOfMatchedComments.append(
individualMention.group())
print("Link: " + link)
TotalAFILinks += link + "\n\n"


# if the TotalAFILinks variable isn"t empty (no matches),
# prepare the reply comment
if TotalAFILinks != "":
Expand All @@ -207,15 +220,13 @@ def checkForReplies(comment_list, rAirForceComments):

# Comments on post
rAirForceComments.reply(replyComment)
print_and_log("Commented on mention of: " + str(ListOfMatchedComments) + " by " +
str(rAirForceComments.author) + ". Comment ID: " + rAirForceComments.id)

print_and_log("Commented on mention of: " + str(ListOfMatchedComments) + " by " +
str(rAirForceComments.author) + ". Comment ID: " + rAirForceComments.id)

# what to do if Ctrl-C is pressed while script is running
except KeyboardInterrupt:
print_and_log("Exiting due to keyboard interrupt")
exit(0)

except Exception as err:
print_and_log(str(err.with_traceback()), error=True)

13 changes: 10 additions & 3 deletions AFILinkerBot/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@

"""Some helping functions for the main script"""

LOG_TIME_FORMAT = "%Y/%m/%d %H:%M:%S "
LOG_TIME_FORMAT = "%Y/%m/%d %H:%M:%S: "

logger = logging.getLogger("AFILinkerBot Rotating Log")
logger.setLevel(logging.INFO)

# add a rotating handler
handler = RotatingFileHandler("AFILinkerBot.log", maxBytes=2048000, backupCount=25)
handler = RotatingFileHandler(
"AFILinkerBot.log", maxBytes=2048000, backupCount=25)
logger.addHandler(handler)

logger404 = logging.getLogger("AFILinkerBot 404 error Rotating Log")
logger404.setLevel(logging.INFO)

# add a rotating handler
handler404 = RotatingFileHandler("404errors.log", maxBytes=2048000, backupCount=25)
handler404 = RotatingFileHandler(
"404errors.log", maxBytes=2048000, backupCount=25)
logger404.addHandler(handler)

"""prints and logs at the same time"""


def print_and_log(text, error=False):
print(text)
if error:
logger.error(time.strftime(LOG_TIME_FORMAT) + text)
else:
logger.info(time.strftime(LOG_TIME_FORMAT) + text)


"""logs 404 errors that come from the epubs site"""


def log404(text):
print(text)
logger404.info(time.strftime(LOG_TIME_FORMAT) + text)

0 comments on commit 2197058

Please sign in to comment.