summaryrefslogtreecommitdiff
path: root/crystal/cgi/user.cr
diff options
context:
space:
mode:
Diffstat (limited to 'crystal/cgi/user.cr')
-rw-r--r--crystal/cgi/user.cr221
1 files changed, 221 insertions, 0 deletions
diff --git a/crystal/cgi/user.cr b/crystal/cgi/user.cr
new file mode 100644
index 0000000..c367c9d
--- /dev/null
+++ b/crystal/cgi/user.cr
@@ -0,0 +1,221 @@
+require "http/client"
+require "json"
+require "pretty_print"
+require "xml"
+#require "inotify"
+require "time"
+require "twitcr"
+require "http/params"
+require "html"
+
+STDOUT.sync = true
+
+puts( "Content-type: text/html" )
+puts( "" )
+print(
+<<-XML
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<title>query results</title>
+</head>
+<body>
+XML
+)
+
+begin
+ params = HTTP::Params.parse( ENV["QUERY_STRING"] )
+ user = params["user"].to_s
+rescue ex
+ puts( "An error occurred! " + ex.message.to_s )
+end
+
+settings = Hash(String, String).new
+
+settings["home"] = "/home/jrayhawk"
+["access_token", "channel", "channel_id", "client_id", "client_id_twitch"].each do |key|
+ begin
+ settings[key] = File.read( settings["home"] + "/.config/twitch/" + key ).chomp
+ rescue Errno
+ STDERR.puts "Warning: Missing " + settings["home"] + "/.config/twitch/" + key
+ end
+end
+
+client = Twitcr::Client.new( settings )
+
+
+if user && user =~ /^([a-zA-Z0-9_]+)$/
+ if user =~ /^[0-9]+$/
+ user_id = UInt64.new( user.to_u64 )
+ user_name = client.user( user_id ).login
+ else
+ user_name = user
+ user_id = UInt64.new( client.user( user_name ).id )
+ end
+else
+ puts( "user should consist of letters, numbers, or underscores" )
+ exit 1
+end
+
+
+def commanderroot_changelog( uid )
+ client = HTTP::Client.new( "twitch-tools.rootonline.de", port = 443, tls = true )
+ response = client.exec( "GET", "/username_changelogs_search.php?q=" + uid )
+ client.close
+
+ document = XML.parse_html( response.body )
+
+ rows = document.xpath_nodes( "/html/body/div/table/tbody/tr" )
+
+ text = Array(String).new
+
+ rows.each do |row|
+ text.push( row.first_element_child.not_nil!.next_element.not_nil!.inner_text.chomp(" ").match(/[0-9a-z_]+/).not_nil![0] + " " + row.first_element_child.not_nil!.next_element.not_nil!.next_element.not_nil!.next_element.not_nil!.inner_text.chomp(" ").match(/\d{4}-\d{2}-\d{2}/).not_nil![0] )
+ end
+
+ text.reverse!
+
+ return text
+end
+
+
+
+begin
+ channel = JSON.parse( client.get_channel( user_id ) )
+# user_ctime = channel["created_at"].to_s.match( /\d{4}-\d{2}-\d{2}/ ).not_nil![0]
+#
+# history = commanderroot_changelog( user_id.to_s )
+#
+# history.unshift( user_ctime )
+# history.unshift( user_id.to_s )
+# history.push( user_name )
+#
+# puts( "<h2>Name history:</h2>" )
+# puts( "<p>" )
+# puts( HTML.escape( history.join( " " ) ) )
+# puts( "</p>" )
+
+ print( "<h2>Banned on #bungmonkey? " )
+ if JSON.parse( client.get_banned( settings["channel_id"].to_u64, user_id ) )["data"].as_a.empty?
+ print( "No." )
+ else
+ print( "<font color=\"red\">YES</font>." )
+ end
+ print( "</h2>\n" )
+
+ puts( "<h2>Channel information:</h2>" )
+ puts( "<table>" )
+ channel.as_h.each do | key, value|
+ print( "<tr><td>" )
+ print( HTML.escape( key.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( value.to_s ) )
+ print( "</td></tr>" )
+ end
+ puts( "</table>" )
+
+rescue ex
+ puts( "An error occurred! " + ex.message.to_s )
+end
+
+def list_user_follows( settings, client, id )
+ puts( "<h2>Last 100 followees:</h2>" )
+ follows = JSON.parse( client.get_user_follows( from: id ) )["data"]
+
+ followsuids = follows.as_a.map { |follow| follow["to_id"].as_s.to_u64 }
+
+ streams = Hash( UInt64, JSON::Any ).new
+ channels = Hash( UInt64, JSON::Any ).new
+
+ JSON.parse( client.get_streams_v5( channel: followsuids ) )["streams"].as_a.each do |stream|
+ streams[ stream["channel"]["_id"].as_i64.to_u64 ] = stream
+ channels[ stream["channel"]["_id"].as_i64.to_u64 ] = stream["channel"]
+ end
+
+ print( "<table>\n" )
+ follows.as_a.each do |follow|
+
+ uid = follow["to_id"].as_s.to_u64
+
+ channel = ( channels[uid]? || JSON.parse( client.get_channel( uid ) ) )
+ stream = ( streams[uid]? || JSON.parse( "{}" ) )
+
+ print( "<tr><td>" )
+ print( HTML.escape( follow["followed_at"]?.to_s.[0..9] ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["created_at"]?.to_s.[0..9] ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["updated_at"]?.to_s.[0..9] ) )
+ print( "</td><td>" )
+ print( HTML.escape( follow["to_id"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( follow["to_name"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( stream["viewers"]?.to_s ) ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["followers"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["views"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( channel["game"]?.to_s ) ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( channel["broadcaster_language"]?.to_s ) ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( channel["status"]?.to_s ) ) )
+ print( "</td></tr>\n" )
+
+ end
+ print( "</table>" )
+
+ puts( "<h2>Last 100 followers:</h2>" )
+
+ followers = JSON.parse( client.get_user_follows( to: id ) )["data"]
+
+ followersuids = followers.as_a.map { |follow| follow["from_id"].as_s.to_u64 }
+
+ JSON.parse( client.get_streams_v5( channel: followersuids - followsuids ) )["streams"].as_a.each do |stream|
+ streams[ stream["channel"]["_id"].as_i64.to_u64 ] = stream
+ channels[ stream["channel"]["_id"].as_i64.to_u64 ] = stream["channel"]
+ end
+
+ print( "<table>\n" )
+ followers.as_a.each do |follow|
+
+ uid = follow["from_id"].as_s.to_u64
+
+ channel = ( channels[uid]? || JSON.parse( client.get_channel( uid ) ) )
+ stream = ( streams[uid]? || JSON.parse( "{}" ) )
+
+ print( "<tr><td>" )
+ print( HTML.escape( follow["followed_at"]?.to_s.[0..9] ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["created_at"]?.to_s.[0..9] ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["updated_at"]?.to_s.[0..9] ) )
+ print( "</td><td>" )
+ print( HTML.escape( follow["from_id"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( follow["from_name"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( stream["viewers"]?.to_s ) ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["followers"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( channel["views"]?.to_s ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( channel["game"]?.to_s ) ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( channel["broadcaster_language"]?.to_s ) ) )
+ print( "</td><td>" )
+ print( HTML.escape( ( channel["status"]?.to_s ) ) )
+ print( "</td></tr>\n" )
+
+
+ end
+ print( "</table>" )
+
+end
+
+list_user_follows( settings, client, user_id )
+
+puts( "</body>" )