Reddit is a popular news/media/time-waster website where people share links and users democratically vote them up or down. I’m a big fan of movies and television, but I only subscribe to Netflix. When someone recommends a show, I would get slightly annoyed that I had to open a separate tab to determine if I could watch it. I made this bot that would automatically tell users like myself whether or not it’s available.
First import the necessary libraries, PRAW helps us scrape Reddit, NetflixRoulette tells us what’s available on Netflix, time will help us delay the bot, and re will let us work with regular expressions.
from NetflixRoulette import * import time import praw import re
Now for the initial setup. We login using praw then move it to the movies and television subreddit.
r = praw.Reddit('test') already_done = set() r.login('Name_of_the_bot', 'password', disable_warning=True) subreddit = r.get_subreddit('movies+television')
With our robot signed in and looking at the right subreddit, we can now make the main loop to check for posts that mention movies or tv that are on Netflix. The variable already_done will be used to record posts that we have already responded to to prevent double posting.
while True: try: subreddit_comments = subreddit.get_comments() flat_comments = praw.helpers.flatten_tree(subreddit_comments) for comment in flat_comments: m = re.search(r"is+.+on netflix", comment.body) if m and comment.id not in already_done: content_type = -1 title = comment.body[m.start()+3:m.end()-11] try: content_type = get_media_type(title) except urllib2.HTTPError,error: contents = error.read() if (content_type >= 0): comment.reply("Yes. ("+title+")") print("Success! "+title) already_done.add(comment.id) #time.sleep(2) except: print("Error!")
This bot only works while the script is running, which can be set up using cron jobs on a certain schedule. You will need to export already_done to a file if you want to repeatedly use this, otherwise it forget the posts it already responded to. Notice I have time.sleep(2) commented out for debugging purposes. Be careful about the number of requests and posts because you can get your bot banned. Read the PRAW guidelines here from their official website before deploying a reddit bot.
These bots are interesting because you can also use them as simple web scrapers. It is not a huge leap to use a similar bot to collect data on what movies and television are most popular with reddit users.
More ideas for reddit bots:
- Responds to people who mention dollar amounts with bitcoin amounts.
- “Darkweb” link aggregator that keeps track of working/broken links.
- Searches for your wordpress tags and if they are mentioned it either notifies your or replies with the relevant post.
- Gambling/blackjack bot – will deal you a hand and you tell it ‘hit’ or ‘stay’.
- Add in datasets or use APIs to authoritatively respond when certain topics are brought up.