summaryrefslogtreecommitdiff
path: root/lstwitchfollowers.rb
blob: a5c3076492f67b6a44a712352cd0db8ac75101a0 (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
58
59
60
61
#!/usr/bin/ruby

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

user = String.new

user = URI.escape(ARGV[0])

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 followers(http, path, key, width, client_id)

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

end

print("Following:\n\n")

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

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

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