summaryrefslogtreecommitdiff
path: root/followalert.rb
blob: 928237280247dba982e02b77ddd32b33958d1ae0 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/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