summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README8
-rwxr-xr-xgettoken.rb45
-rwxr-xr-xlstwitch.rb33
-rwxr-xr-xlstwitchfollowers.rb49
-rw-r--r--twitch-rb-editchannel.patch15
-rwxr-xr-xtwitchupdate.rb32
6 files changed, 182 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..47cfcab
--- /dev/null
+++ b/README
@@ -0,0 +1,8 @@
+Authenticated commands use https://github.com/dustinlakin/twitch-rb
+
+twitch-rb is incredibly badly maintained because the Ruby community does not
+believe in stable APIs. I am tentatively targeting commit f619e3a of it because
+it requires less patching than every other commit I've found.
+
+You'll probably want to use 'patch -p1 < twitch-rb-editchannel.patch' on it.
+
diff --git a/gettoken.rb b/gettoken.rb
new file mode 100755
index 0000000..7896e5a
--- /dev/null
+++ b/gettoken.rb
@@ -0,0 +1,45 @@
+#!/usr/bin/ruby
+
+require 'twitch'
+require 'pp'
+
+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
+secret_key = IO.read( confdir+'secret_key' ).chomp
+redirect_uri = IO.read( confdir+'redirect_uri' ).chomp
+
+# Feel free to use https://www.omgwallhack.org/toys/env.cgi as a redirect URI; I am probably too lazy to steal your oauth token.
+
+scope = '
+user_read
+user_blocks_edit
+user_blocks_read
+user_follows_edit
+channel_read
+channel_editor
+channel_commercial
+channel_stream
+channel_subscriptions
+channel_check_subscription
+chat_login
+'.split.sort.uniq
+
+twitch = Twitch.new({
+ :client_id => client_id,
+ :secret_key => secret_key,
+ :redirect_uri => redirect_uri,
+ :scope => scope,
+})
+
+print( twitch.getLink + "\n" )
+
+#access_token = twitch.auth(code)[:body]["access_token"]
+#
+#twitch=Twitch.new({
+# :access_token => access_token
+#})
+#
+#channel = twitch.getYourChannel
diff --git a/lstwitch.rb b/lstwitch.rb
new file mode 100755
index 0000000..92e5255
--- /dev/null
+++ b/lstwitch.rb
@@ -0,0 +1,33 @@
+#!/usr/bin/ruby
+
+require 'json'
+require 'net/http'
+
+game = String.new
+
+unless ARGV.empty?
+ game = "&game=#{URI.escape(ARGV[0])}"
+ if ARGV[0] == 'APB' or ARGV[0] == 'apb'
+ game = "&game=APB%20Reloaded"
+ end
+end
+
+http = Net::HTTP.new('api.twitch.tv', 443)
+http.use_ssl = true
+#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+
+offset = 0
+total = 1
+
+width = `stty size`.split[1].to_i
+
+while offset < total
+
+ response = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/streams/?offset=#{offset}#{game}")).body)
+ total = response["_total"]
+ for stream in response['streams']
+ printf( "%-45s %-36s %6i %-.#{width-46-37-7}s\n", stream['channel']['url'].to_s, stream['channel']['game'].to_s, stream['viewers'], stream['channel']['status'].to_s.gsub(/\n/,'') )
+ end
+ offset += 100
+
+end
diff --git a/lstwitchfollowers.rb b/lstwitchfollowers.rb
new file mode 100755
index 0000000..bb762a3
--- /dev/null
+++ b/lstwitchfollowers.rb
@@ -0,0 +1,49 @@
+#!/usr/bin/ruby
+
+# the 'streams' api much more efficient but requires authentication
+
+require 'json'
+require 'net/http'
+
+user = String.new
+
+user = URI.escape(ARGV[0])
+
+http = Net::HTTP.new('api.twitch.tv', 443)
+http.use_ssl = true
+#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+http.read_timeout = 500
+
+width = `stty size`.split[1].to_i
+
+def followers(http, path, key, width)
+
+ followed = { '_total' => 0 }
+ offset = 0
+
+ while offset <= followed['_total']
+
+ for channel in JSON.parse(http.request(Net::HTTP::Get.new("#{path}?limit=100&offset=#{offset}")).body)['follows']
+ name = channel[key]['display_name']
+ stream = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/streams/#{name}")).body)['stream']
+ if stream
+ printf( "%-45s %-36s %6i %-.#{width-46-37-7}s\n", stream['channel']['url'].to_s, stream['channel']['game'].to_s, stream['viewers'], stream['channel']['status'].to_s.gsub(/\n/,'') )
+# print(" #{stream['game']} #{stream['channel']['status']}")
+# Curses.cols
+ else
+ print("twitch.tv/#{name}\n")
+ end
+ end
+ offset += 100
+
+ end
+
+end
+
+print("Following:\n\n")
+
+followers(http, "/kraken/users/#{user}/follows/channels", 'channel', width)
+
+print("\nFollowed by:\n\n")
+
+followers(http, "/kraken/channels/#{user}/follows", 'user', width)
diff --git a/twitch-rb-editchannel.patch b/twitch-rb-editchannel.patch
new file mode 100644
index 0000000..520b251
--- /dev/null
+++ b/twitch-rb-editchannel.patch
@@ -0,0 +1,15 @@
+--- a/lib/twitch.rb
++++ b/lib/twitch.rb
+@@ -77,9 +77,9 @@ class Twitch
+ get(url)
+ end
+
+- def editChannel(status, game)
++ def editChannel(channel, status, game)
+ return false if !@access_token
+- path = "/channels/dustinlakin/?oauth_token=#{@access_token}"
++ path = "/channels/#{channel}/?oauth_token=#{@access_token}"
+ url = @base_url + path
+ data = {
+ :channel =>{
+
diff --git a/twitchupdate.rb b/twitchupdate.rb
new file mode 100755
index 0000000..946d627
--- /dev/null
+++ b/twitchupdate.rb
@@ -0,0 +1,32 @@
+#!/usr/bin/ruby
+
+require 'twitch'
+require 'pp'
+
+confdir = Dir.home + '/.config/twitch/'
+ENV['APPDATA'] && confdir = ENV['APPDATA'] + "\\twitch\\"
+ENV['XDG_CONFIG_HOME'] && confdir = ENV['XDG_CONFIG_HOME'] + '/twitch/'
+
+access_token = IO.read( confdir + 'access_token' ).chomp
+
+twitch=Twitch.new({
+ :access_token => access_token
+})
+
+channel = twitch.getYourChannel
+
+game = channel[:body]["game"]
+if ARGV[0].to_s.length > 0
+ game = ARGV[0]
+end
+
+status = channel[:body]["status"]
+if ARGV[1].to_s.length > 0
+ status = ARGV[1]
+end
+
+unless ARGV.empty?
+ pp twitch.editChannel(channel[:body]['name'],status,game)
+else
+ pp channel
+end