Created
July 24, 2009 09:41
-
-
Save hc5duke/153939 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
require 'rubygems' | |
require 'open-uri' | |
require 'json' | |
swivelers = ['badave', 'burnto', 'bmulloy', 'bwalsh', 'gerad', 'hc5duke', 'huned', 'kalendae', 'nataliegrae', 'quanghiem', 'teamswivel', 'visnup'].sort | |
debug = true | |
mode = 'total' # or 'daily' or 'weekly' | |
daily = {} | |
# grab files first | |
swivelers.each do |u| | |
unless File.exist?("saved_#{u}.twitfeed") | |
# grab remote feeds | |
puts "> grab new feeds for #{u}" if debug | |
url = "http://twitter.com/statuses/user_timeline.json?id=#{u}&count=3200" | |
dates = open(url) { |f| JSON.parse f.read }.map { |e| Time.parse e['created_at'] } | |
File.open("saved_#{u}.twitfeed", 'w') {|f| f.write(dates.to_json) } | |
end | |
end | |
swivelers.each do |u| | |
puts "> grab local copy for #{u}" if debug | |
dates = File.open("saved_#{u}.twitfeed") { |f| JSON.parse f.read } | |
dates.each do |t| | |
# remove this line later | |
t = Time.parse(t) if t.kind_of? String | |
if mode == 'weekly' | |
t = t.strftime('%a') # day of week | |
else | |
t = t.strftime('%Y-%m-%d') | |
end | |
daily[t] ||= {} | |
daily[t][u] = (daily[t][u]||0) + 1 | |
end | |
sum = 0 | |
total = daily.map { 0 } | |
daily.sort.each { |k| sum += daily[k] && daily[k][u] || 0 } | |
puts "done with #{u}" if debug | |
end | |
total = {} | |
prev_date = {} | |
swivelers.each {|s| prev_date[s] = 0 } | |
puts prev_date.to_json | |
daily.keys.sort.each do |k| | |
total[k] = {} | |
swivelers.each {|u| total[k][u] = prev_date[u] + (daily[k][u] || 0)} | |
prev_date = total[k] | |
end | |
if mode == 'daily' | |
# daily statistics | |
puts "Twivtel\t" + swivelers.map {|u| "\t#{u}"}.join | |
daily.keys.sort.each do |k| | |
puts "#{k}\t" + swivelers.map {|u| "\t#{daily[k][u] || 0}"}.join | |
end | |
else | |
# total | |
puts "Twivtel" + swivelers.map {|u| "\t#{u}"}.join | |
total.keys.sort.each do |k| | |
puts "#{k}" + swivelers.map {|u| "\t#{total[k][u] || 0}"}.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment