summaryrefslogtreecommitdiff
path: root/crystal/cgi/user.cr
blob: c367c9d7702f058916d959f908b103746be0bae4 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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>" )