def test_quoted_afsc(self): comment = ">I heard you were a 1W0X1\n\nYou are mistaken, clearly I'm a 1W051" expected = [ "1W051 = Weather Journeyman [^wiki](https://www.reddit.com/r/AirForce/wiki/jobs/1w0x1)" ] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)
def test_normal_afsc(self): comment = "Hi I am a 1W051" expected = [ "1W051 = Weather Journeyman [^wiki](https://www.reddit.com/r/AirForce/wiki/jobs/1w0x1)" ] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)
def test_afsc_repeated(self): comment = "Here's a 1W051, there's a 1W051, everywhere's a 1W091" expected = [ "1W051 = Weather Journeyman [^wiki](https://www.reddit.com/r/AirForce/wiki/jobs/1w0x1)", "1W091 = Weather Superintendent [^wiki](https://www.reddit.com/r/AirForce/wiki/jobs/1w0x1)" ] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)
def test_caps(self): comment = "doesnt matter what caps I use with k1n2x1 or 1C8X2\n\n" \ "but it DOES matter what I use with W13BXY. 16f doesn't work." expected = [ "K1N2X1 = Instructor Signals Intelligence Analyst [^wiki](https://www.reddit.com/r/AirForce/wiki/jobs/1n2x1ac)", "1C8X2 = Airfield Systems", "W13BXY = Weapons Officer Air Battle Manager, General" ] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)
def main(): # Initialize a logging object #logging.basicConfig(filename='AFSCbot.log', level=logging.INFO) print_and_log("Starting script") # reddit user object reddit = login(credsUserName, credsPassword, credsClientSecret, credsClientID, credsUserAgent) # load all the AFSCs and prefixes into dictionaries try: full_afsc_dict = get_AFSCs(reddit) prefix_dict = get_prefixes() # subreddit instance of /r/AirForce. rAirForce = reddit.subreddit(subreddit) except Exception as e: print_and_log("Couldn't load dicts, {}".format(e), error=True) sys.exit(1) print_and_log("Starting processing loop for subreddit: " + subreddit) comments_seen = 0 try: while True: # stream all comments from /r/AirForce for rAirForceComment in rAirForce.stream.comments(): #If the post is older than about 5 months, ignore it and move on. if (time.time() - rAirForceComment.created) > 13148715: print_and_log("Post too old, continuing") continue # prints a link to the comment. A True for permalink # generates a fast find (but is not an accurate link, # just makes the script faster (SIGNIFICANTLY FASTER) permlink = "http://www.reddit.com{}".format( rAirForceComment.permalink) print_and_log("Processing comment: " + permlink) # Check replies to make sure the bot hasn't responded yet rAirForceComment.refresh() rAirForceComment.replies.replace_more() if checkForReplies(rAirForceComment.replies.list(), rAirForceComment, permlink): continue reply_text = generate_reply(rAirForceComment, full_afsc_dict, prefix_dict) # log that comment was prepared comment_info_text = ( "Preparing to reply to id {} by author: {}".format( rAirForceComment.id, rAirForceComment.author)) print_and_log(comment_info_text) if reply_text: send_reply(reply_text, rAirForceComment) else: print_and_log("No AFSC found, skipping...") comments_seen += 1 print_and_log("") print_and_log("Comments processed since start of script: {}".format( comments_seen)) # what to do if Ctrl-C is pressed while script is running except KeyboardInterrupt: print_and_log("Exiting due to keyboard interrupt")
def main(): # Initialize a logging object logging.basicConfig(filename='AFSCbot.log', level=logging.INFO) print_and_log("Starting script") # reddit user object reddit = login() # creates file tracking if script is currently running open_pid() # setup pid, database, and AFSC dicts conn, dbCommentRecord = setup_database() # load all the AFSCs and prefixes into dictionaries try: full_afsc_dict = get_AFSCs(reddit) prefix_dict = get_prefixes() # subreddit instance of /r/AirForce. rAirForce = reddit.subreddit(SUBREDDIT) except Exception as e: print_and_log("Couldn't load dicts, {}".format(e), error=True) close_pid() sys.exit(1) print_and_log("Starting processing loop for subreddit: " + SUBREDDIT) comments_seen = 0 try: while True: # stream all comments from /r/AirForce for rAirForceComment in rAirForce.stream.comments(): # prints a link to the comment. A True for permalink # generates a fast find (but is not an accurate link, # just makes the script faster (SIGNIFICANTLY FASTER) permlink = "http://www.reddit.com{}/".format( rAirForceComment.permalink(True)) print_and_log("Processing comment: " + permlink) # Pulls all comments previously commented on and checks # if the current comment has been replied to dbCommentRecord.execute( "SELECT * FROM comments WHERE comment=?", (rAirForceComment.id,)) id_already_exists = dbCommentRecord.fetchone() # Make sure we don't reply to the same comment twice # or to the bot itself if id_already_exists: print_and_log("Already replied to comment, skipping...") elif rAirForceComment.author in ("AFSCbot", "CSFAbot"): print_and_log("Author was the bot, skipping...") else: reply_text = generate_reply(rAirForceComment.body, full_afsc_dict, prefix_dict) # log that comment was prepared comment_info_text = ( "Preparing to reply to id {} by author: {}".format( rAirForceComment.id, rAirForceComment.author)) print_and_log(comment_info_text) if reply_text: send_reply(reply_text, rAirForceComment) # insert comment id into database so it wont be repeated dbCommentRecord.execute('INSERT INTO comments VALUES (?);', (rAirForceComment.id,)) conn.commit() else: print("No AFSC found, skipping...") comments_seen += 1 print() print("Comments processed since start of script: {}".format( comments_seen)) # what to do if Ctrl-C is pressed while script is running except KeyboardInterrupt: print("Exiting due to keyboard interrupt") finally: conn.commit() conn.close() close_pid()
def test_hyperlink(self): comment = "I just went to https://www.reddit.com/r/AirForce/wiki/jobs/1w0x1" expected = [] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)
def test_afsc_doesnt_exist(self): comment = "These are valid AFSCs but they don't exist 1C4X2 and 14V" expected = [] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)
def test_12s(self): comment = "I'm tired of working 12s as a K13SXB..." expected = ["K13SXB = Instructor Space Operations, Spacelift"] actual = generate_reply(comment, full_afsc_dict, prefix_dict) self.assertEqual(expected, actual)