#!/usr/bin/env ruby # print new follows/unfollows to stdout require 'json' require 'net/http' require 'date' channel = "bungmonkey" unless ARGV.empty? channel = ARGV[0] end 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 def difftime(seconds) units = [60,60,24].reduce([seconds]) { |m,o| m.unshift(m.shift.divmod(o)).flatten } # [days, hours, minutes, seconds] result = Array.new units[3] > 0 && result.unshift(units[3].to_s + " seconds") units[2] > 0 && result.unshift(units[2].to_s + " minutes") units[1] > 0 && result.unshift(units[1].to_s + " hours") units[0] > 0 && result.unshift(units[0].to_s + " days") result.length > 1 && result[-1]="and "+result[-1] return result.join(', ') end unless File.exists?(confdir+channel+'/follows') unless File.exists?(confdir+channel) unless File.exists?(confdir) unless File.exists?(Dir.home+'/.config') Dir.mkdir(Dir.home+'/.config') end Dir.mkdir(confdir) end Dir.mkdir(confdir+channel) end Dir.mkdir(confdir+channel+'/follows') end unless File.exists?(confdir+channel+'/unfollows') Dir.mkdir(confdir+channel+'/unfollows') end http = Net::HTTP.new('api.twitch.tv', 443) http.use_ssl = true http.read_timeout = 500 offset = 0 total = 1 twitchfollows = Array.new localfollows = Array.new localunfollows = Array.new while offset < total begin response = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/channels/#{channel}/follows?offset=#{offset}&limit=100&client_id=#{client_id}")).body) total = response['_total'] for follow in response['follows'] name = follow['user']['name'].gsub('/','slash') # sterilize followtime = DateTime.iso8601(follow["created_at"]).strftime(format='%s') followpath = confdir+channel+'/follows/'+name unfollowpath = confdir+channel+'/unfollows/'+name unless File.exists?(followpath) File.write(followpath, followtime) if File.exists?(unfollowpath) print name + ' refollowed after ' + difftime( followtime.to_i - File.read(unfollowpath).to_i ) + "!\n" else print name + " is now following!\n" end end twitchfollows << name end offset += 95 rescue exit 2 # if twitch is broken, give up for now and wait until later # very occasionally the API spits out blank json. it would be nice to detect this, but i have not caught it in debugging. end end localfollows = Dir.entries(confdir+channel+'/follows/') - [".", ".."] localunfollows = Dir.entries(confdir+channel+'/unfollows/') - [".", ".."] for unfollow in localfollows - twitchfollows unfollowpath = confdir+channel+'/unfollows/'+unfollow followpath = confdir+channel+'/follows/'+unfollow unless File.exists?(unfollowpath) unfollowtime = DateTime.now.strftime(format='%s') File.write(unfollowpath, unfollowtime) print unfollow + ' unfollowed after ' + difftime(unfollowtime.to_i - File.read(followpath).to_i) + "!\n" File.unlink(followpath) end end