Skip to content

Instantly share code, notes, and snippets.

@snaka
Created January 23, 2010 07:27
Show Gist options
  • Save snaka/284486 to your computer and use it in GitHub Desktop.
Save snaka/284486 to your computer and use it in GitHub Desktop.
ニコ生アラートを受信するスクリプト
require 'net/http'
require 'rubygems'
require 'nokogiri'
KCODE = 'UTF-8'
# アラートを通知してくれるコメントサーバーの情報を取得
response = Net::HTTP.get(URI('http://live.nicovideo.jp/api/getalertinfo'))
xml = Nokogiri(response)
address = (xml/'addr').inner_text()
port = (xml/'port').inner_text()
thread = (xml/'thread').inner_text()
# コメントサーバーからストリームを受信
TCPSocket.open(address, port.to_i) {|sock|
sock.write("<thread thread=\"#{thread}\" version=\"20061206\" res_from=\"-1\"/>\0")
while true
stream = sock.gets("\0")
next if stream =~ /^<thread/
live_id, commu_id, owner_id = stream.match(/<chat[^>]+>(.*)<\/chat>/)[1].split(',')
# 以下ニコニコ側に負荷かけそうなのでコメントアウトしとく...
# 受信したアラート情報をもとに放送情報を取得
#response = Net::HTTP.get(URI("http://live.nicovideo.jp/api/getstreaminfo/lv#{live_id}"))
#xml = Nokogiri(response)
#title = (xml/'streaminfo/title').inner_text()
#description = (xml/'streaminfo/description').inner_text()
# 垂れ流す
#puts "#{'-'*60}\n#{title}\n#{description}"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment