Skip to content

Instantly share code, notes, and snippets.

@aereal
Forked from shenqi/sokuho.rb
Created September 16, 2010 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/582338 to your computer and use it in GitHub Desktop.
Save aereal/582338 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby -Ku
require 'net/http'
require 'rubygems'
require 'nokogiri'
require 'meow'
TOPURL = 'http://baseball.yahoo.co.jp/npb/schedule/'
$appname = 'Yahoo! プロ野球速報'
$image = '/Users/shenqi/baseball.png'
TIMEFORMAT = '%H:%M:%S'
def growl(title, message, url, option = 'default')
growl = Meow.new($appname)
icon = Meow.import_image($image)
opt = { :icon => icon }
case option
when 'default'
when 'impact' then opt.merge!( { :priority => '1' } )
when 'sticky' then opt.merge!( { :sticky => 'true' } )
when 'error' then opt.merge!( { :sticky => 'true', :priority => '1' } )
else #その他の処理
opt.merge!( { :sticky => 'true' } )
warn "invalid option '#{option}' given.")
end
growl.notify(title, message.chomp, opt) do
system("open '#{url}'")
end
end
def get_todays_match
cards, data = [], [] #initilise
teams = Nokogiri::HTML(Net::HTTP.get(URI(TOPURL))).xpath('//table[@class="teams"]')
teams.each do |team|
# cards << ['card', 'url', 'standby']
cards << [team.xpath('.//th[@class="bt bb bl"]/a/@title').to_a.join(' 対 '), 'http://baseball.yahoo.co.jp' + team.xpath('.//table[@class="score"]/tr[2]/td/a/@href').to_s, /standby/ =~ team.xpath('.//table[@class="score"]/tr[2]/td/@class').to_s]
cards.last << team.xpath('.//table[@class="score"]/tr[3]/td[@class="yjSt bl"]').text if cards.last.last
end
puts '-------今日の試合---------'
cards.length.times do |i|
puts "#{i}: #{cards[i-1][0]}"
end
puts '--------------------------'
#puts 'Puts the number of match you want to notify, then press enter.'
puts '観戦したい試合を番号で選んでください(複数の場合はスペースで区切ってください)'
#ToDo: error処理をする
gets.split.each {|t| data << cards[t.to_i - 1]}
return data
end
def notify(card)
baseurl = card[1]
#check weather the match has already started
growl(card[0], '試合開始は' + card[3] + 'からです。', baseurl, 'impact') if card[2] == true
unless card[2]
url = baseurl + 'text'
message, pmessage = 'none'
html = Nokogiri::HTML(Net::HTTP.get(URI(url)))
text = html.xpath('//td[@class="act"]/dl').text
growl('現在の試合状況', text, url, 'impact')
test = html.xpath('//div[@id="i1"]/preceding-sibling::div').empty?
unless test
title = html.xpath('//h3[@class="yjS"]')[1].text.gsub(' ', ' ')
status = html.xpath('//div[@id="yjSNLiveTextlive"]/div[1]/ul/li[last()]')
message = status.text
link = url + '#' + html.xpath('//div[@id="yjSNLiveTextlive"]/div[1]/@id').to_s
if message == pmessage
puts Time.now.strftime(TIMEFORMAT) + ': no update for ' + card[0]
elsif status.search('.//b').empty?
puts Time.now.strftime(TIMEFORMAT) + ': fetched new for ' + card[0]
growl(title, message, link)
else
puts Time.now.strftime(TIMEFORMAT) + ': fetched new with <b> for ' + card[0]
growl(title, message, link, 'impact')
end
pmessage = message
sleep(30)
end
Meow.new($title).notify(text, '本日の試合は終了しました。', {:icon => Meow.import_image($image), :priority => '1', :sticky => 'true'}) do
topurl = baseurl + 'top'
senpyo = Nokogiri::HTML(Net::HTTP.get(URI(topurl))).xpath('//div[@id="yjSNLiveBattlereview"]/p[2]').text
growl('戦評', text + ': ' + senpyo, topurl, 'sticky')
end
end
end
ar = get_todays_match
nt = ar.length
threads = []
for t in 1..nt do
threads << Thread::new(ar[t-1]) {|match| notify(match)}
sleep(0.5)
end
for t in 1..nt do
threads[t-1].join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment