Class: Niconico::Live

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/niconico/live.rb,
lib/niconico/live/api.rb,
lib/niconico/live/util.rb,
lib/niconico/live/client.rb,
lib/niconico/live/mypage.rb,
lib/niconico/live/client/search_result.rb,
lib/niconico/live/client/search_filters.rb

Defined Under Namespace

Classes: API, AcceptingReservationFailed, Client, Mypage, ReservationNotAccepted, ReservationOutdated, TicketRetrievingFailed, Util

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deferrable

included

Constructor Details

#initialize(parent, live_id, preload = nil) ⇒ Live

Returns a new instance of Live.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/niconico/live.rb', line 34

def initialize(parent, live_id, preload = nil)
  @parent = parent
  @agent = parent.agent
  @id = @live_id = live_id
  @client = Niconico::Live::API.new(@agent)

  if preload
    preload_deffered_values(preload)
  else
    get
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



47
48
49
# File 'lib/niconico/live.rb', line 47

def id
  @id
end

#liveObject (readonly)

Returns the value of attribute live.



47
48
49
# File 'lib/niconico/live.rb', line 47

def live
  @live
end

#public_keyObject



50
51
52
# File 'lib/niconico/live.rb', line 50

def public_key
  @public_key || self.class.public_key
end

#ticketObject (readonly)

Returns the value of attribute ticket.



47
48
49
# File 'lib/niconico/live.rb', line 47

def ticket
  @ticket
end

Class Method Details

.public_keyObject



19
20
21
22
23
24
25
26
27
# File 'lib/niconico/live.rb', line 19

def public_key
  @public_key ||= begin
    if ENV["NICONICO_LIVE_PUBLIC_KEY"]
      File.read(File.expand_path(ENV["NICONICO_LIVE_PUBLIC_KEY"]))
    else
      nil
    end
  end
end

.public_key=(other) ⇒ Object



29
30
31
# File 'lib/niconico/live.rb', line 29

def public_key=(other)
  @public_key = other
end

Instance Method Details

#accept_reservationObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/niconico/live.rb', line 76

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

#closed?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/niconico/live.rb', line 120

def closed?
  status == :closed
end

#execute_rtmpdump(file_base, ignore_failure = false) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/niconico/live.rb', line 172

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/niconico/live.rb', line 54

def fetched?
  !!@fetched
end

#get(force = false) ⇒ Object



58
59
60
61
62
63
# File 'lib/niconico/live.rb', line 58

def get(force=false)
  return self if @fetched && !force
  @live = @client.get(@live_id)
  @fetched = true
  self
end

#inspectObject



88
89
90
# File 'lib/niconico/live.rb', line 88

def inspect
  "#<Niconico::Live: #{id}, #{title}#{fetched? ? '': ' (deferred)'}>"
end

#on_air?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/niconico/live.rb', line 116

def on_air?
  status == :on_air
end

#premium?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/niconico/live.rb', line 156

def premium?
  !!seat[:premium?]
end

#quesheetObject



168
169
170
# File 'lib/niconico/live.rb', line 168

def quesheet
  seat[:quesheet]
end

#reservation_accepted?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/niconico/live.rb', line 140

def reservation_accepted?
  reserved? && reservation[:status] == :accepted
end

#reservation_available?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/niconico/live.rb', line 132

def reservation_available?
  reserved? && reservation[:available]
end

#reservation_expires_atObject



148
149
150
# File 'lib/niconico/live.rb', line 148

def reservation_expires_at
  reserved? ? reservation[:expires_at] : nil
end

#reservation_outdated?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/niconico/live.rb', line 144

def reservation_outdated?
  reserved? && reservation[:status] == :outdated
end

#reservation_unaccepted?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/niconico/live.rb', line 136

def reservation_unaccepted?
  reservation_available? && reservation[:status] == :reserved
end

#reserved?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/niconico/live.rb', line 128

def reserved?
  !!reservation
end

#rtmp_urlObject



160
161
162
# File 'lib/niconico/live.rb', line 160

def rtmp_url
  seat[:rtmp][:url]
end

#rtmpdump_commands(file_base) ⇒ Object



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
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/niconico/live.rb', line 181

def rtmpdump_commands(file_base)
  file_base = File.expand_path(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

Returns:

  • (Boolean)


112
113
114
# File 'lib/niconico/live.rb', line 112

def scheduled?
  status == :scheduled
end

#seat(force = false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/niconico/live.rb', line 65

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