Skip to content

Commit e13b522

Browse files
authored
Merge pull request #31 from fterh/release-v0.8.0-beta
Release v0.8.0 beta
2 parents bf5bb29 + 3eef5f2 commit e13b522

7 files changed

Lines changed: 105 additions & 41 deletions

File tree

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dist: xenial
2+
language: python
3+
python:
4+
- "3.7"
5+
install:
6+
- pip install pipenv
7+
script:
8+
- pipenv install
9+
- pipenv run invoke test

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# sneakpeek
2+
3+
[![Build Status](https://travis-ci.com/fterh/sneakpeek.svg?branch=master)](https://travis-ci.com/fterh/sneakpeek)
4+
25
A Reddit bot that previews hyperlinks and posts their contents as a comment.
36
It should **never spam or double-post**, and will skip a comment if it is
47
too long.
@@ -49,6 +52,9 @@ If the final comment in Markdown does not exceed a pre-configured comment length
4952
(`config.COMMENT_LENGTH_LIMIT`), the comment is posted, and the action written
5053
to the database (through `DatabaseManager`) to prevent double-posting.
5154

55+
Logging is written to standard output, and logging level can be configured in
56+
`config.py`.
57+
5258
### Handlers
5359
`handler.py` contains a HandlerManager that checks if a website has a Handler.
5460

@@ -78,6 +84,12 @@ the commands.
7884
`ENV=prod python main.py` or `ENV=prod nohup python main.py &`
7985

8086
## Changelog
87+
### v0.8.0-beta
88+
* Fix program stops running after a while (issue #30)
89+
* Implement proper logging
90+
* Clean up and refactor codebase
91+
* Travis CI
92+
8193
### v0.7.0-beta
8294
* Fix random crashes (issue #25)
8395
* Fix README formatting issues

config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import logging
12
import os
3+
import sys
24
from dotenv import load_dotenv
35

46

@@ -7,8 +9,14 @@
79
# Set to "prod" in production, but default to "dev"
810
ENV = os.getenv("ENV", "dev")
911

12+
# Logging configuration
13+
LOGGING = {
14+
"LEVEL": logging.DEBUG,
15+
"HANDLER": logging.StreamHandler(sys.stdout) # Log to stdout (see: https://12factor.net/logs)
16+
}
17+
1018
BOT = {
11-
"VERSION": "0.7.0-beta",
19+
"VERSION": "0.8.0-beta",
1220
"REPO_LINK": "https://github.com/fterh/sneakpeek",
1321
"CONTRIBUTE_LINK": "https://github.com/fterh/sneakpeek"
1422
}

main.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
import traceback
1+
import logging
22
import praw
33
import config
44
from scan import scan
55

66

7-
def start():
8-
print("Starting main")
7+
def setup_logging():
8+
root = logging.getLogger()
9+
root.setLevel(config.LOGGING["LEVEL"])
10+
11+
handler = config.LOGGING["HANDLER"]
12+
handler.setLevel(logging.DEBUG)
13+
14+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
915

16+
handler.setFormatter(formatter)
17+
root.addHandler(handler)
18+
19+
20+
def start():
21+
logging.info("Starting application")
22+
logging.info("Instantiating Reddit instance")
1023
reddit = praw.Reddit(
1124
client_id=config.CLIENT["ID"],
1225
client_secret=config.CLIENT["SECRET"],
@@ -20,9 +33,10 @@ def start():
2033
# This should never happen,
2134
# because it breaks the infinite subreddit monitoring
2235
# provided by subreddit.stream.submissions()
23-
print("Exception occurred while scanning. This should never happen!")
24-
traceback.print_exc()
36+
logging.critical("Exception occurred while scanning. This should never happen.")
37+
logging.critical(e)
2538

2639

2740
if __name__ == "__main__":
41+
setup_logging()
2842
start()

qualify.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from handler import HandlerManager
23

34

@@ -8,10 +9,14 @@ def qualify(submission):
89
(1) Submission is a link
910
(2) Submission has a Handler
1011
"""
12+
logging.info("Qualifying submission id: {}".format(submission.id))
13+
1114
# Check (1) Submission is a link
1215
is_link = not submission.is_self
1316

1417
# Check (2) Submission has a Handler
1518
has_handler = HandlerManager.has_handler(submission.url)
1619

20+
logging.debug("is_link = {}, has_handler = {}".format(is_link, has_handler))
21+
1722
return is_link and has_handler

scan.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import traceback
2-
31
import config
2+
import logging
43
import sys
54
from handler import HandlerManager
65
from comment import format_comment
@@ -9,46 +8,64 @@
98

109
def scan(subreddit):
1110
"""Scan a Subreddit for new submissions."""
12-
print("Starting scan")
11+
logging.info("Starting scan")
1312

1413
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+
))
1619

1720
does_qualify = qualify(submission)
1821

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")
2127

28+
handler = None
29+
try:
30+
logging.info("Attempting to get article handler")
2231
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
2357

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:
2563
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")
2767
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))
4670
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")

test_scan.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sqlite3
21
import unittest
32
from unittest import mock, TestCase
43

0 commit comments

Comments
 (0)