Monday 18 September 2017

Putting some of my Python knowledge to a good use: a Reddit reading bot!

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:

16 comments:

  1. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    python training in chennai

    ReplyDelete
  2. Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing.data science course in malaysia

    ReplyDelete
  3. Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.data science certification malaysia

    ReplyDelete
  4. This article is a great article that I have seen in my python programming career so far.

    website development company in Surat Gujarat

    ReplyDelete
  5. Python Training in Pune
    Thanks for sharing the information about the python and keep updating us.This information is really useful to me.

    Python Classes in Pune

    ReplyDelete

  6. Thank you for sharing wonderful information with us to get some idea about that content.
    Python Online Training Course
    Python Developer Course Online Training

    ReplyDelete
  7. Hi, I read your whole blog. This is very nice. Good to know about the career in. Python Training & Certification , anyone interested can Python Training for making their career in this field.

    ReplyDelete
  8. Thanks for sharing such a useful and wonderful post.
    Python Course in Nagpur

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. amazing write , keep posting and if you are intresting in big data coder and code developer then checkout python classes in pune

    ReplyDelete