summaryrefslogtreecommitdiff
path: root/lstwitch.rb
blob: be3d233170ada6434127614decc8854dbc2878c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
#!/usr/bin/ruby

require 'json'
require 'net/http'

game = String.new

unless ARGV.empty?
  game = "&game=#{URI.escape(ARGV[0])}"
  if ARGV[0] == 'APB' or ARGV[0] == 'apb'
    game = "&game=APB%20Reloaded"
  end
  if ARGV[0] == 'PUBG' or ARGV[0] == 'pubg'
	  game = "&game=#{URI.escape("PLAYERUNKNOWN'S BATTLEGROUNDS")}"
  end
end

confdir = Dir.home + '/.config/twitch/'
ENV['APPDATA'] && confdir = ENV['APPDATA'] + "\\twitch\\"
ENV['XDG_CONFIG_HOME'] && confdir = ENV['XDG_CONFIG_HOME'] + '/twitch/'

client_id = IO.read( confdir + 'client_id' ).chomp

http = Net::HTTP.new('api.twitch.tv', 443)
http.use_ssl = true
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE

offset = 0
total = 1

width = `stty size`.split[1].to_i

if width < 97
  width=97
end

while offset < total

  response = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/streams/?limit=100&offset=#{offset}#{game}&client_id=#{client_id}")).body)
  total = response["_total"]
  for stream in response['streams']
    printf( "%-45s %-36s %6i %6i %-.#{width-46-37-7-7}s\n", stream['channel']['url'].to_s.sub(/http:\/\/.+?\./,''), stream['channel']['game'].to_s, stream['viewers'].to_i, stream['channel']['followers'].to_i, stream['channel']['status'].to_s.gsub(/\n/,'') )
  end
  offset += 100

end