Class: Niconico::Live

Inherits:
Object
  • Object
show all
Defined in:
lib/niconico/live.rb,
lib/niconico/live/api.rb

Defined Under Namespace

Classes: API, AcceptingReservationFailed, ReservationNotAccepted, ReservationOutdated, TicketRetrievingFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, live_id) ⇒ Live

Returns a new instance of Live.



31
32
33
34
35
36
37
38
# File 'lib/niconico/live.rb', line 31

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

  get()
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



40
41
42
# File 'lib/niconico/live.rb', line 40

def id
  @id
end

#liveObject (readonly)

Returns the value of attribute live.



40
41
42
# File 'lib/niconico/live.rb', line 40

def live
  @live
end

#public_keyObject



43
44
45
# File 'lib/niconico/live.rb', line 43

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

#ticketObject (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_keyObject



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.expand_path(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_reservationObject



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

#channelObject



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

def channel
  get.live[:channel]
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  status == :closed
end

#descriptionObject



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

Returns:

  • (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

#inspectObject



81
82
83
# File 'lib/niconico/live.rb', line 81

def inspect
  "#<Niconico::Live: #{id}, #{title}>"
end

#on_air?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/niconico/live.rb', line 109

def on_air?
  status == :on_air
end

#opens_atObject



93
94
95
# File 'lib/niconico/live.rb', line 93

def opens_at
  get.live[:opens_at]
end

#premium?Boolean

Returns:

  • (Boolean)


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

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

#quesheetObject



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

def quesheet
  seat[:quesheet]
end

#reservation_accepted?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/niconico/live.rb', line 125

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

#reservation_available?Boolean

Returns:

  • (Boolean)


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

def reservation_available?
  reservation_unaccepted? || reservation_accepted?
end

#reservation_expires_atObject



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

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

#reservation_outdated?Boolean

Returns:

  • (Boolean)


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

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

#reservation_unaccepted?Boolean

Returns:

  • (Boolean)


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

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

#reserved?Boolean

Returns:

  • (Boolean)


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

def reserved?
  get.live.key? :reservation
end

#rtmp_urlObject



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.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)


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_atObject



97
98
99
# File 'lib/niconico/live.rb', line 97

def starts_at
  get.live[:starts_at]
end

#statusObject



101
102
103
# File 'lib/niconico/live.rb', line 101

def status
  get.live[:status]
end

#titleObject



85
86
87
# File 'lib/niconico/live.rb', line 85

def title
  get.live[:title]
end