Class: Twitch::V2::Streams

Inherits:
Object
  • Object
show all
Defined in:
lib/kappa/stream.rb

Overview

Query class for finding featured streams or streams meeting certain criteria.

See Also:

Instance Method Summary collapse

Instance Method Details

#all(options = {}) {|stream| ... } ⇒ Array<Stream>?

Get all currently live streams sorted by descending viewer count.

Examples:

Twitch.streams.all
Twitch.streams.all(:offset => 100, :limit => 10)
Twitch.streams.all do |stream|
  next if stream.viewer_count < 1000
  puts stream.url
end

Parameters:

  • options (Hash) (defaults to: {})

    Limit criteria.

Options Hash (options):

  • :limit (Fixnum) — default: nil

    Limit on the number of results returned.

  • :offset (Fixnum) — default: 0

    Offset into the result set to begin enumeration.

Yields:

  • Optional. If a block is given, each stream is yielded.

Yield Parameters:

  • stream (Stream)

    Current stream.

Returns:

  • (Array<Stream>)

    Currently live streams, sorted by descending viewer count, if no block is given.

  • (nil)

    If a block is given.

See Also:



146
147
148
149
150
151
152
153
154
155
# File 'lib/kappa/stream.rb', line 146

def all(options = {}, &block)
  return @query.connection.accumulate(
    :path => 'streams',
    :json => 'streams',
    :create => -> hash { Stream.new(hash, @query) },
    :limit => options[:limit],
    :offset => options[:offset],
    &block
  )
end
Note:

There is no guarantee of how many streams are featured at any given time.

Get the currently featured (promoted) streams. This includes the streams shown on the Twitch homepage.

Examples:

Twitch.streams.featured
Twitch.streams.featured(:limit => 5)
Twitch.streams.featured do |stream|
  next if stream.viewer_count < 1000
  puts stream.url
end

Parameters:

  • options (Hash) (defaults to: {})

    Filter criteria.

Options Hash (options):

  • :hls (Boolean) — default: nil

    If true, limit the streams to those using HLS (HTTP Live Streaming). If false or nil, do not limit.

  • :limit (Fixnum) — default: nil

    Limit on the number of results returned.

  • :offset (Fixnum) — default: 0

    Offset into the result set to begin enumeration.

Yields:

  • Optional. If a block is given, each featured stream is yielded.

Yield Parameters:

  • stream (Stream)

    Current stream.

Returns:

  • (Array<Stream>)

    Featured streams, if no block is given.

  • (nil)

    If a block is given.

See Also:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/kappa/stream.rb', line 258

def featured(options = {}, &block)
  params = {}

  if options[:hls]
    params[:hls] = true
  end

  return @query.connection.accumulate(
    :path => 'streams/featured',
    :params => params,
    :json => 'featured',
    :sub_json => 'stream',
    :create => -> hash { Stream.new(hash, @query) },
    :limit => options[:limit],
    :offset => options[:offset],
    &block
  )
end

#find(options) {|stream| ... } ⇒ Array<Stream>?

Get streams for a specific game, for a set of channels, or by other criteria, sorted by descending viewer count.

Examples:

Twitch.streams.find(:game => 'League of Legends', :limit => 50)
Twitch.streams.find(:channel => ['fgtvlive', 'incontroltv', 'destiny'])
Twitch.streams.find(:game => 'Diablo III', :channel => ['nl_kripp', 'protech'])
Twitch.streams.find(:game => 'League of Legends') do |stream|
  next if stream.viewer_count < 1000
  puts stream.url
end

Parameters:

  • options (Hash)

    Search criteria.

Options Hash (options):

  • :game (String, Game, #name)

    Only return streams currently streaming the specified game.

  • :channel (Array<String, Channel, #name>)

    Only return streams for these channels. If a channel is not currently streaming, it is omitted. You must specify an array of channels or channel names. If you want to find the stream for a single channel, see #get.

  • :embeddable (Boolean) — default: nil

    If true, limit the streams to those that can be embedded. If false or nil, do not limit.

  • :hls (Boolean) — default: nil

    If true, limit the streams to those using HLS (HTTP Live Streaming). If false or nil, do not limit.

  • :limit (Fixnum) — default: nil

    Limit on the number of results returned.

  • :offset (Fixnum) — default: 0

    Offset into the result set to begin enumeration.

Yields:

  • Optional. If a block is given, each stream found is yielded.

Yield Parameters:

  • stream (Stream)

    Current stream.

Returns:

  • (Array<Stream>)

    Streams matching the specified criteria, sorted by descending viewer count, if no block is given.

  • (nil)

    If a block is given.

Raises:

  • (ArgumentError)

    If options does not specify a search criteria (:game, :channel, :embeddable, or :hls).

  • (ArgumentError)

    If :channel is not an array.

See Also:



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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/kappa/stream.rb', line 187

def find(options, &block)
  check = options.dup
  check.delete(:limit)
  check.delete(:offset)
  raise ArgumentError, 'options' if check.empty?

  params = {}

  channels = options[:channel]
  if channels
    if !channels.respond_to?(:map)
      raise ArgumentError, ':channel'
    end

    params[:channel] = channels.map { |channel|
      if channel.respond_to?(:name)
        channel.name
      else
        channel.to_s
      end
    }.join(',')
  end

  game = options[:game]
  if game
    if game.respond_to?(:name)
      params[:game] = game.name
    else
      params[:game] = game.to_s
    end
  end

  if options[:hls]
    params[:hls] = true
  end

  if options[:embeddable]
    params[:embeddable] = true
  end

  return @query.connection.accumulate(
    :path => 'streams',
    :params => params,
    :json => 'streams',
    :create => -> hash { Stream.new(hash, @query) },
    :limit => options[:limit],
    :offset => options[:offset],
    &block
  )
end

#get(stream_name) ⇒ Stream?

Get a live stream by name.

Examples:

s = Twitch.streams.get('lagtvmaximusblack')
s.nil?          # => false (stream is live)
s.game_name     # => "StarCraft II: Heart of the Swarm"
s.viewer_count  # => 2403
s = Twitch.streams.get('destiny')
s.nil?          # => true (stream is offline)

Parameters:

  • stream_name (String)

    The name of the stream to get. This is the same as the channel or user name.

Returns:

  • (Stream)

    A valid Stream object, if the stream exists and is live.

  • (nil)

    If the stream does not exist or is not live.

See Also:



116
117
118
119
120
121
122
123
124
125
# File 'lib/kappa/stream.rb', line 116

def get(stream_name)
  name = CGI.escape(stream_name)

  # HTTP 422 can happen if the stream is associated with a Justin.tv account.
  Twitch::Status.map(404 => nil, 422 => nil) do
    json = @query.connection.get("streams/#{name}")
    stream = json['stream']
    stream.nil? ? nil : Stream.new(stream, @query)
  end
end

#summaryStreamSummary

Get site-wide stream summary statistics.

Examples:

summary = Twitch.streams.summary
summary.viewer_count  # => 194774
summary.channel_count # => 4144

Returns:

See Also:



284
285
286
287
# File 'lib/kappa/stream.rb', line 284

def summary
  json = @query.connection.get('streams/summary')
  StreamSummary.new(json)
end