Skip to content

Commit bf5bb29

Browse files
authored
Merge pull request #29 from fterh/release-v0.7.0-beta
Release v0.7.0 beta
2 parents 7c72d5b + 069eeb1 commit bf5bb29

12 files changed

Lines changed: 35 additions & 306 deletions

README.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,19 @@ too long.
2727
4. Possibly commit minor bug fixes to the release branch
2828
5. Merge the release branch into `master` and `develop`
2929

30-
This project follows this [Git branching workflow]
31-
(https://nvie.com/posts/a-successful-git-branching-model/):
32-
30+
This project follows this [Git branching workflow](https://nvie.com/posts/a-successful-git-branching-model/):
3331

3432
## How it works
3533
### General
3634
`main.py` starts the bot and calls `scan(subreddit)` (in `scan.py`),
37-
which scans for submissions in the provided subreddit.
35+
which monitors for new submissions in the provided subreddit.
3836

39-
`scan` gets a pre-configured number (`config.LIMIT`) of the latest submissions
40-
and checks if they qualify for preview by calling `qualify` (in `qualify.py`).
37+
For each new submission, `scan` checks if they qualify for preview
38+
by calling `qualify` (in `qualify.py`).
4139

4240
A submission qualifies for preview if it:
4341
1. Is a link
44-
2. Has not been encountered by the bot previously
45-
3. Has a Handler for the website
42+
2. Has a Handler for the website
4643

4744
If a submission qualifies, `scan` calls the `handle` method of the Handler
4845
to generate the raw comment, then `format_comment(comment)` in the
@@ -66,31 +63,29 @@ The comments module (in `comment.py`) exports the Comment class,
6663
which all Handlers must return. A Comment class requires a `title` and `body`,
6764
and accepts a `byline` and `attribution` (which are optional).
6865

69-
### Database
70-
|submission_id|action|notes
71-
|--|--|--
72-
|text|text|text
73-
74-
A list of valid actions is provided by `DatabaseActionEnum` in the
75-
`database` module: `ERROR`, `SKIP`, and `SUCCESS`.
76-
7766
## Running and deploying
7867
All the commands below assume you have already activated the
7968
virtual environment (`pipenv shell`). Alternatively, prepend `pipenv run` to
8069
the commands.
8170

8271
### Development
83-
`invoke run`
72+
`python main.py`
8473

8574
### Testing
8675
`invoke test`
8776

8877
### Production
89-
`invoke start` (possibly with `nohup`)
78+
`ENV=prod python main.py` or `ENV=prod nohup python main.py &`
9079

9180
## Changelog
81+
### v0.7.0-beta
82+
* Fix random crashes (issue #25)
83+
* Fix README formatting issues
84+
* Clean up code
85+
* Update README developer documentation
86+
9287
### v0.6.0-beta
93-
* Update subreddit monitoring implementation (fixes #25)
88+
* Update subreddit monitoring implementation
9489
### v0.5.0-beta
9590
* Fix program crash when exception occurs (@yleong PR #22)
9691
* Fix exception in handling Ricemedia links (@yleong PR #20)
@@ -114,12 +109,12 @@ the commands.
114109
* Fix start script to run immediately
115110
* Fix long lines in README
116111

117-
#### v0.2.0-beta
112+
### v0.2.0-beta
118113
* Add scheduling to run every 2 minutes
119114
* Update database module to be compatible with new database table structure
120115
(3 columns)
121116

122-
#### v0.1.0-beta
117+
### v0.1.0-beta
123118
* Minimum viable product
124119
* Supports channelnewsasia.com, mothership.sg, ricemedia.co, straitstimes.com,
125120
todayonline.com, zula.sg

config.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,12 @@
77
# Set to "prod" in production, but default to "dev"
88
ENV = os.getenv("ENV", "dev")
99

10-
# In minutes
11-
RUN_EVERY = 2
12-
1310
BOT = {
14-
"VERSION": "0.6.0-beta",
11+
"VERSION": "0.7.0-beta",
1512
"REPO_LINK": "https://github.com/fterh/sneakpeek",
1613
"CONTRIBUTE_LINK": "https://github.com/fterh/sneakpeek"
1714
}
1815

19-
DATABASE = {
20-
"NAME": "main.db" if ENV == "prod" else (
21-
"main_test.db" if ENV == "test" else "main_dev.db"),
22-
"TABLES": {
23-
"SUBMISSIONS": {
24-
"NAME": "submissions",
25-
"ID_NAME": "submission_id",
26-
"ACTION_NAME": "action",
27-
"ID_INDEX_NAME": "idx_submission_id"
28-
}
29-
}
30-
}
31-
3216
CLIENT = {
3317
"ID": os.getenv("CLIENT_ID"),
3418
"SECRET": os.getenv("CLIENT_SECRET")
@@ -40,5 +24,4 @@
4024
USER_AGENT = os.getenv("USER_AGENT")
4125

4226
SUBREDDIT = "singapore" if ENV == "prod" else "rsgretrivr"
43-
LIMIT = 10
4427
COMMENT_LENGTH_LIMIT = 9900

database.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

main.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import time
21
import traceback
3-
42
import praw
5-
import schedule
6-
73
import config
8-
from database import DatabaseManager
94
from scan import scan
105

116

@@ -29,8 +24,5 @@ def start():
2924
traceback.print_exc()
3025

3126

32-
DatabaseManager.disconnect()
33-
34-
3527
if __name__ == "__main__":
3628
start()

main_dev.db

-16 KB
Binary file not shown.

main_test.db

-16 KB
Binary file not shown.

qualify.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from database import DatabaseManager
21
from handler import HandlerManager
32

43

@@ -7,16 +6,12 @@ def qualify(submission):
76
Check if a submission qualifies to be previewed by the bot.
87
Conditions for preview:
98
(1) Submission is a link
10-
(2) Submission has not been previously encountered
11-
(3) Submission has a Handler
9+
(2) Submission has a Handler
1210
"""
1311
# Check (1) Submission is a link
1412
is_link = not submission.is_self
1513

16-
# Check (2) Submission is new
17-
is_new = not DatabaseManager.check_id(submission.id)
18-
19-
# Check (3) Submission has a Handler
14+
# Check (2) Submission has a Handler
2015
has_handler = HandlerManager.has_handler(submission.url)
2116

22-
return is_link and is_new and has_handler
17+
return is_link and has_handler

scan.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import traceback
22

33
import config
4-
from database import DatabaseManager, DatabaseActionEnum
4+
import sys
55
from handler import HandlerManager
66
from comment import format_comment
77
from qualify import qualify
@@ -11,7 +11,7 @@ def scan(subreddit):
1111
"""Scan a Subreddit for new submissions."""
1212
print("Starting scan")
1313

14-
for submission in subreddit.stream.submissions():
14+
for submission in subreddit.stream.submissions(skip_existing=True):
1515
print("Operating on submission ID: " + submission.id)
1616

1717
does_qualify = qualify(submission)
@@ -38,27 +38,17 @@ def scan(subreddit):
3838
print("Attempting to post a comment")
3939
submission.reply(comment_markdown)
4040
print("Comment posting succeeded")
41-
print("Attempting to write success to database")
42-
DatabaseManager.write_id(submission.id, DatabaseActionEnum.SUCCESS)
43-
print("Database write succeeded")
4441
except Exception as e:
4542
print("An error occurred:")
4643
print(e)
4744
else:
4845
print("Submission is too long to be posted.")
49-
print("Attempting to write skip to database")
50-
DatabaseManager.write_id(submission.id, DatabaseActionEnum.SKIP)
51-
print("Database write succeeded")
5246
else:
5347
skip(submission)
5448

49+
# Flush stdout buffer
50+
sys.stdout.flush()
51+
5552
def skip(submission):
5653
# If submission does not qualify, write SKIP to database only if it is new.
5754
print("Submission does not qualify")
58-
print("Checking if submission is new")
59-
if DatabaseManager.check_id(submission.id):
60-
print("Submission already exists in database. Skipping.")
61-
else:
62-
print("Attempting to write skip to database")
63-
DatabaseManager.write_id(submission.id, DatabaseActionEnum.SKIP)
64-
print("Database write succeeded")

template.db

-16 KB
Binary file not shown.

test_database.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)