Created
November 22, 2011 03:04
-
-
Save hannahherbig/1384779 to your computer and use it in GitHub Desktop.
just an example of how you can poll reddit
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
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