summaryrefslogtreecommitdiff
path: root/lstwitchfollowers.rb
blob: bb762a3833d2549b8aa911b6319799c9818977a4 (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
#!/usr/bin/ruby

# the 'streams' api much more efficient but requires authentication

require 'json'
require 'net/http'

user = String.new

user = URI.escape(ARGV[0])

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

def followers(http, path, key, width)

  followed = { '_total' => 0 }
  offset = 0
  
  while offset <= followed['_total']
  
    for channel in JSON.parse(http.request(Net::HTTP::Get.new("#{path}?limit=100&offset=#{offset}")).body)['follows']
      name = channel[key]['display_name']
      stream = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/streams/#{name}")).body)['stream']
      if stream
        printf( "%-45s %-36s %6i %-.#{width-46-37-7}s\n", stream['channel']['url'].to_s, stream['channel']['game'].to_s, stream['viewers'], stream['channel']['status'].to_s.gsub(/\n/,'') )
#        print(" #{stream['game']} #{stream['channel']['status']}")
#        Curses.cols
      else
        print("twitch.tv/#{name}\n")
      end
    end
    offset += 100
  
  end

end

print("Following:\n\n")

followers(http, "/kraken/users/#{user}/follows/channels", 'channel', width)

print("\nFollowed by:\n\n")

followers(http, "/kraken/channels/#{user}/follows", 'user', width)