Skip to content

Instantly share code, notes, and snippets.

@hannahherbig
Created November 22, 2011 03:04
Show Gist options
  • Save hannahherbig/1384779 to your computer and use it in GitHub Desktop.
Save hannahherbig/1384779 to your computer and use it in GitHub Desktop.
just an example of how you can poll reddit
require 'open-uri'
require 'json'
received_posts = [] # posts we've already received
loop do
# get all of the current posts
posts = open('http://www.reddit.com/new.json') { |f| JSON.parse(f.read) }
posts = posts['data']['children']
# get a list of new posts by taking the ones we just received and removing
# the ones we've received before
new_posts = posts - received_posts
new_posts.each do |post|
# put this one into the list of received ones, so we don't get it again
received_posts << post
post = post['data']
# print out the title
puts post['title']
end
sleep 60 # wait 1 minute before trying again
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment