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.
Class Method Summary collapse
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.
14 15 16 |
# File 'lib/niconico/live/api.rb', line 14 def initialize(agent) @agent = agent end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
18 19 20 |
# File 'lib/niconico/live/api.rb', line 18 def agent @agent end |
Class Method Details
.parse_reservation_message(message) ⇒ Object
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 |
# File 'lib/niconico/live/api.rb', line 20 def self.() = .match(/(?:使用|利用)?期限: (.+?)まで|(?:期限中、何度でも視聴できます|視聴権(?:利用|使用)期限が切れています|タイムシフト利用期間は終了しました)\s*\[(.+?)まで\]/) = .match(/\[視聴期限未定\]/) case when .match(/予約中\s*\[/) {status: :reserved, available: false} when || {}.tap do |reservation| if reservation[:expires_at] = Time.parse("#{[1] || [2]} +0900") end if .include?('視聴権を使用し、タイムシフト視聴を行いますか?') reservation[:status] = :reserved reservation[:available] = true elsif .include?('本番組は、タイムシフト視聴を行う事が可能です。') \ || .include?('期限中、何度でも視聴できます') reservation[:status] = :accepted reservation[:available] = true elsif .include?('タイムシフト視聴をこれ以上行う事は出来ません。') \ || .include?('視聴権の利用期限が過ぎています。') \ || .include?('視聴権利用期限が切れています') \ || .include?('視聴権使用期限が切れています') \ || .include?('タイムシフト利用期間は終了しました') \ || .include?('アーカイブ公開期限は終了しました。') reservation[:status] = :outdated reservation[:available] = false end end else nil end end |
Instance Method Details
#accept_watching_reservation(id_) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/niconico/live/api.rb', line 198 def accept_watching_reservation(id_) id = Util::normalize_id(id_, with_lv: false) page = agent.get("http://live.nicovideo.jp/api/watchingreservation?mode=watch_num&vid=#{id}&next_url&analytic") token = Util::fetch_token(@agent) page = agent.post("http://live.nicovideo.jp/api/watchingreservation", mode: 'auto_register', vid: id, token: token, '_' => '') token = Util::fetch_token(@agent) page = agent.post("http://live.nicovideo.jp/api/watchingreservation", accept: 'true', mode: 'use', vid: id, token: token) end |
#decrypt_encrypted_player_status(body, public_key) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/niconico/live/api.rb', line 212 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/niconico/live/api.rb', line 55 def get(id) id = Util::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 result[:reservation] = self.class.(comment_area) if !result[:reservation] && page.search(".watching_reservation_reserved").any? { |_| _['onclick'].include?(id) } result[:reservation] = {status: :reserved, available: false} 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
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/niconico/live/api.rb', line 101 def get_player_status(id, public_key = nil) id = Util::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, } stream = status.at('stream') result[:stream] = { id: stream.at('id').inner_text, default_community: stream.at('default_community').inner_text, base_time: stream.at('base_time').inner_text.to_i, open_time: stream.at('open_time').inner_text.to_i, watch_count: stream.at('watch_count').inner_text.to_i, comment_count: stream.at('comment_count').inner_text.to_i } 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
97 98 99 |
# File 'lib/niconico/live/api.rb', line 97 def heartbeat raise NotImplementedError end |
#watching_reservations ⇒ Object
193 194 195 196 |
# File 'lib/niconico/live/api.rb', line 193 def watching_reservations page = agent.get(URL_WATCHINGRESERVATION_LIST) page.search('vid').map(&:inner_text).map{ |_| Util::normalize_id(_) } end |