Skip to content

Instantly share code, notes, and snippets.

@ohac
Forked from zh/em_stream.rb
Created March 18, 2011 10:07
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 ohac/875845 to your computer and use it in GitHub Desktop.
Save ohac/875845 to your computer and use it in GitHub Desktop.
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
}
@ohac
Copy link
Author

ohac commented Mar 18, 2011

↓こんな感じに表示されます。

18:01
千葉県広島市

18:02
しろくろ冬朧埼玉県草加市神奈川県川崎市中原区新丸子大阪市港区めしつか in ハピネス
ヒル@ngo, HAN, BKK, or anywhere

18:03
大分県東京都江東区東京心はいつも江古田

18:04
神奈川神奈川県兵庫のどっか柏エロマンガ島

18:05
埋れ木家の犬小屋神奈川県夢の国で潤っている県&本州の孤島←埼玉

18:06
日本で一番海から遠い地点の付近ブルマが好きです

18:07
東京東京都 Tokyo,JAPAN大阪市西区赤羽 が好きですTokyo,Japan東京 サンライズがある区

@ohac
Copy link
Author

ohac commented Mar 18, 2011

ちょっと見た目変更した。

18:32 ********
18:33 *******
18:34 **********
18:35 ****
18:36 ************
宮崎(*), 京都(**), 埼玉(*), 山口(*), 大阪(*), 東京(***), 福島(*), niigata(*), saga(*)
18:37 *****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment