วันศุกร์, พฤษภาคม 16, 2551

เอามาแบ่งปัน เผื่อ tweetple ท่านไหนอยากเอาไปเล่น
เขียนเล่นๆได้ออกมาแบบนี้
อย่าหวังผลว่าจะถูกต้องมาก เขียนเอามันส์ :)

#! /usr/bin/env ruby
################################################
# pre-installed
# 1. sudo gem install activesupport
# 2. sudo gem install hpricot
# 3. sudo gem install gchart
# How to run?

# username = 'whoyouare'
# city = 'whereareyou'
# tweets = Tweet.new(username, :local => city) #
# tweets.stats(:of => :last_24hrs)

# just for fun script, dont' trust the result :P
#################################################
require 'rubygems'
require 'activesupport'
require 'hpricot'
require 'open-uri'
require 'gchart'

class Tweet
def initialize(username, options={})
@username = username
@twittr_url = "http://twitter.com/#{@username}"
@page = 1
@doc = Hpricot(twittr_page)
@tweets = [current_tweet]
@tweets.concat(page_tweets)
local = options[:local]
locality(local) unless local.nil?
end

def locality(city)
# where else you could add.. i'm lazy...
case city
when 'bangkok'
@zone_offset = 7
when 'london', 'paris'
@zone_offset = 1
when 'boston'
@zone_offset = -4
else
@zone_offset = 0
end
end

def twittr_page
page = ""
open("#{@twittr_url}?page=#{@page}",
"User-Agent" => "Ruby/#{RUBY_VERSION}",
"From" => "zdk",
"Referer" => "http://ziddik.blogspot.com/") {|f|
f.each_line { |line| page << line }
}
page
end

def current_tweet
tweet,time = @doc/'div.desc'/'p'
tweet = tweet.inner_html
time = DateTime.parse(time.at('abbr')['title'])
[tweet, time]
end

def page_tweets
(@doc/'div.tab'/'tr.hentry').map do |tweet|
tweet,time = tweet/'span'
tweet = tweet.inner_html.gsub(/^\s*(.*)\s*$/, '\1')
time = DateTime.parse(time.at('abbr')['title'])
[tweet, time]
end
end

def has_older?
(@doc/'div.tab'/'div.pagination'/'a').last.inner_text =~ /Older/
end

def each_tweet
if @tweets.empty?
return nil unless has_older?
@page += 1
@doc = Hpricot(twittr_page)
@tweets = page_tweets
end
@tweets.shift
end

def break_when(options = {})
current_time = options[:current]
tweet_time = options[:tweet]
case options[:stat]
when :last_24hrs
ressult = current_time.yesterday >= tweet_time and tweet_time.year == current_time.year
when :per_hour
result = current_time.months_ago(1) >= tweet_time # get 1 month stats
end
return result
end

def parse_date(time)
return DateTime.parse(time)
end

def get_stats(stat)
hour_data = Array.new(24, 0)
current_update_time = current_tweet.last
while t = each_tweet
tweet,time = t
hour_data[(time.hour+(@zone_offset))%24] += 1
current_year = DateTime.now.year
current_time = parse_date(current_update_time.to_s)
tweet_time = parse_date(time.to_s)
break if break_when(:stat => stat, :current => current_time, :tweet => tweet_time)
end
chart_url = GChart.line(
:title => "#{@username} Tweets #{stat.to_s.humanize}",
:data => hour_data,
:width => 400,
:height => 300,
:extras => { 'chxt' => 'x,y', 'chxl' => "0:|#{(0..23).to_a.join('|')}|1:|#{hour_data.min}|#{hour_data.max}" }
).to_url
puts chart_url
end

def last_24hrs_stats
get_stats(:last_24hrs)
end

def per_hour_stats
get_stats(:per_hour)
end

def stats(options = {})
case options[:of].to_sym
when :last_24hrs
last_24hrs_stats
when :per_hour
per_hour_stats
end
end
end

username = 'zdk'
city = 'bangkok'
tweets = Tweet.new(username, :local => city) #
tweets.stats(:of => :last_24hrs)