-
-
Save ohac/875845 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 'em-http' | |
require 'json' | |
require 'notify' | |
keywords = <<EOK | |
愛知 | |
愛媛 | |
茨城 | |
岡山 | |
沖縄 | |
岩手 | |
岐阜 | |
宮崎 | |
宮城 | |
京都 | |
熊本 | |
群馬 | |
広島 | |
香川 | |
高知 | |
佐賀 | |
埼玉 | |
三重 | |
山形 | |
山口 | |
山梨 | |
滋賀 | |
鹿児島 | |
新潟 | |
神奈川 | |
青森 | |
静岡 | |
石川 | |
千葉 | |
大阪 | |
大分 | |
長崎 | |
長野 | |
鳥取 | |
島根 | |
東京 | |
徳島 | |
栃木 | |
奈良 | |
富山 | |
福井 | |
福岡 | |
福島 | |
兵庫 | |
北海道 | |
和歌山 | |
aichi | |
aomori | |
chiba | |
ehime | |
fukui | |
fukuoka | |
fukushima | |
gifu | |
gumma | |
hiroshima | |
hokkaido | |
hyougo | |
ibaraki | |
ishikawa | |
iwate | |
kagawa | |
kagoshima | |
kanagawa | |
kouchi | |
kumamoto | |
kyoto | |
mie | |
miyagi | |
miyazaki | |
nagano | |
nagasaki | |
nara | |
niigata | |
okayama | |
okinawa | |
ooita | |
osaka | |
saga | |
saitama | |
shiga | |
shimane | |
shizuoka | |
tochigi | |
tokushima | |
tokyo | |
tottori | |
toyama | |
wakayama | |
yamagata | |
yamaguchi | |
yamanashi | |
yokohama | |
EOK | |
ks = keywords.each_line.map(&:chomp) | |
EM.run { | |
username = 'tw_user' | |
password = 'tw_password' | |
buffer = "" | |
lasttime = nil | |
ids = {} | |
api = 'http://stream.twitter.com/1/statuses/sample.json' | |
http = EventMachine::HttpRequest.new(api).post({ | |
:head => { 'Authorization' => [ username, password ] }, | |
}) | |
http.callback { | |
unless http.response_header.status == 200 | |
puts "Call failed with response code #{http.response_header.status}" | |
end | |
} | |
http.stream do |chunk| | |
buffer += chunk | |
while line = buffer.slice!(/.+\r\n/) | |
tweet = JSON.parse(line) | |
u = tweet['user'] | |
next unless u | |
t = tweet['text'].downcase | |
keywords = ['震', '揺れ', 'ゆれ', '津波', 'earthquake'] | |
next unless keywords.any?{|k| t.include?(k)} | |
now = Time.now.strftime("%H:%M") | |
if lasttime != now | |
lasttime = now | |
puts | |
bigtext = ids.values.flatten.join.downcase | |
result = ks.map do |k| | |
c = bigtext.gsub(k).to_a.size | |
next if c == 0 | |
"%s(%s)" % [k, '*' * c] | |
end | |
res = result.compact.join(', ') | |
if ids.size > 10 | |
puts res unless res.empty? | |
Notify.notify '*' * ids.size, res | |
end | |
ids = {} | |
print now | |
print ' ' | |
end | |
loc = u['location'] | |
tz = u['time_zone'] | |
id = u['screen_name'] | |
next if ids.key?(id) | |
ids[id] = [loc, t] | |
print '*' | |
STDOUT.flush | |
end | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ちょっと見た目変更した。