Created
March 24, 2014 08:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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