summaryrefslogtreecommitdiff
path: root/lstwitchvid.rb
blob: c8dd7b5686249eee640c5a1fa66a2484ec94a841 (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
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/ruby

#require 'curses'
require 'json'
require 'net/http'

user = String.new

user = URI.escape(ARGV[0])

broadcasts = String.new

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
http.read_timeout = 500

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

if width < 97
  width=97
end

def videos(http, path, width, broadcasts, client_id)

  total = 100
  offset = 0
  
  while offset <= total
  
    response = JSON.parse(http.request(Net::HTTP::Get.new("#{path}?limit=100&offset=#{offset}&broadcasts=#{broadcasts}&client_id=#{client_id}")).body)
    if response['videos']
    total = response['_total']
      for video in response['videos']
       printf( "%-45s %6i %6i %-36s %-.#{width-46-37-7-7}s\n", video['url'].to_s, video['length'].to_i, video['views'].to_i, video['game'].to_s, video['title'].to_s)
#      Curses.cols
      end
    end
    offset += 100
  
  end

end

print("Highlights:\n\n")

videos(http, "/kraken/channels/#{user}/videos/", width, false, client_id)

print("\nBroadcasts:\n\n")

videos(http, "/kraken/channels/#{user}/videos/", width, true, client_id)