Skip to content

Instantly share code, notes, and snippets.

@kevana
Created March 24, 2014 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevana/9736634 to your computer and use it in GitHub Desktop.
Save kevana/9736634 to your computer and use it in GitHub Desktop.
A quick and dirty script that monitors reddit's newest comments and searches for a particular substring in each comment.
# New comment monitor for reddit
#
# Copyright (C) 2014 Kevan Ahlquist
#
import requests
import json
import pprint
from time import sleep
url = r'http://www.reddit.com/comments.json' # Get comments from all subreddits
args = {'limit' : '10', # Retrieve at most 10 comments
'sort' : 'new'} # Retrieve the newest comments first
# This bot will run until the world ends or an error occurs
# It checks every 5 seconds for new comments and does a basic substring search
while True:
# Send request to the reddit API
r = requests.get(url, params=args)
# Process the results into a JSON object
rData = r.json()
# Check if any comments were returned
if rData['data']['children']:
# Loop through comments and
for comment in rData['data']['children']:
# Check for a particular substring in the comment body
if 'truth' in comment['data']['body']:
# This is where you can do things like:
# Print something out to the screen
# Send an email
# Reply to the comment
print('"truth" found:')
print(i['data']['body'])
print('-----')
# Make sure we don't process the same comment twice
# The API will only return comments posted after the comment ID we set here
if rData['data']['after']:
args['after'] = rData['data']['after']
else:
# No new comments have been posted since we last checked
pass
# Wait a few seconds
sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment