Skip to content

Instantly share code, notes, and snippets.

@huobazi
Forked from basicxman/so.rb
Created June 17, 2011 00:47
Show Gist options
  • Save huobazi/1030653 to your computer and use it in GitHub Desktop.
Save huobazi/1030653 to your computer and use it in GitHub Desktop.
A horrible script for Stack Overflow Growl notifications
require 'nokogiri'
require 'open-uri'
require 'json/pure'
require 'ruby-growl'
class StackOverflowNotifier
def initialize(*args)
@data = if File.exists? "data.json"
JSON.parse(File.read("data.json"))
else
{}
end
parse("http://stackoverflow.com/feeds/tag?tagnames=#{args.join("+")}&sort=newest")
write
end
def parse(page)
page = Nokogiri::XML(open(page))
entries = page.css("entry")
entries.each do |entry|
url = entry.css("id").text
title = entry.css("title").text
notify(url, title) unless @data[url]
end
end
def write
File.open("data.json", "w") { |f| f.write(@data.to_json) }
end
def notify(url, title)
g = Growl.new("localhost", "ruby-growl", ["Stack Overflow Notification"])
g.notify("Stack Overflow Notification", title, url)
`open #{url}`
@data[url] = title
end
end
File.open("so.log", "a") { |f| f.puts "Running on #{Time.now}" }
StackOverflowNotifier.new "ruby", "ruby-on-rails", "ruby-on-rails-3", "javascript"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment