Created
July 1, 2010 10:57
-
-
Save DRMacIver/459828 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
require "rubygems" | |
require "open-uri" | |
require "json" | |
require "set" | |
require "date" | |
if ARGV.length != 1 | |
STDERR.puts "usage: fetch.rb username" | |
end | |
followers = JSON.parse(open("http://twitter.com/followers/ids.json?screen_name=#{ARGV[0]}").read).sort | |
old_followers = if File.exists? "followers" then IO.read("followers").split("\n").map{|x| x.to_i} end | |
def name_of_id(id) | |
JSON.parse(open("http://twitter.com/users/show.json?id=#{id}").read)["screen_name"] || (raise "couldn't find user with ID #{id}") | |
end | |
followed = nil | |
unfollowed = nil | |
if old_followers | |
fs = Set[*followers] | |
ofs = Set[*old_followers] | |
followed = (fs - ofs).map{|x| name_of_id(x) rescue nil}.compact | |
unfollowed = (ofs - fs).map{|x| name_of_id(x) rescue nil}.compact | |
if followed.empty? && unfollowed.empty? | |
exit(0) | |
end | |
now = DateTime.now | |
date = now.strftime "%d %B %Y" | |
time = now.strftime "%H:%M" | |
File.open("follow.log", "a"){ |o| | |
followed.each{|p| | |
o.puts "#{p} started following you at #{time} on #{date}" | |
} | |
unfollowed.each{|p| | |
o.puts "#{p} stopped following you at #{time} on #{date}" | |
} | |
} | |
end | |
File.open("followers", "w"){ |o| | |
o.puts followers | |
} | |
system "git add followers" | |
system "git add follow.log" | |
system "git commit followers follow.log -m 'updated follow list'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment