Putting some of my Python knowledge to a good use: a Reddit reading bot!
This article is originally published at http://firsttimeprogrammer.blogspot.com/
One of the perks of knowing a programming language is that you can build your own tools and applications. Depending on what you need, it may even be a fast process since you usually do not need to write production grade code and a detailed documentation (although it might still be helpful in the future).
I’ve got used to read news stuff on Reddit, however, it sometimes can be a bit time consuming since it tends to keep you wandering through every and each rabbit hole that pops up. This is fine if you are commuting and have just some spare time to spend on browsing the web but sometimes I just need a quick glance at what’s new and relevant to my interests.
In order to automate this search process, I’ve written a bot that takes as input a list of subreddits, a list of keywords and flags and browses each subreddit looking for the given keywords.
If a keyword is found inside either the body or in the title of a post which has been submitted in one of the selected subreddits, the post title and the links are either printed in the console or saved in a file (in this case the file name must be supplied when starting the search).
The bot is written using praw.
What do I need to use the bot?
In order to use the bot you’ll need to set up an app using your Reddit account and save the client_id, client_secret, username and password in a file named config_data.py which should be stored in the same folder as the reddit_browsing_bot_main.py script.
How does the bot work?
The bot is designed to be a command line application and can be used either in Linux terminal or in the PowerShell if you are the Windows type ;)
This choice of adopting a CLI was undoubtebly a bad choice if I wanted to make other people use the application but in my case I am the end user, and I like command line tools, a lot.
For each subreddit entered, the bot checks if that subreddit exists, if it doesn’t the subreddit is discarded. Then, within each subreddit, the bot searches the first –l posts and returns the posts that contained at least a keyword.
This is an example of use:
reddit_browsing_bot_main.py -s python -k pycon -l 80 -f new -o output.txt –v
In the example above I am searching the first 80 posts in the “new” section of the python
subreddit for posts that mention pycon. The –o flag tells the program to output the results
of the search to the output.txt file. The –v flag makes the program print the output to the
console.
You can search in more subreddits and/or use more keywords, just separate each new
subreddit/keyword with a comma. If you did not supply an output file, the program will
just output the results to the console.
Type:
reddit_browsing_bot_main.py -h
for a help menu. Maybe in the future I’ll add some features but for now this is pretty much it.
Is it ok to use the bot?
As far as I know, the bot is not violating any of the terms written in the Reddit’s API. Also,
the API calls are already limited by the praw module in order to comply with the Reddit’s
API limits. The bot is not downvoting nor upvoting any post, it just reads what is online.
Anyway, should you want to check the code yourself, it is available on its dedicated
GitHub repository or on the project page.
I’ve also copied and pasted a gist below so that you can have a look at the code here:
Thanks for visiting r-craft.org
This article is originally published at http://firsttimeprogrammer.blogspot.com/
Please visit source website for post related comments.