def run(self): while True: if not self.check_status(): break # see if we need to update mods if datetime.datetime.now() - self.last_mod_update > self.policy.Mod_Update_Period: if __debug__: logging.info(u"Blacklist Query updating mod list...") self.update_mods() if __debug__: logging.info(u"Modlist updated to: {}".format(u", ".join(self.mod_list))) # get unread unread = Actions.get_unread(self.praw, limit=self.policy.Unread_To_Load) try: messages = [message for message in unread] if __debug__: logging.info(u"Blacklist query processing {} messages...".format(len(messages))) for message in messages: self.process_message(message) if __debug__: logging.info(u"Blacklist query processing message from user {}.\nSubject:{}\nBody:{}". format(message.author.name if message.author.name is not None else u"DELETED", message.subject, message.body)) except Exception, e: logging.error(u"Error on retrieving unread messages") if __debug__: logging.exception(e) self.log_error() self.message_cache = [] # and wait (min of 30s to prevent return of cached answers on default PRAW install) time.sleep(max(self.policy.Blacklist_Query_Period, 30))
def test_get_message(credentials): r = praw.Reddit(user_agent=credentials['USERAGENT']) r.login(username=credentials['ALTUSER'], password=credentials['ALTPASS']) messages = a.get_unread(r) message = messages.next() success = message.author.name == 'centralscruuutinizer' and message.body == "testmessage" and message.subject == "test" if success: print "Test Get Message: Passed" return True print "Test Get Message: Failed" return True
def test_blacklist_query(): try: os.remove("test_database.db") except: pass credentials = CRImport("TestCredentials.cred") credentials["SUBREDDIT"] = "centralscrutinizer" # swap usernames temp = credentials['USERNAME'] credentials['USERNAME'] = credentials['ALTUSER'] credentials['ALTUSER'] = temp temp = credentials['PASSWORD'] credentials['PASSWORD'] = credentials['ALTPASS'] credentials['ALTPASS'] = temp #get subs mypraw = u.create_multiprocess_praw(credentials) pol = Policies.DebugPolicy(None) #alt user my_handler = praw.handlers.MultiprocessHandler() r = praw.Reddit(user_agent="message-send-test", handler=my_handler) r.login(username=credentials['ALTUSER'], password=credentials['ALTPASS']) #mark all old as read for x in a.get_unread(mypraw, None): x.mark_as_read() for x in a.get_unread(r, None): x.mark_as_read() class author_dummy: def __init__(self): self.name = credentials['ALTUSER'] class message_dummy: def __init__(self, text, id): self.author = author_dummy() self.body = text self.id = id self.subject = "test" def mark_as_read(self): pass print "Starting Blacklist Query tests..." #h'ok here we go. #first we'll create three posts from a black/whitelisted channel and a not found with DataBase.DataBaseWrapper("test_database.db") as db: entries = [ ("arghdos", "youtube.com", "http://www.youtube.com/user/arghdos", Blacklist.BlacklistEnums.Whitelisted, 0), ("IGN", "youtube.com", "http://www.youtube.com/user/IGN", Blacklist.BlacklistEnums.Blacklisted, 0), ("Karen Jones", "youtube.com", "http://www.youtube.com/user/Karen Jones", Blacklist.BlacklistEnums.NotFound, 0)] db.add_channels(entries) #create scrutinizer cs = CentralScrutinizer.CentralScrutinizer(credentials, pol, "test_database.db") bq = cs.bquery print "Testing Add/Remove..." #test print functions id = 0 message = message_dummy("add\nwhitelist\nhttp://soundcloud.com/maggiesmithmusic/100-needles-for-zil", id) id += 1 result = bq.process_message(message) r_list = db.get_channels(blacklist=Blacklist.BlacklistEnums.Whitelisted, domain="soundcloud", id_filter="MaggieSmithMusic") if not len(r_list) or result != "": print "Failed" return False message = message_dummy("remove\nwhitelist\nMaggieSmithMusic\nsoundcloud", id) id += 1 result = bq.process_message(message) #result should be "" and "" should be listed in the message r_list = db.get_channels(blacklist=Blacklist.BlacklistEnums.Whitelisted, domain="soundcloud", id_filter="MaggieSmithMusic") if len(r_list) or result != "": print "Failed" return False message = message_dummy("add\nblacklist\nhttp://soundcloud.com/maggiesmithmusic/100-needles-for-zil", id) id += 1 result = bq.process_message(message) r_list = db.get_channels(blacklist=Blacklist.BlacklistEnums.Blacklisted, domain="soundcloud", id_filter="MaggieSmithMusic") if not len(r_list) or result != "": print "Failed" return False message = message_dummy("remove\nblacklist\nhttp://soundcloud.com/maggiesmithmusic/100-needles-for-zil", id) id += 1 result = bq.process_message(message) #result should be "" and "" should be listed in the message r_list = db.get_channels(blacklist=Blacklist.BlacklistEnums.Blacklisted, domain="soundcloud", id_filter="MaggieSmithMusic") if len(r_list) or result != "": print "Failed" return False message.mark_as_read() print "Add/Remove passed" #test help message print "Testing Help Message..." message = message_dummy("testmessage", id) id += 1 result = bq.process_message(message) #result should be "unknown", and we should get the help message message = [m for m in a.get_unread(r)][-1] if not message.body.startswith('Sorry, I did not recognize your query.') or result != "unknown": print "Failed" return False message.mark_as_read() sleep(30) message = message_dummy("help", id) id += 1 result = bq.process_message(message) #result should be "", and we should get the help message message = [m for m in a.get_unread(r)][-1] if message.body.startswith("Available Commands:") and result == "": print "Passed" else: print "Failed" return False message.mark_as_read() print "Testing print statement..." sleep(30) message = message_dummy("print\nwhitelist\nyoutube", id) id += 1 #test print functions result = bq.process_message(message) #result should be "", and we should get arghdos back message = [m for m in a.get_unread(r)][-1] if message.body != "Results:\narghdos" or result != "": print "Failed" return False message.mark_as_read() sleep(30) message = message_dummy("print\nwhitelist\nyoutube.com\n\"danny\"", id) id += 1 result = bq.process_message(message) #result should be "" and "" should be listed in the message message = [m for m in a.get_unread(r)][-1] if message.body != "Results:\n" or result != "": print "Failed" return False message.mark_as_read() sleep(30) message = message_dummy("print\nblacklist\nyoutube.com", id) id += 1 result = bq.process_message(message) #result should be "", and we should get arghdos back message = [m for m in a.get_unread(r)][-1] if message.body != "Results:\nIGN" or result != "": print "Failed" return False message.mark_as_read() sleep(30) message = message_dummy("print\nBlacklist\nyoutube\ndanny", id) id += 1 result = bq.process_message(message) #result should be "" and "" should be listed in the message message = [m for m in a.get_unread(r)][-1] if message.body != "Results:\n" or result != "": print "Failed" return False message.mark_as_read() print "Passed" print "Test update mods..." sleep(30) #finally update-mods message = message_dummy("update-mods", id) id += 1 result = bq.process_message(message) #result should be "" and "" should be listed in the message message = [m for m in a.get_unread(r)][-1] if message.body != "Mod update successful!" or result != "": print "Failed" return False message.mark_as_read() print "Passed" return True