diff options
Diffstat (limited to 'lstwitchfollowers.rb')
-rwxr-xr-x | lstwitchfollowers.rb | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/lstwitchfollowers.rb b/lstwitchfollowers.rb index a5c3076..8faebbb 100755 --- a/lstwitchfollowers.rb +++ b/lstwitchfollowers.rb @@ -1,6 +1,6 @@ #!/usr/bin/ruby -#require 'curses' +require 'io/console' # ruby >1.9.3 require 'json' require 'net/http' @@ -19,31 +19,36 @@ http.use_ssl = true #http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.read_timeout = 500 -width = `stty size`.split[1].to_i +WIDTH = [ IO.console.winsize[1], 80 ].max -if width < 97 - width=97 +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 followers(http, path, key, width, client_id) +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 = JSON.parse(http.request(Net::HTTP::Get.new("#{path}?limit=100&offset=#{offset}&client_id=#{client_id}")).body) + response = twitchrequest( http, path, offset, client_id ) 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'] + stream = twitchrequest( http, '/kraken/streams/' + name, 0, client_id )['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 + printfollow( stream['viewers'], name, stream['channel']['game'].to_s, stream['channel']['followers'], stream['channel']['status'].to_s.gsub(/\n/,'') ) 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/,'') ) + profile = twitchrequest( http, '/kraken/channels/' + name, 0, client_id ) + printfollow( '', name, profile['game'].to_s, profile['followers'], profile['status'].to_s.gsub(/\n/,'') ) end end offset += 100 @@ -54,8 +59,8 @@ end print("Following:\n\n") -followers(http, "/kraken/users/#{user}/follows/channels", 'channel', width, client_id) +followers(http, "/kraken/users/#{user}/follows/channels", 'channel', client_id) print("\nFollowed by:\n\n") -followers(http, "/kraken/channels/#{user}/follows", 'user', width, client_id) +followers(http, "/kraken/channels/#{user}/follows", 'user', client_id) |