Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View swivel_twitter.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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
Something went wrong with that request. Please try again.