#!/usr/bin/ruby require 'io/console' # ruby >1.9.3 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 = [ IO.console.winsize[1], 80 ].max def twitchrequest( http, path, offset, client_id) tries ||=3 return JSON.parse(http.request(Net::HTTP::Get.new("#{path}?limit=100&offset=#{offset}&client_id=#{client_id}")).body) rescue retry unless (tries -= 1).zero? end def printfollow( viewers, name, game, followers, status ) printf( "%6s %-30s %-30.30s %6i %-.#{WIDTH-31-31-7-7}s\n", viewers, 'twitch.tv/' + name, game, followers, status ) end def followers(http, path, key, client_id) total = 100 offset = 0 while offset <= total response = twitchrequest( http, path, offset, client_id ) total = response['_total'] for channel in response['follows'] name = channel[key]['name'] stream = twitchrequest( http, '/kraken/streams/' + name, 0, client_id )['stream'] if stream printfollow( stream['viewers'], name, stream['channel']['game'].to_s, stream['channel']['followers'].to_i, stream['channel']['status'].to_s.gsub(/\n/,'') ) else profile = twitchrequest( http, '/kraken/channels/' + name, 0, client_id ) printfollow( '', name, profile['game'].to_s, profile['followers'].to_i, profile['status'].to_s.gsub(/\n/,'') ) end end offset += 100 end end print("Following:\n\n") followers(http, "/kraken/users/#{user}/follows/channels", 'channel', client_id) print("\nFollowed by:\n\n") followers(http, "/kraken/channels/#{user}/follows", 'user', client_id)