Hatena::Grouprubyist

たばさの RSSフィード

04/10(土) 2010

twitter新着の差分を表示

| 12:19 | twitter新着の差分を表示 - たばさの を含むブックマーク はてなブックマーク - twitter新着の差分を表示 - たばさの

ごちゃごちゃ。

hpricotとnokogiriで動くように

#!ruby -Ks
# -*- encoding: "Shift_JIS" -*-
# twittergetById.rb

require 'mechanize'
require 'kconv'
require 'optparse'

delim="\n"
$max=500
interval=60*5
id="tabasano"
opt = OptionParser.new
opt.on('-i val',"id") {|v| id=v }
opt.on('-s',"single (check onece)") {|v| $once=v }
opt.on('-b',"beep") {|v| $beep=v }
opt.on('-n i',"twit number") {|v| $number=v.to_i }
opt.on('-p i',"log print current number") {|v| $printnumber=v.to_i }
opt.on('-j i',"delim for joining") {|v| delim=v }
opt.on('-t val',"interval") {|v| interval=v.to_i if v.to_i>=60 }
opt.parse!(ARGV)

$logfile="log.id.#{id}.txt"
def save data
  open($logfile,"a"){|f| f.puts data }
end
def load
  if File.exist? $logfile
    File.readlines($logfile).map{|i|i.chomp}
  else
    []
  end
end
def logtrim
  max=$max
  file=$logfile
  return if load==[]
  data=File.readlines(file)
  puts "log size #{data.size}" if $DEBUG
  if data.size>max
    open(file,"w"){|f|
      f.puts data[0..max/2]
    }
  end
end

agent = Mechanize.new 
agent.max_history=1
agent.read_timeout = 100

def itemform text,time
  "#{text} _#{time.strftime("%Y/%m/%d %H:%M:%S")}"
end

url="http://twitter.com/"
last=""
logtrim
log=load
puts "(log size: #{log.size})" if $DEBUG
first=true
while true
  begin
    page = agent.get(url+id)
    $stderr.print"."
    items=page.search('li/span')
    tmp=items.map{|data|
      next if data.search('span.timestamp')[0]==nil
      timestamp=data.search('span.timestamp')
      begin
        timestamp[0].attribute('data').text=~/'([^']+)/
        time=$1
      rescue
        time=timestamp[0].to_s
        time=~/'([^']+)/
        time=$1
      end
      puts time if $DEBUG
      time=Time.parse(time.tosjis)
      begin
        text=data.search('span.entry-content')[0].text.tosjis.gsub(/\s+/," ")
      rescue
        text=data.search('span.entry-content')[0].inner_text.tosjis.gsub(/\s+/," ")
      end
      puts itemform(text,time) if $DEBUG
      [text,time]
    }.reject{|i| i==nil}
    data = tmp.map{|i| itemform(i[0],i[1])}
    data -= log
    save(data)
    log += data
    puts log*"/" if $DEBUG && first
    if (not $DEBUG) && $printnumber
      puts log.uniq.sort_by{|i|
        i=~/_(.*)/;Time.parse($1)
      }.reverse[0...$printnumber]*"/"
    end
    if first
      showdata=data
      showdata=data[0...$number] if $number
      puts showdata*delim
    end
    if data !=[] && !first
      puts data*delim
      if $beep
        print"\a\a"
      end
    end
    first =false
    last=data
  rescue Exception => e
    STDERR.puts e
    sleep 60
  end
  break if $once
  sleep interval 
end

トラックバック - http://rubyist.g.hatena.ne.jp/hatecha/20100410
カレンダー
<< 2010/04 >>
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
archive Error : RSSが取得できませんでした。