|
1 | | -import traceback |
2 | | - |
3 | 1 | import config |
| 2 | +import logging |
4 | 3 | import sys |
5 | 4 | from handler import HandlerManager |
6 | 5 | from comment import format_comment |
|
9 | 8 |
|
10 | 9 | def scan(subreddit): |
11 | 10 | """Scan a Subreddit for new submissions.""" |
12 | | - print("Starting scan") |
| 11 | + logging.info("Starting scan") |
13 | 12 |
|
14 | 13 | for submission in subreddit.stream.submissions(skip_existing=True): |
15 | | - print("Operating on submission ID: " + submission.id) |
| 14 | + logging.info("Operating on submission") |
| 15 | + logging.debug("Submission ID = {}, title = {}".format( |
| 16 | + submission.id, |
| 17 | + submission.title |
| 18 | + )) |
16 | 19 |
|
17 | 20 | does_qualify = qualify(submission) |
18 | 21 |
|
19 | | - if does_qualify: |
20 | | - print("Submission qualifies") |
| 22 | + if not does_qualify: |
| 23 | + logging.info("Comment does not qualify; skipping comment") |
| 24 | + continue |
| 25 | + |
| 26 | + logging.info("Submission qualifies") |
21 | 27 |
|
| 28 | + handler = None |
| 29 | + try: |
| 30 | + logging.info("Attempting to get article handler") |
22 | 31 | handler = HandlerManager.get_handler(submission.url) |
| 32 | + logging.info("Article handler = {}".format(handler)) |
| 33 | + except Exception as e: |
| 34 | + logging.error(""" |
| 35 | + An error occurred while getting article handler. This should never happen. |
| 36 | + """) |
| 37 | + logging.error("Exception = {}".format(e)) |
| 38 | + logging.info("Skipping current submission") |
| 39 | + continue |
| 40 | + |
| 41 | + comment_raw = None |
| 42 | + try: |
| 43 | + logging.info("Attempting to generate raw comment using handler") |
| 44 | + comment_raw = handler.handle(submission.url) |
| 45 | + logging.info("Raw comment generated") |
| 46 | + except Exception as e: |
| 47 | + logging.error("An error occurred while handling URL = {}".format( |
| 48 | + submission.url |
| 49 | + )) |
| 50 | + logging.error("Exception = {}".format(e)) |
| 51 | + logging.info("Skipping current submission") |
| 52 | + continue |
| 53 | + |
| 54 | + if comment_raw is None: |
| 55 | + logging.error("comment_raw is None; skipping current submission") |
| 56 | + continue |
23 | 57 |
|
24 | | - comment_raw = None |
| 58 | + logging.info("Generating formatted comment") |
| 59 | + comment_markdown = format_comment(comment_raw) |
| 60 | + logging.info("Formatted comment generated") |
| 61 | + |
| 62 | + if len(comment_markdown) < config.COMMENT_LENGTH_LIMIT: |
25 | 63 | try: |
26 | | - comment_raw = handler.handle(submission.url) |
| 64 | + logging.info("Attempting to post comment") |
| 65 | + submission.reply(comment_markdown) |
| 66 | + logging.info("Comment posting succeeded") |
27 | 67 | except Exception as e: |
28 | | - print(f"Exception occurred while handling {submission.url}") |
29 | | - traceback.print_exc() |
30 | | - |
31 | | - if comment_raw is None: |
32 | | - skip(submission) |
33 | | - return |
34 | | - comment_markdown = format_comment(comment_raw) |
35 | | - |
36 | | - if len(comment_markdown) < config.COMMENT_LENGTH_LIMIT: |
37 | | - try: |
38 | | - print("Attempting to post a comment") |
39 | | - submission.reply(comment_markdown) |
40 | | - print("Comment posting succeeded") |
41 | | - except Exception as e: |
42 | | - print("An error occurred:") |
43 | | - print(e) |
44 | | - else: |
45 | | - print("Submission is too long to be posted.") |
| 68 | + logging.error("An error occurred while posting comment") |
| 69 | + logging.error("Exception = {}".format(e)) |
46 | 70 | else: |
47 | | - skip(submission) |
48 | | - |
49 | | - # Flush stdout buffer |
50 | | - sys.stdout.flush() |
51 | | - |
52 | | -def skip(submission): |
53 | | - # If submission does not qualify, write SKIP to database only if it is new. |
54 | | - print("Submission does not qualify") |
| 71 | + logging.warning("Submission is too long to be posted") |
0 commit comments