Created
August 31, 2010 16:33
-
-
Save aereal/559307 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
#!/usr/bin/env ruby | |
# !!!!! OAuth !!!!! | |
require "rubygems" | |
require "logger" | |
require "net/http" | |
require "json" | |
class Remtter | |
def initialize(env) | |
@env = { | |
:proxy_addr => nil, | |
:proxy_port => nil, | |
:log => STDOUT, | |
:debug => false, | |
:notify => nil, | |
:api_base => '/1/', | |
:host => 'twitter.com', | |
:waiting => 60 * 60, | |
}.merge(env) | |
@http = Net::HTTP::Proxy(env[:proxy_addr], env[:proxy_port]) | |
@latest = [] | |
@logger = Logger.new(@env[:log]) | |
@logger.level = @env[:debug] ? Logger::DEBUG : Logger::INFO | |
end | |
def start | |
loop do | |
diff = compare | |
if diff.size > 0 | |
users = lookup(*diff).map {|detail| detail['screen_name'] } | |
@env[:notify_command] && notify(*users) | |
@logger.info(*users) | |
end | |
sleep @env[:waiting] | |
end | |
end | |
private | |
def notify(*users) | |
__send__(@env[:notify_command], *users) | |
end | |
def notify_send(*users) | |
system("notify-send -i ~/Downloads/Twitter_Icon.svg 'These have stopped following you' '#{users.join(', ')}'") | |
end | |
def followers | |
api('followers/ids') | |
end | |
def lookup(*ids) | |
api('users/lookup' :user_id => ids.join(',')) | |
end | |
def compare | |
current = followers | |
ret = @latest - current | |
@latest = current | |
ret | |
end | |
def api(path, query={}) | |
path = @env[:api_base] + path | |
path = append_query_string(path, query) unless query.size > 0 | |
@http.start(@env[:host]) {|h| h.get(path) }.body | |
end | |
def append_query_string(path, query) | |
path + '?' + query.to_query_str | |
end | |
end | |
class Hash | |
def to_query_str | |
self.map {|(k, v)| "#{k}=#{v}" }.join('&') | |
end | |
end | |
def Remtter(env) | |
Remtter.new(env) | |
end | |
if $0 == __FILE__ | |
Remtter({ | |
:waiting => 5 * 60, | |
}).start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment