Created
April 9, 2009 10:33
-
-
Save snaka/92382 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/ruby | |
# | |
# Ruby/GNTP example : twitter notifier | |
# | |
require 'net/http' | |
require 'rubygems' | |
require 'json' | |
require 'pit' | |
require 'gntp' | |
$tweeted = {} | |
$growl = GNTP.new | |
$growl.register({ | |
:app_name => "Twitter", | |
:notifies => [{ | |
:name => "Tweet", | |
:enabled => true | |
},{ | |
:name => "Error", | |
:enabled => true | |
}] | |
}) | |
def get_timeline | |
max_count = 20 | |
config = Pit.get("twitter", :require => { | |
"username" => "your twittername", | |
"password" => "your password" | |
}) | |
Net::HTTP.version_1_2 | |
req = Net::HTTP::Get.new('/statuses/friends_timeline.json') | |
req.basic_auth config["username"], config["password"] | |
proxy_host = ENV["proxy_host"] | |
proxy_port = ENV["proxy_port"] | |
Net::HTTP::Proxy(proxy_host, proxy_port).start('twitter.com') {|http| | |
res = http.request(req) | |
if res.code != '200' | |
$growl.notify "Error", "Error occurd", "Can not get messages" | |
return | |
end | |
results = JSON.parser.new(res.body).parse() | |
results.reverse! | |
results.length.times do |i| | |
break if i >= max_count | |
id = results[i]["id"] | |
next if $tweeted.include?(id) | |
puts screen_name = results[i]["user"]["screen_name"] | |
puts text = results[i]["text"] | |
puts icon = results[i]["user"]["profile_image_url"] | |
$growl.notify({ | |
:name => "Tweet", | |
:title => screen_name, | |
:text => text, | |
:icon => icon | |
}) | |
$tweeted[id] = true | |
sleep 1 | |
end | |
} | |
end | |
while true do | |
get_timeline | |
sleep 30 | |
end | |
# vim: ts=2 sw=2 et fdm=marker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment