Skip to content

Instantly share code, notes, and snippets.

@motikan2010
Forked from kokudori/gist:1366106
Created April 3, 2014 04: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 motikan2010/9948122 to your computer and use it in GitHub Desktop.
Save motikan2010/9948122 to your computer and use it in GitHub Desktop.
# -*- 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