Created
November 15, 2011 04:10
-
-
Save kokudori/1366106 to your computer and use it in GitHub Desktop.
Rubyでニコ生のコメントを取得
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
# -*- encoding: utf-8 -*- | |
require 'net/http' | |
require 'rubygems' | |
require 'nokogiri' | |
class Comment | |
class << self | |
attr_accessor :cookie | |
end | |
#下の値はブラウザのクッキーから取得してください | |
self.cookie = 'user_session=user_session_xxxxxxxx_xxxxxxxxxxxxxxx;' | |
def initialize(live_id) | |
@live_id = live_id | |
@status = get_playerstatus() | |
end | |
def get_playerstatus | |
Net::HTTP.start 'watch.live.nicovideo.jp' do |http| | |
response = http.get "/api/getplayerstatus?v=#{@live_id}", | |
{'Cookie' => Comment.cookie, 'User-Agent' => 'Ruby/Nicolive'} | |
xml = Nokogiri(response.body) | |
if not xml.elements.attribute('status').value() == 'ok' | |
raise '放送は既に終了しています' | |
end | |
{ | |
address: (xml/:addr).inner_text(), | |
port: (xml/:port).inner_text(), | |
thread: (xml/:thread).inner_text() | |
} | |
end | |
end | |
def start() | |
TCPSocket.open(@status[:address], @status[:port].to_i) do |sock| | |
ticket = "<thread thread=\"#{@status[:thread]}\" version=\"20061206\" res_from=\"-1000\"/>\0" | |
sock.write(ticket) | |
p ticket | |
while true | |
stream = sock.gets("\0") | |
puts stream | |
end | |
end | |
end | |
end | |
#みたい放送の放送IDを入れる(coxxxxxxx形式でもOK) | |
comment = Comment.new 'lvxxxxxxxx' | |
comment.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment