From 79e64d40baaf614a58cbe69ca5b1e3fdd99389aa Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Sun, 25 Oct 2020 00:03:01 -0700 Subject: New: simple user display CGI --- crystal/cgi/user.cr | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 crystal/cgi/user.cr (limited to 'crystal/cgi/user.cr') 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 + + + +query results + + +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( "

Name history:

" ) +# puts( "

" ) +# puts( HTML.escape( history.join( " " ) ) ) +# puts( "

" ) + + print( "

Banned on #bungmonkey? " ) + if JSON.parse( client.get_banned( settings["channel_id"].to_u64, user_id ) )["data"].as_a.empty? + print( "No." ) + else + print( "YES." ) + end + print( "

\n" ) + + puts( "

Channel information:

" ) + puts( "" ) + channel.as_h.each do | key, value| + print( "" ) + end + puts( "
" ) + print( HTML.escape( key.to_s ) ) + print( "" ) + print( HTML.escape( value.to_s ) ) + print( "
" ) + +rescue ex + puts( "An error occurred! " + ex.message.to_s ) +end + +def list_user_follows( settings, client, id ) + puts( "

Last 100 followees:

" ) + 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( "\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( "\n" ) + + end + print( "
" ) + print( HTML.escape( follow["followed_at"]?.to_s.[0..9] ) ) + print( "" ) + print( HTML.escape( channel["created_at"]?.to_s.[0..9] ) ) + print( "" ) + print( HTML.escape( channel["updated_at"]?.to_s.[0..9] ) ) + print( "" ) + print( HTML.escape( follow["to_id"]?.to_s ) ) + print( "" ) + print( HTML.escape( follow["to_name"]?.to_s ) ) + print( "" ) + print( HTML.escape( ( stream["viewers"]?.to_s ) ) ) + print( "" ) + print( HTML.escape( channel["followers"]?.to_s ) ) + print( "" ) + print( HTML.escape( channel["views"]?.to_s ) ) + print( "" ) + print( HTML.escape( ( channel["game"]?.to_s ) ) ) + print( "" ) + print( HTML.escape( ( channel["broadcaster_language"]?.to_s ) ) ) + print( "" ) + print( HTML.escape( ( channel["status"]?.to_s ) ) ) + print( "
" ) + + puts( "

Last 100 followers:

" ) + + 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( "\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( "\n" ) + + + end + print( "
" ) + print( HTML.escape( follow["followed_at"]?.to_s.[0..9] ) ) + print( "" ) + print( HTML.escape( channel["created_at"]?.to_s.[0..9] ) ) + print( "" ) + print( HTML.escape( channel["updated_at"]?.to_s.[0..9] ) ) + print( "" ) + print( HTML.escape( follow["from_id"]?.to_s ) ) + print( "" ) + print( HTML.escape( follow["from_name"]?.to_s ) ) + print( "" ) + print( HTML.escape( ( stream["viewers"]?.to_s ) ) ) + print( "" ) + print( HTML.escape( channel["followers"]?.to_s ) ) + print( "" ) + print( HTML.escape( channel["views"]?.to_s ) ) + print( "" ) + print( HTML.escape( ( channel["game"]?.to_s ) ) ) + print( "" ) + print( HTML.escape( ( channel["broadcaster_language"]?.to_s ) ) ) + print( "" ) + print( HTML.escape( ( channel["status"]?.to_s ) ) ) + print( "
" ) + +end + +list_user_follows( settings, client, user_id ) + +puts( "" ) -- cgit v1.2.3