def search(self, query, site=None, limit=1): blocked = set() exceptions = [] while True: # Always try to get the engine with the lowest # wait time. engine = None for e in self._engine_order: if e not in blocked: engine = e break # If there is no engine, return None if engine is None: break # Perform the query. try: res = engine.search(query, site, limit) except Exception as e: bot_tools.print_exception(e, level=logging.WARN) else: if res: return res # Mark the engine as queried. blocked.add(engine) if len(exceptions) > 0: raise IOError(exceptions) return None
def stream_strategy(): post_queue = Queue() threads = {} multireddit = "+".join(SUBREDDIT_LIST) stream_types = ["comments", "submissions", "inbox"] def assign_thread(stream_type): threads[stream_type] = _make_thread(stream_type, multireddit, post_queue) threads[stream_type].daemon = True threads[stream_type].start() logging.info("(Stream Strategy) Began stream type: {0}".format(stream_type)) for stream_type in stream_types: assign_thread(stream_type) while True: try: post_receiver(post_queue) except Exception as e: logging.error("(Stream Strategy) Exception below.") bot_tools.print_exception(e) for stream_type in stream_types: if not threads[stream_type].isAlive(): logging.error('(Stream Strategy) Restarting failed thread: {0}'.format(stream_type)) assign_thread(stream_type)
def parse_submissions(SUBREDDIT): """Parses all user-submissions.""" print("==================================================") print("Parsing submissions on SUBREDDIT", SUBREDDIT) for submission in SUBREDDIT.get_hot(limit=50): # Also parse the submission text. if not is_submission_checked(submission): parse_submission_text(submission) check_submission(submission) logging.info("Checking SUBMISSION: ", submission.id) flat_comments = praw.helpers.flatten_tree(submission.comments) for comment in flat_comments: logging.info( 'Checking COMMENT: ' + comment.id + ' in submission ' + submission.id) if str(comment.id) in CHECKED_COMMENTS: logging.info("Comment " + comment.id + " already parsed!") else: print("Parsing comment ", comment.id, ' in submission ', submission.id) try: make_reply(comment.body, comment.id, comment.id, comment.reply) except Exception: logging.error("\n\nPARSING COMMENT: Error has occured!") bot_tools.print_exception() check_comment(comment.id) print("Parsing on SUBREDDIT ", SUBREDDIT, " complete.") print("==================================================")
def make_reply(body, id, reply_func, markers=None, additions=()): """Makes a reply for the given comment.""" try: reply = list(formulate_reply(body, markers, additions)) except StoryLimitExceeded: if not DRY_RUN: reply_func("You requested too many fics.\n" "\nWe allow a maximum of 30 stories") bot_tools.print_exception(level=logging.DEBUG) print("Too many fics...") return raw_reply = "".join(reply) if len(raw_reply) > 10: print( "Writing reply to", id, "(", len(raw_reply), "characters in", len(reply), "messages)") # Do not send the comment. if not DRY_RUN: for part in reply: reply_func(part + FOOTER) bot_tools.pause(1, 20) print('Continuing to parse submissions...') else: logging.info("No reply conditions met.")
def main(args): parser = construct_parser(False) result = parser.parse_known_args(args)[0] load_settings(result.settings) settings = get_settings() main_func = settings["bot"]["main"] *module, func = main_func.split(".") logging.debug("Fanfiction.Net Bot") logging.debug("Initializing Bot.") logging.info("Searching main-func: " + main_func) # Find module. module = ".".join(module) module = importlib.import_module(module) func = getattr(module, func) logging.debug("Starting bot...") logging.debug("=====================") try: sys.exit(func(args)) except Exception as e: print_exception(e) logging.critical("Error detected. Stopping bot.") sys.exit(255)
def load_checked_comments(): """Loads all comments that have been checked.""" global CHECKED_COMMENTS logging.info('Loading CHECKED_COMMENTS...') try: with open('CHECKED_COMMENTS.txt', 'r') as file: CHECKED_COMMENTS = {str(line.rstrip('\n')) for line in file} except IOError: bot_tools.print_exception() CHECKED_COMMENTS = set() print('Loaded CHECKED_COMMENTS.') logging.info(CHECKED_COMMENTS)
def _run_forever(): """Run-Forever""" while True: try: main() # Exit on sys.exit and keyboard interrupts. except KeyboardInterrupt: raise except SystemExit as e: return e.code except: bot_tools.print_exception() bot_tools.pause(0, 10)
def _run_forever(): """Run-Forever""" while True: try: main() except SystemExit as e: return e.code except KeyboardInterrupt: return 0 except: logging.error("MAIN: AN EXCEPTION HAS OCCURED!") bot_tools.print_exception() bot_tools.pause(1, 0)
def process(self, request, context): try: link = self.find_link(request, context) except (StopIteration, Exception): bot_tools.print_exception() return None if link is None: return None try: return self.generate_response(link, context) except Exception: bot_tools.print_exception() return None
def login_to_reddit(r, settings): global _authenticator if not _authenticator: _authenticator = Authenticator(r) try: _authenticator.authenticate(settings) except ValueError as e: logging.critical("Failed to login:"******"Failed to perform login.") print_exception(e) return False return True
def single_pass(): try: # We actually use a multireddit to acieve our goal # of watching multiple reddits. subreddit = r.get_subreddit("+".join(SUBREDDIT_LIST)) logging.info("Parsing new submissions.") for submission in subreddit.get_new(limit=50): handle_submission(submission) logging.info("Parsing new comments.") for comment in subreddit.get_comments(limit=100): handle_comment(comment) except Exception: bot_tools.print_exception() bot_tools.pause(1, 0)
def _run_forever(): """Run-Forever""" while True: try: main() # Exit on sys.exit and keyboard interrupts. except KeyboardInterrupt: raise except SystemExit as e: return e.code except: logging.error("MAIN: AN EXCEPTION HAS OCCURED!") bot_tools.print_exception() bot_tools.pause(1, 0) finally: if CHECKED_COMMENTS is not None: CHECKED_COMMENTS.save()
def _run_forever(argv): """Run-Forever""" while True: try: return main(argv) # Exit on sys.exit and keyboard interrupts. except KeyboardInterrupt as e: # Exit the program unclean. bot_tools.print_exception(e, level=logging.debug) save_things() os._exit(0) except SystemExit as e: return e.code except: logging.error("MAIN: AN EXCEPTION HAS OCCURED!") bot_tools.print_exception() bot_tools.pause(1, 0) finally: save_things()
def stream_strategy(): from queue import Queue from threading import Thread from praw.helpers import submission_stream, comment_stream post_queue = Queue() threads = [] threads.append(Thread(target=lambda: stream_handler( post_queue, comment_stream( r, "+".join(SUBREDDIT_LIST), limit=100, verbosity=0 ), handle_comment ))) threads.append(Thread(target=lambda: stream_handler( post_queue, submission_stream( r, "+".join(SUBREDDIT_LIST), limit=100, verbosity=0 ), handle_submission ))) for thread in threads: thread.daemon = True thread.start() while True: try: post_receiver(post_queue) except Exception as e: for thread in threads: if not thread.isAlive(): raise KeyboardInterrupt from e bot_tools.print_exception(e)