Created
July 6, 2014 13:30
-
-
Save ixixi/19119c12be887e646d76 to your computer and use it in GitHub Desktop.
in_nicolive_comment.rb
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' | |
require 'openssl' | |
require 'fluent-logger' | |
require 'active_support/core_ext' | |
$fluent_logger = Fluent::Logger::FluentLogger.new(nil, :host=>'localhost', :port=>24224) | |
$TAG = "nicolive.comment" | |
def login_nicovideo(mail, pass) | |
host = 'secure.nicovideo.jp' | |
path = '/secure/login?site=niconico' | |
body = "mail=#{mail}&password=#{pass}" | |
https = Net::HTTP.new(host, 443) | |
https.use_ssl = true | |
https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
response = https.start { |https| | |
https.post(path, body) | |
} | |
cookie = '' | |
response['set-cookie'].split('; ').each do |st| | |
if idx=st.index('user_session_') | |
cookie = "user_session=#{st[idx..-1]}" | |
break | |
end | |
end | |
return cookie | |
end | |
class Comment | |
class << self | |
attr_accessor :cookie | |
end | |
user = `cat ./nicouser`.chomp | |
pass = `cat ./nicopass`.chomp | |
cook = login_nicovideo(user,pass) | |
self.cookie = "#{cook};" | |
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 'closed...' | |
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=\"0\"/>\0" | |
sock.write(ticket) | |
p ticket | |
while true | |
xml_event = sock.gets("\0").to_s.force_encoding(Encoding::UTF_8) | |
xml_event.delete!("\0") | |
doc = ActiveSupport::XmlMini.parse(xml_event) | |
if doc.key?('chat') | |
doc['chat']['live_id'] = @live_id | |
doc['chat']['message'] = doc['chat']['__content__'] | |
doc['chat'].delete('__content__') | |
doc['chat']['post_time'] = (doc['chat']['date']+doc['chat']['date_usec']).to_i/1000 | |
puts doc['chat'] | |
$fluent_logger.post($TAG, doc['chat']) | |
end | |
end | |
end | |
end | |
end | |
comment = Comment.new ARGV[0] | |
comment.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment