Class: Niconico::Live::API
- Inherits:
-
Object
- Object
- Niconico::Live::API
- Defined in:
- lib/niconico/live/api.rb
Defined Under Namespace
Classes: NoPublicKeyProvided
Constant Summary collapse
- URL_GETPLAYERSTATUS =
'http://ow.live.nicovideo.jp/api/getplayerstatus'.freeze
- URL_WATCHINGRESERVATION_LIST =
'http://live.nicovideo.jp/api/watchingreservation?mode=list'
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
Instance Method Summary collapse
- #accept_watching_reservation(id_) ⇒ Object
- #decrypt_encrypted_player_status(body, public_key) ⇒ Object
- #get(id) ⇒ Object
- #get_player_status(id, public_key = nil) ⇒ Object
- #heartbeat ⇒ Object
-
#initialize(agent) ⇒ API
constructor
A new instance of API.
- #watching_reservations ⇒ Object
Constructor Details
#initialize(agent) ⇒ API
Returns a new instance of API.
13 14 15 |
# File 'lib/niconico/live/api.rb', line 13 def initialize(agent) @agent = agent end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
17 18 19 |
# File 'lib/niconico/live/api.rb', line 17 def agent @agent end |
Instance Method Details
#accept_watching_reservation(id_) ⇒ Object
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/niconico/live/api.rb', line 163 def accept_watching_reservation(id_) id = normalize_id(id_, with_lv: false) page = agent.get("http://live.nicovideo.jp/api/watchingreservation?mode=confirm_watch_my&vid=#{id}&next_url&analytic") token = page.at('#reserve img')['onclick'].scan(/'(.+?)'/)[1][0] page = agent.post("http://live.nicovideo.jp/api/watchingreservation", accept: 'true', mode: 'use', vid: id, token: token) page.at('nicolive_video_response')['status'] == 'ok' end |
#decrypt_encrypted_player_status(body, public_key) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/niconico/live/api.rb', line 174 def decrypt_encrypted_player_status(body, public_key) unless public_key raise NoPublicKeyProvided, 'You should provide proper public key to decrypt ' \ 'encrypted player status' end lines = body.lines pubkey = OpenSSL::PKey::RSA.new(public_key) encrypted_shared_key = lines[1].unpack('m*')[0] shared_key_raw = pubkey.public_decrypt(encrypted_shared_key) shared_key = shared_key_raw.unpack('L>*')[0].to_s cipher = OpenSSL::Cipher.new('bf-ecb').decrypt cipher.padding = 0 cipher.key_len = shared_key.size cipher.key = shared_key encrypted_body = lines[2].unpack('m*')[0] body = cipher.update(encrypted_body) + cipher.final body.force_encoding('utf-8') end |
#get(id) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/niconico/live/api.rb', line 19 def get(id) id = normalize_id(id) page = agent.get("http://live.nicovideo.jp/gate/#{id}") comment_area = page.at("#comment_area#{id}").inner_text result = { title: page.at('h2 span[itemprop="name"]').inner_text, id: id, description: page.at('.stream_description .text_area').inner_html, } kaijo = page.search('.kaijo strong').map(&:inner_text) result[:opens_at] = Time.parse("#{kaijo[0]} #{kaijo[1]} +0900") result[:starts_at] = Time.parse("#{kaijo[0]} #{kaijo[2]} +0900") result[:status] = :scheduled if comment_area.include?('開場まで、あと') result[:status] = :on_air if comment_area.include?('現在放送中') = comment_area.match(/この番組は(.+?)に終了いたしました。/) if result[:status] = :closed result[:closed_at] = Time.parse("#{[1]} +0900") end = comment_area.match(/使用期限: (.+?)まで/) if result[:reservation] = {} result[:reservation][:expires_at] = Time.parse("#{[1]} +0900") if comment_area.include?('視聴権を使用し、タイムシフト視聴を行いますか?') result[:reservation][:status] = :reserved result[:reservation][:available] = true elsif comment_area.include?('本番組は、タイムシフト視聴を行う事が可能です。') result[:reservation][:status] = :accepted result[:reservation][:available] = true elsif comment_area.include?('タイムシフト視聴をこれ以上行う事は出来ません。') || comment_area.include?('視聴権の利用期限が過ぎています。') result[:reservation][:status] = :outdated end end channel = page.at('div.chan') if channel result[:channel] = { name: channel.at('.shosai a').inner_text, id: channel.at('.shosai a')['href'].split('/').last, link: channel.at('.shosai a')['href'], } end result end |
#get_player_status(id, public_key = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/niconico/live/api.rb', line 76 def get_player_status(id, public_key = nil) id = normalize_id(id) page = agent.get("http://ow.live.nicovideo.jp/api/getplayerstatus?locale=GLOBAL&lang=ja%2Djp&v=#{id}&seat%5Flocale=JP") if page.body[0] == 'c' # encrypted page = Nokogiri::XML(decrypt_encrypted_player_status(page.body, public_key)) end status = page.at('getplayerstatus') if status['status'] == 'fail' error = page.at('error code').inner_text case error when 'notlogin' return {error: :not_logged_in} when 'comingsoon' return {error: :not_yet_started} when 'closed' return {error: :closed} when 'require_accept_print_timeshift_ticket' return {error: :reservation_not_accepted} when 'timeshift_ticket_expire' return {error: :reservation_expired} when 'noauth' return {error: :archive_closed} when 'notfound' return {error: :not_found} else return {error: error} end end result = {} # Strings %w(id title description provider_type owner_name bourbon_url full_video kickout_video).each do |key| item = status.at(key) result[key.to_sym] = item.inner_text if item end # Integers %w(watch_count comment_count owner_id watch_count comment_count).each do |key| item = status.at(key) result[key.to_sym] = item.inner_text.to_i if item end # Flags %w(is_premium is_reserved is_owner international is_rerun_stream is_archiveplayserver archive allow_netduetto is_nonarchive_timeshift_enabled is_timeshift_reserved).each do |key| item = status.at(key) result[key.sub(/^is_/,'').concat('?').to_sym] = item.inner_text == '1' if item end # Datetimes %w(base_time open_time start_time end_time).each do |key| item = status.at(key) result[key.to_sym] = Time.at(item.inner_text.to_i) if item end rtmp = status.at('rtmp') result[:rtmp] = { url: rtmp.at('url').inner_text, ticket: rtmp.at('ticket').inner_text, } ms = status.at('ms') result[:ms] = { address: ms.at('addr').inner_text, port: ms.at('port').inner_text.to_i, thread: ms.at('thread').inner_text, } quesheet = status.search('quesheet que') result[:quesheet] = quesheet.map do |que| {vpos: que['vpos'].to_i, mail: que['mail'], name: que['name'], body: que.inner_text} end result end |
#heartbeat ⇒ Object
72 73 74 |
# File 'lib/niconico/live/api.rb', line 72 def heartbeat raise NotImplementedError end |
#watching_reservations ⇒ Object
158 159 160 161 |
# File 'lib/niconico/live/api.rb', line 158 def watching_reservations page = agent.get(URL_WATCHINGRESERVATION_LIST) page.search('vid').map(&:inner_text).map{ |_| normalize_id(_) } end |