A Reddit bot that previews hyperlinks and posts their contents as a comment. It should never spam or double-post, and will skip a comment if it is too long.
- businesstimes.com.sg
- channelnewsasia.com
- channelnewsasia.com (CNAlifestyle)
- mothership.sg
- ricemedia.co
- straitstimes.com
- tnp.sg
- todayonline.com
- yahoo.com
- zula.sg
PRs are always welcome.
- Write Handlers to support more websites
- Improve test coverage
- Improve documentation
- Create a release branch (e.g.
release-v1.2.3
) from thedevelop
branch - Bump version numbers in
config.py
to the release branch - Update README
- Possibly commit minor bug fixes to the release branch
- Merge the release branch into
master
anddevelop
This project follows this Git branching workflow.
The program requires an environmental variable SUBREDDIT
to be set.
This specifies the subreddit that the bot will monitor.
If it's not set, the default subreddit /r/all
will be monitored.
At the moment, only 1 subreddit may be specified.
All the commands below assume that the virtual environment has been activated
(pipenv shell
).
- Running:
SUBREDDIT=name python main.py
(orSUBREDDIT=name nohup python main.py &
) - Testing:
invoke test
This application can be built and run as a Docker image.
main.py
starts the bot and calls scan(subreddit)
(in scan.py
),
which monitors for new submissions in the provided subreddit.
For each new submission, scan
checks if they qualify for preview
by calling qualify
(in qualify.py
).
A submission qualifies for preview if it:
- Is a link
- 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
comments module to generate the final comment in Markdown.
If the final comment in Markdown does not exceed a pre-configured comment length
(config.COMMENT_LENGTH_LIMIT
), the comment is posted, and the action written
to the database (through DatabaseManager
) to prevent double-posting.
Logging is written to standard output, and logging level can be configured in
config.py
.
handler.py
contains a HandlerManager that checks if a website has a Handler.
A Handler is a class with a @classmethod handle(cls, url)
that accepts a URL
and returns a Comment. All Handlers must inherit from AbstractBaseHandler
.
The Handler can be part of a module or package, and have as many supporting sub-modules or sub-packages as necessary.
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).