Created
April 16, 2013 15:25
-
-
Save jonasschneider/5396860 to your computer and use it in GitHub Desktop.
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 'net/http' | |
require 'nokogiri' | |
def scrape | |
uri = URI('http://www.reddit.com/') | |
page = Net::HTTP.get(uri) | |
document = Nokogiri::HTML(page) | |
data = {} | |
document.css('#siteTable .thing').each do |story_el| | |
title_el = story_el.css(".title a").first | |
title = title_el.content | |
score = story_el.css(".score.unvoted").first.content.to_i | |
link = title_el[:href] | |
data[title] = { title: title, link: link, score: score } | |
end | |
data | |
end | |
# scrape.each do |id, entry| | |
# puts entry[:title] | |
# puts entry[:link] | |
# puts "Score: #{entry[:score]} points" | |
# puts | |
# end | |
old_state = scrape | |
loop do | |
state = scrape | |
staying_stories = old_state.keys & state.keys | |
staying_stories.each do |title| | |
if state[title][:score] != old_state[title][:score] | |
puts "#{title} score: #{old_state[title][:score]} -> #{state[title][:score]}" | |
end | |
end | |
new_stories = state.keys - old_state.keys | |
new_stories.each do |title| | |
puts "New: #{title} on frontpage with #{state[title][:score]}" | |
end | |
gone_stories = old_state.keys - state.keys | |
gone_stories.each do |title| | |
puts "#{title} gone from frontpage :( – had #{old_state[title][:score]} points" | |
end | |
sleep 5 | |
old_state = state | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment