Class: Niconico::Live
- Inherits:
-
Object
- Object
- Niconico::Live
- Defined in:
- lib/niconico/live.rb,
lib/niconico/live/api.rb
Defined Under Namespace
Classes: API, AcceptingReservationFailed, ReservationNotAccepted, ReservationOutdated, TicketRetrievingFailed
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#live ⇒ Object
readonly
Returns the value of attribute live.
- #public_key ⇒ Object
-
#ticket ⇒ Object
readonly
Returns the value of attribute ticket.
Class Method Summary collapse
Instance Method Summary collapse
- #accept_reservation ⇒ Object
- #channel ⇒ Object
- #closed? ⇒ Boolean
- #description ⇒ Object
- #execute_rtmpdump(file_base, ignore_failure = false) ⇒ Object
- #fetched? ⇒ Boolean
- #get(force = false) ⇒ Object
-
#initialize(parent, live_id) ⇒ Live
constructor
A new instance of Live.
- #inspect ⇒ Object
- #on_air? ⇒ Boolean
- #opens_at ⇒ Object
- #premium? ⇒ Boolean
- #quesheet ⇒ Object
- #reservation_accepted? ⇒ Boolean
- #reservation_available? ⇒ Boolean
- #reservation_expires_at ⇒ Object
- #reservation_outdated? ⇒ Boolean
- #reservation_unaccepted? ⇒ Boolean
- #reserved? ⇒ Boolean
- #rtmp_url ⇒ Object
- #rtmpdump_commands(file_base) ⇒ Object
- #scheduled? ⇒ Boolean
- #seat(force = false) ⇒ Object
- #starts_at ⇒ Object
- #status ⇒ Object
- #title ⇒ Object
Constructor Details
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
40 41 42 |
# File 'lib/niconico/live.rb', line 40 def id @id end |
#live ⇒ Object (readonly)
Returns the value of attribute live.
40 41 42 |
# File 'lib/niconico/live.rb', line 40 def live @live end |
#public_key ⇒ Object
43 44 45 |
# File 'lib/niconico/live.rb', line 43 def public_key @public_key || self.class.public_key end |
#ticket ⇒ Object (readonly)
Returns the value of attribute ticket.
40 41 42 |
# File 'lib/niconico/live.rb', line 40 def ticket @ticket end |
Class Method Details
.public_key ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/niconico/live.rb', line 16 def public_key @public_key ||= begin if ENV["NICONICO_LIVE_PUBLIC_KEY"] File.read(File.(ENV["NICONICO_LIVE_PUBLIC_KEY"])) else nil end end end |
.public_key=(other) ⇒ Object
26 27 28 |
# File 'lib/niconico/live.rb', line 26 def public_key=(other) @public_key = other end |
Instance Method Details
#accept_reservation ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/niconico/live.rb', line 69 def accept_reservation return self if reservation_accepted? raise ReservationOutdated if reservation_outdated? result = @client.accept_watching_reservation(self.id) raise AcceptingReservationFailed unless result get(:reload) self end |
#channel ⇒ Object
141 142 143 |
# File 'lib/niconico/live.rb', line 141 def channel get.live[:channel] end |
#closed? ⇒ Boolean
113 114 115 |
# File 'lib/niconico/live.rb', line 113 def closed? status == :closed end |
#description ⇒ Object
89 90 91 |
# File 'lib/niconico/live.rb', line 89 def description get.live[:description] end |
#execute_rtmpdump(file_base, ignore_failure = false) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/niconico/live.rb', line 161 def execute_rtmpdump(file_base, ignore_failure = false) rtmpdump_commands(file_base).map do |cmd| system *cmd retval = $? raise RtmpdumpFailed, "#{cmd.inspect} failed" if !retval.success? && !ignore_failure [cmd, retval] end end |
#fetched? ⇒ Boolean
47 48 49 |
# File 'lib/niconico/live.rb', line 47 def fetched? !!@fetched end |
#get(force = false) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/niconico/live.rb', line 51 def get(force=false) return self if @fetched && !force @live = @client.get(@live_id) @fetched = true self end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/niconico/live.rb', line 81 def inspect "#<Niconico::Live: #{id}, #{title}>" end |
#on_air? ⇒ Boolean
109 110 111 |
# File 'lib/niconico/live.rb', line 109 def on_air? status == :on_air end |
#opens_at ⇒ Object
93 94 95 |
# File 'lib/niconico/live.rb', line 93 def opens_at get.live[:opens_at] end |
#premium? ⇒ Boolean
145 146 147 |
# File 'lib/niconico/live.rb', line 145 def premium? !!seat[:premium?] end |
#quesheet ⇒ Object
157 158 159 |
# File 'lib/niconico/live.rb', line 157 def quesheet seat[:quesheet] end |
#reservation_accepted? ⇒ Boolean
125 126 127 |
# File 'lib/niconico/live.rb', line 125 def reservation_accepted? reserved? && live[:reservation][:status] == :accepted end |
#reservation_available? ⇒ Boolean
133 134 135 |
# File 'lib/niconico/live.rb', line 133 def reservation_available? reservation_unaccepted? || reservation_accepted? end |
#reservation_expires_at ⇒ Object
137 138 139 |
# File 'lib/niconico/live.rb', line 137 def reservation_expires_at reserved? ? live[:reservation][:expires_at] : nil end |
#reservation_outdated? ⇒ Boolean
129 130 131 |
# File 'lib/niconico/live.rb', line 129 def reservation_outdated? reserved? && live[:reservation][:status] == :outdated end |
#reservation_unaccepted? ⇒ Boolean
121 122 123 |
# File 'lib/niconico/live.rb', line 121 def reservation_unaccepted? reserved? && live[:reservation][:status] == :reserved end |
#reserved? ⇒ Boolean
117 118 119 |
# File 'lib/niconico/live.rb', line 117 def reserved? get.live.key? :reservation end |
#rtmp_url ⇒ Object
149 150 151 |
# File 'lib/niconico/live.rb', line 149 def rtmp_url seat[:rtmp][:url] end |
#rtmpdump_commands(file_base) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/niconico/live.rb', line 170 def rtmpdump_commands(file_base) file_base = File.(file_base) publishes = quesheet.select{ |_| /^\/publish / =~ _[:body] }.map do |publish| publish[:body].split(/ /).tap(&:shift) end plays = quesheet.select{ |_| /^\/play / =~ _[:body] } plays.flat_map.with_index do |play, i| cases = play[:body].sub(/^case:/,'').split(/ /)[1].split(/,/) publish_id = nil publish_id = cases.find { |_| _.start_with?('premium:') } if premium? publish_id ||= cases.find { |_| _.start_with?('default:') } publish_id ||= cases[0] publish_id = publish_id.split(/:/).last contents = publishes.select{ |_| _[0] == publish_id } contents.map.with_index do |content, j| content = content[1] rtmp = "#{self.rtmp_url}/mp4:#{content}" seq = 0 begin file = "#{file_base}.#{i}.#{j}.#{seq}.flv" seq += 1 end while File.exist?(file) ['rtmpdump', '-V', '-o', file, '-r', rtmp, '-C', "S:#{ticket}", '--playpath', "mp4:#{content}", '--app', URI.parse(self.rtmp_url).path.sub(/^\//,'') ] end end end |
#scheduled? ⇒ Boolean
105 106 107 |
# File 'lib/niconico/live.rb', line 105 def scheduled? status == :scheduled end |
#seat(force = false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/niconico/live.rb', line 58 def seat(force=false) return @seat if @seat && !force raise ReservationNotAccepted if reserved? && !reservation_accepted? @seat = @client.get_player_status(self.id, self.public_key) raise TicketRetrievingFailed, @seat[:error] if @seat[:error] @seat end |
#starts_at ⇒ Object
97 98 99 |
# File 'lib/niconico/live.rb', line 97 def starts_at get.live[:starts_at] end |
#status ⇒ Object
101 102 103 |
# File 'lib/niconico/live.rb', line 101 def status get.live[:status] end |
#title ⇒ Object
85 86 87 |
# File 'lib/niconico/live.rb', line 85 def title get.live[:title] end |