Skip to content

Commit

Permalink
Merge pull request #29 from fterh/release-v0.7.0-beta
Browse files Browse the repository at this point in the history
Release v0.7.0 beta
  • Loading branch information
fterh authored May 4, 2019
2 parents 7c72d5b + 069eeb1 commit bf5bb29
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 306 deletions.
37 changes: 16 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@ too long.
4. Possibly commit minor bug fixes to the release branch
5. Merge the release branch into `master` and `develop`

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

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

## How it works
### General
`main.py` starts the bot and calls `scan(subreddit)` (in `scan.py`),
which scans for submissions in the provided subreddit.
which monitors for new submissions in the provided subreddit.

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

A submission qualifies for preview if it:
1. Is a link
2. Has not been encountered by the bot previously
3. Has a Handler for the website
2. Has a Handler for the website

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

### Database
|submission_id|action|notes
|--|--|--
|text|text|text

A list of valid actions is provided by `DatabaseActionEnum` in the
`database` module: `ERROR`, `SKIP`, and `SUCCESS`.

## Running and deploying
All the commands below assume you have already activated the
virtual environment (`pipenv shell`). Alternatively, prepend `pipenv run` to
the commands.

### Development
`invoke run`
`python main.py`

### Testing
`invoke test`

### Production
`invoke start` (possibly with `nohup`)
`ENV=prod python main.py` or `ENV=prod nohup python main.py &`

## Changelog
### v0.7.0-beta
* Fix random crashes (issue #25)
* Fix README formatting issues
* Clean up code
* Update README developer documentation

### v0.6.0-beta
* Update subreddit monitoring implementation (fixes #25)
* Update subreddit monitoring implementation
### v0.5.0-beta
* Fix program crash when exception occurs (@yleong PR #22)
* Fix exception in handling Ricemedia links (@yleong PR #20)
Expand All @@ -114,12 +109,12 @@ the commands.
* Fix start script to run immediately
* Fix long lines in README

#### v0.2.0-beta
### v0.2.0-beta
* Add scheduling to run every 2 minutes
* Update database module to be compatible with new database table structure
(3 columns)

#### v0.1.0-beta
### v0.1.0-beta
* Minimum viable product
* Supports channelnewsasia.com, mothership.sg, ricemedia.co, straitstimes.com,
todayonline.com, zula.sg
19 changes: 1 addition & 18 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@
# Set to "prod" in production, but default to "dev"
ENV = os.getenv("ENV", "dev")

# In minutes
RUN_EVERY = 2

BOT = {
"VERSION": "0.6.0-beta",
"VERSION": "0.7.0-beta",
"REPO_LINK": "https://github.com/fterh/sneakpeek",
"CONTRIBUTE_LINK": "https://github.com/fterh/sneakpeek"
}

DATABASE = {
"NAME": "main.db" if ENV == "prod" else (
"main_test.db" if ENV == "test" else "main_dev.db"),
"TABLES": {
"SUBMISSIONS": {
"NAME": "submissions",
"ID_NAME": "submission_id",
"ACTION_NAME": "action",
"ID_INDEX_NAME": "idx_submission_id"
}
}
}

CLIENT = {
"ID": os.getenv("CLIENT_ID"),
"SECRET": os.getenv("CLIENT_SECRET")
Expand All @@ -40,5 +24,4 @@
USER_AGENT = os.getenv("USER_AGENT")

SUBREDDIT = "singapore" if ENV == "prod" else "rsgretrivr"
LIMIT = 10
COMMENT_LENGTH_LIMIT = 9900
85 changes: 0 additions & 85 deletions database.py

This file was deleted.

8 changes: 0 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import time
import traceback

import praw
import schedule

import config
from database import DatabaseManager
from scan import scan


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


DatabaseManager.disconnect()


if __name__ == "__main__":
start()
Binary file removed main_dev.db
Binary file not shown.
Binary file removed main_test.db
Binary file not shown.
11 changes: 3 additions & 8 deletions qualify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from database import DatabaseManager
from handler import HandlerManager


Expand All @@ -7,16 +6,12 @@ def qualify(submission):
Check if a submission qualifies to be previewed by the bot.
Conditions for preview:
(1) Submission is a link
(2) Submission has not been previously encountered
(3) Submission has a Handler
(2) Submission has a Handler
"""
# Check (1) Submission is a link
is_link = not submission.is_self

# Check (2) Submission is new
is_new = not DatabaseManager.check_id(submission.id)

# Check (3) Submission has a Handler
# Check (2) Submission has a Handler
has_handler = HandlerManager.has_handler(submission.url)

return is_link and is_new and has_handler
return is_link and has_handler
20 changes: 5 additions & 15 deletions scan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import traceback

import config
from database import DatabaseManager, DatabaseActionEnum
import sys
from handler import HandlerManager
from comment import format_comment
from qualify import qualify
Expand All @@ -11,7 +11,7 @@ def scan(subreddit):
"""Scan a Subreddit for new submissions."""
print("Starting scan")

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

does_qualify = qualify(submission)
Expand All @@ -38,27 +38,17 @@ def scan(subreddit):
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)

# Flush stdout buffer
sys.stdout.flush()

def skip(submission):
# If submission does not qualify, write SKIP to database only if it is new.
print("Submission does not qualify")
print("Checking if submission is new")
if DatabaseManager.check_id(submission.id):
print("Submission already exists in database. Skipping.")
else:
print("Attempting to write skip to database")
DatabaseManager.write_id(submission.id, DatabaseActionEnum.SKIP)
print("Database write succeeded")
Binary file removed template.db
Binary file not shown.
60 changes: 0 additions & 60 deletions test_database.py

This file was deleted.

Loading

0 comments on commit bf5bb29

Please sign in to comment.