summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xfollowalert.rb45
1 files changed, 24 insertions, 21 deletions
diff --git a/followalert.rb b/followalert.rb
index 61caba1..9282372 100755
--- a/followalert.rb
+++ b/followalert.rb
@@ -11,8 +11,11 @@ unless ARGV.empty?
channel = ARGV[0]
end
-# this is technically data but we'll shove it in .config because i am a bad person
-DATAPATH=Dir.home+'/.config/twitch/'
+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 }
@@ -26,21 +29,21 @@ def difftime(seconds)
return result.join(', ')
end
-unless File.exists?(DATAPATH+channel+'/follows')
- unless File.exists?(DATAPATH+channel)
- unless File.exists?(DATAPATH)
+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(DATAPATH)
+ Dir.mkdir(confdir)
end
- Dir.mkdir(DATAPATH+channel)
+ Dir.mkdir(confdir+channel)
end
- Dir.mkdir(DATAPATH+channel+'/follows')
+ Dir.mkdir(confdir+channel+'/follows')
end
-unless File.exists?(DATAPATH+channel+'/unfollows')
- Dir.mkdir(DATAPATH+channel+'/unfollows')
+unless File.exists?(confdir+channel+'/unfollows')
+ Dir.mkdir(confdir+channel+'/unfollows')
end
http = Net::HTTP.new('api.twitch.tv', 443)
@@ -56,17 +59,13 @@ localunfollows = Array.new
while offset < total
begin
- response = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/channels/#{channel}/follows?offset=#{offset}&limit=100")).body)
-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
+ 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 = DATAPATH+channel+'/follows/'+name
- unfollowpath = DATAPATH+channel+'/unfollows/'+name
+ followpath = confdir+channel+'/follows/'+name
+ unfollowpath = confdir+channel+'/unfollows/'+name
unless File.exists?(followpath)
File.write(followpath, followtime)
if File.exists?(unfollowpath)
@@ -78,15 +77,19 @@ 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(DATAPATH+channel+'/follows/') - [".", ".."]
-localunfollows = Dir.entries(DATAPATH+channel+'/unfollows/') - [".", ".."]
+localfollows = Dir.entries(confdir+channel+'/follows/') - [".", ".."]
+localunfollows = Dir.entries(confdir+channel+'/unfollows/') - [".", ".."]
for unfollow in localfollows - twitchfollows
- unfollowpath = DATAPATH+channel+'/unfollows/'+unfollow
- followpath = DATAPATH+channel+'/follows/'+unfollow
+ unfollowpath = confdir+channel+'/unfollows/'+unfollow
+ followpath = confdir+channel+'/follows/'+unfollow
unless File.exists?(unfollowpath)
unfollowtime = DateTime.now.strftime(format='%s')
File.write(unfollowpath, unfollowtime)