示例#1
0
文件: scan.py 项目: yleong/sneakpeek
def scan(subreddit):
    """Scan a Subreddit for new submissions."""
    print("Starting scan")

    for submission in subreddit.new(limit=config.LIMIT):
        print("Operating on submission ID: " + submission.id)

        does_qualify = qualify(submission)

        if does_qualify:
            print("Submission qualifies")

            handler = HandlerManager.get_handler(submission.url)

            comment_raw = None
            try:
                comment_raw = handler.handle(submission.url)
            except Exception as e:
                print(f"Exception occurred while handling {submission.url}")
                traceback.print_exc()

            if comment_raw is None:
                skip(submission)
                return
            comment_markdown = format_comment(comment_raw)

            if len(comment_markdown) < config.COMMENT_LENGTH_LIMIT:
                try:
                    print("Attempting to post a comment")
                    submission.reply(comment_markdown)
                    print("Comment posting succeeded")
                    print("Attempting to write success to database")
                    DatabaseManager.write_id(submission.id, DatabaseActionEnum.SUCCESS)
                    print("Database write succeeded")
                except Exception as e:
                    print("An error occurred:")
                    print(e)
            else:
                print("Submission is too long to be posted.")
                print("Attempting to write skip to database")
                DatabaseManager.write_id(submission.id, DatabaseActionEnum.SKIP)
                print("Database write succeeded")
        else:
            skip(submission)
示例#2
0
def scan(subreddit):
    """Scan a Subreddit for new submissions."""
    logging.info("Starting scan")

    for submission in subreddit.stream.submissions(skip_existing=True):
        logging.info("Operating on submission")
        logging.debug("Submission ID = %s, title = %s",
                      submission.id,
                      submission.title)

        does_qualify = qualify(submission)

        if not does_qualify:
            logging.info("Comment does not qualify; skipping comment")
            continue

        logging.info("Submission qualifies")

        handler = None
        try:
            logging.info("Attempting to get article handler")
            handler = HandlerManager.get_handler(submission.url)
            logging.info("Article handler = %s",
                         handler)
        except Exception as exception:
            logging.error("""
                            An error occurred while getting article handler. This should never happen.
                            """)
            logging.error("Exception = %s", exception)
            logging.info("Skipping current submission")
            continue

        comment_raw = None
        try:
            logging.info("Attempting to generate raw comment using handler")
            comment_raw = handler.handle(submission.url)
            logging.info("Raw comment generated")
        except Exception as exception:
            logging.error("An error occurred while handling URL = %s",
                          submission.url)
            logging.error("Exception = %s", exception)
            logging.info("Skipping current submission")
            continue

        if comment_raw is None:
            logging.error("comment_raw is None; skipping current submission")
            continue

        logging.info("Generating formatted comment")
        comment_markdown = format_comment(comment_raw)
        logging.info("Formatted comment generated")

        if len(comment_markdown) < config.COMMENT_LENGTH_LIMIT:
            try:
                logging.info("Attempting to post comment")
                submission.reply(comment_markdown)
                logging.info("Comment posting succeeded")
            except Exception as exception:
                logging.error("An error occurred while posting comment")
                logging.error("Exception = %s", exception)
        else:
            logging.warning("Submission is too long to be posted")