Skip to content

Instantly share code, notes, and snippets.

@siong1987
Created August 16, 2009 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save siong1987/168518 to your computer and use it in GitHub Desktop.
Save siong1987/168518 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'rufus/verbs' # gem rufus-verbs
require 'json' # gem json or json_pure
require 'growl' # gem growlnotifier (and rubycocoa)
def get_password key
result = `/usr/bin/security 2>&1 >/dev/null find-internet-password -gs #{key}`
if result =~ /password: "(.*)"/
$1
else
raise "Unknown password"
end
end
# get password from keychain
USER = 'user_name'
PASS = get_password 'twitter.com'
GROWL = Growl::Notifier.sharedInstance
GROWL.register('twatch', ['twitter_update', 'issue'])
class ObjectStash
def self.store obj, file_name
marshal_dump = Marshal.dump(obj)
file = File.new(file_name,'w')
file.write marshal_dump
file.close
return obj
end
def self.load file_name
begin
file = File.open(file_name, 'r')
ensure
obj = Marshal.load file.read
file.close
return obj
end
end
end
begin
$last_seen_id = ObjectStash.load "#{ENV['HOME']}/growl.stash"
rescue
$last_seen_id = ''
end
def fetch_tweets
if $last_seen_id == ''
res = Rufus::Verbs.get(
"https://twitter.com/statuses/friends_timeline.json",
:hba => [ USER, PASS ]
)
else
res = Rufus::Verbs.get(
"https://twitter.com/statuses/friends_timeline.json?since_id=#{$last_seen_id}",
:hba => [ USER, PASS ]
)
end
raise "#{res.code} != 200" if res.code.to_i != 200
json = JSON.parse(res.body)
json.each_with_index do |message, i|
# store the last id
$last_seen_id = message['id'] if i == 0
user = message['user']
u = user['profile_image_url']
u = OSX::NSURL.alloc.initWithString(u)
i = OSX::NSImage.alloc.initWithContentsOfURL(u)
GROWL.notify(
'twitter_update',
"#{user['name']} (#{user['screen_name']})",
message['text'],
:icon => i
)
end
end
begin
fetch_tweets
rescue Exception => e
GROWL.notify('issue', 'twatch issue', e.to_s)
end
ObjectStash.store $last_seen_id, "#{ENV['HOME']}/growl.stash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment