From a4b6e747d353ce23e82d28e6a91f45623b4ef3b2 Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Wed, 3 Aug 2022 06:01:21 -0700 Subject: Update to new helix/users/follows system, switch to new config directory --- src/twitcr/rest.cr | 1 + util/twitch.cr | 234 +++++++++++++++++------------------------------------ 2 files changed, 76 insertions(+), 159 deletions(-) diff --git a/src/twitcr/rest.cr b/src/twitcr/rest.cr index ae958a7..5286aeb 100644 --- a/src/twitcr/rest.cr +++ b/src/twitcr/rest.cr @@ -166,6 +166,7 @@ module Twitcr::REST def get_videos( id : UInt64, sort : String = "time", type : String = "all" ); request( "GET", Api::Helix, "/videos?user_id=#{id.to_s}&first=100&sort=#{sort}&type=#{type}" ) end def get_channels_videos( id : UInt64, sort : String = "time", type : String = "all" ); request( "GET", Api::Kraken, "/channels/#{id.to_s}/videos?limit=100&sort=#{sort}&broadcast_type=#{type}" ) end #def get_channel( ); request( "GET", Api::Kraken, "/channel" ) end + def get_channel( ids : Array( UInt64 ) ); request( "GET", Api::Helix, "/channels/?broadcaster_id=#{ids.join( "&broadcaster_id=" )}" ) end def get_channel( id : UInt64 ); request( "GET", Api::Helix, "/channels/?broadcaster_id=#{id.to_s}" ) end def put_channel!( id : UInt64, *, delay : String? = nil, game : String? = nil, title : String? = nil, lang : String? = nil ); #body = Hash( String, Hash( String, String ) ).new diff --git a/util/twitch.cr b/util/twitch.cr index dea1ef1..077abe0 100644 --- a/util/twitch.cr +++ b/util/twitch.cr @@ -4,14 +4,37 @@ require "http/client" require "json" require "pretty_print" +# make json more convenient +struct Nil + def as_s? + self + end +end + + settings = Hash(String, String).new -settings["home"] = Path.home.to_s +settings["configdir"] = Path.home./("/.config/bungmobott/").to_s +settings["statedir"] = Path.home./("/.local/state/bungmobott/").to_s + +if ENV["LOCALAPPDATA"]? + settings["configdir"] = Path.windows( ENV["LOCALAPPDATA"] + "\\bungmobott\\" ).to_s + settings["statedir"] = Path.windows( ENV["LOCALAPPDATA"] + "\\bungmobott\\state\\" ).to_s +end + +ENV["XDG_CONFIG_HOME"]? && ( settings["configdir"] = ENV["XDG_CONFIG_HOME"] + "/bungmobott/" ) +#ENV["XDG_DATA_HOME"]? && ( settings["datadir"] = ENV["XDG_DATA_HOME"] ) # unused? +ENV["XDG_STATE_HOME"]? && ( settings["statedir"] = ENV["XDG_STATE_HOME"] + "/bungmobott/" ) + +Dir.mkdir_p( settings["configdir"] ) +Dir.mkdir_p( settings["statedir"] ) + +settings["home"] = Path.home.to_s ["access_token", "channel", "channel_id", "client_id", "client_id_twitch", "client_secret", "app_access_token", "eventsub_secret", "scopes", "redirect_uri"].each do |key| begin - settings[key] = File.read( settings["home"] + "/.config/twitch/" + key ).chomp - rescue -# STDERR.puts "Warning: Missing " + settings["home"] + "/.config/twitch/" + key + settings[key] = File.read( settings["configdir"] + key ).chomp + rescue IO::Error + STDERR.puts "Warning: Missing " + settings["configdir"] + key end end @@ -22,14 +45,10 @@ client = Twitcr::Client.new( settings ) case command when "videos", "lsvideo", "lsvideos", "videos_helix" # smaller json, less information list_videos_helix( settings, client ) -when "follows", "lsfollow", "lsfollows" - list_user_follows( settings, client ) when "followees" list_user_followees( settings, client ) when "followers" list_user_followers( settings, client ) -when "get_follow" - pp JSON.parse( client.get_user_follows( from: client.user_id( ARGV[1] ).to_u64, to: client.user_id( ARGV[2] ).to_u64 ) ) when "follow" pp JSON.parse( client.follow( settings["channel_id"], ARGV[1] ) ) when "unfollow" @@ -141,33 +160,7 @@ def list_videos_helix( settings, client ) *fcell( video["view_count"].as_i.to_s ), *fcell( video["title"].as_s.chomp), *fcell( video["description"].as_s.chomp ), -) -#pp celw -# if video["title"].as_s.chomp.bytesize > celw[celi] -# celw[celi] = video["title"].as_s.chomp.bytesize -# end -# if celw.sum( foh ) > winsz -# # don't exceed winsz, even on screwball wide characters -# if celi == 0 -# # preserve integrity of primary index at expense of last cell -# celw[-1] = celw[-1] - ( celw.sum( foh ) - winsz ) # 33 - 13 = 20 -# elsif celi != ( celw.size - 1 ) -# puts -# pp foh # 5 -# # sum = 93 -# pp winsz # 80 -# pp celi # 5 -# pp celw # [39, 16, 1, 8, 1, 33] -# pp celw.size # 6 -# celw[( celw.size-1 )] -# else -# celw[celi] = celw[celi] - ( celw.sum( foh ) - winsz ) - ( video["title"].as_s.chomp.bytesize - video["title"].as_s.chomp.size ) -# end -# end -# celi+=1 -# printf( "%*.*s\n", celw[celi-1], celw[celi-1], video["title"].as_s.chomp ) -# -# ) + ) end @@ -184,7 +177,7 @@ def list_user_followees( settings, client ) # cell index celi=0 - format = "%*.*s %-*.*s %*.*s %*.*s %*.*s %-*.*s %*.*s %-*.*s\n" + format = "%*.*s %-*.*s %*.*s %*.*s %-*.*s %-*.*s %*.*s %-*.*s\n" # format overhead foh = format.delete( "^ " ).size @@ -195,36 +188,48 @@ def list_user_followees( settings, client ) streams = Hash( UInt64, JSON::Any ).new channels = Hash( UInt64, JSON::Any ).new + users = Hash( UInt64, JSON::Any ).new + + JSON.parse( client.get_streams( user_ids: followsuids ) )["data"].as_a.each do |stream| + streams[ stream["user_id"].as_s.to_u64 ] = stream + end - 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"] + JSON.parse( client.get_channel( ids: followsuids ) )["data"].as_a.each do |channel| + channels[ channel["broadcaster_id"].as_s.to_u64 ] = channel + end + + JSON.parse( client.get_user( id: followsuids ) )["data"].as_a.each do |user| + users[ user["id"].as_s.to_u64 ] = user end follows.as_a.each do |follow| uid = follow["to_id"].as_s.to_u64 - channel = ( channels[uid]? || JSON.parse( client.get_channel( uid ) ) ) + channel = ( channels[uid]? || JSON.parse( "{}" ) ) stream = ( streams[uid]? || JSON.parse( "{}" ) ) + user = ( users[uid]? || JSON.parse( "{}" ) ) celi=0 begin printf( format, - #*fcell( follow["followed_at"]?.to_s.[0..9] ), *fcell( follow["followed_at"]?.to_s ), - *fcell( follow["to_name"]?.to_s ), - *fcell( stream["viewers"]?.to_s ), - *fcell( channel["followers"]?.to_s ), - *fcell( channel["views"]?.to_s ), - *fcell( channel["game"]?.to_s ), + *fcell( follow["to_login"]?.to_s ), + *fcell( stream["viewer_count"]?.to_s ), + #*fcell( channel["followers"]?.to_s ), + *fcell( user["view_count"]?.to_s ), + *fcell( channel["game_name"]?.to_s ), *fcell( channel["broadcaster_language"]?.to_s ), - *fcell( channel["status"]?.to_s ), + *fcell( ( user["broadcaster_type"]?.to_s.presence || " " )[0].to_s ), + *fcell( channel["title"]?.to_s ), ) rescue ex puts( ex ) pp follow + pp channel + pp stream + pp user end end @@ -241,7 +246,7 @@ def list_user_followers( settings, client ) # cell index celi=0 - format = "%*.*s %-*.*s %*.*s %*.*s %*.*s %-*.*s %*.*s %-*.*s\n" + format = "%*.*s %-*.*s %*.*s %*.*s %-*.*s %-*.*s %*.*s %-*.*s\n" # format overhead foh = format.delete( "^ " ).size @@ -258,142 +263,53 @@ def list_user_followers( settings, client ) streams = Hash( UInt64, JSON::Any ).new channels = Hash( UInt64, JSON::Any ).new + users = Hash( UInt64, JSON::Any ).new - - JSON.parse( client.get_streams_v5( channel: followersuids ) )["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"] + JSON.parse( client.get_streams( user_ids: followersuids ) )["data"].as_a.each do |stream| + streams[ stream["user_id"].as_s.to_u64 ] = stream end - 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( "{}" ) ) - - celi=0 - - begin - printf( format, - *fcell( follow["followed_at"]?.to_s.[0..15] ), - *fcell( follow["from_name"]?.to_s ), - *fcell( stream["viewers"]?.to_s ), - *fcell( channel["followers"]?.to_s ), - *fcell( channel["views"]?.to_s ), - *fcell( channel["game"]?.to_s ), - *fcell( channel["broadcaster_language"]?.to_s ), - *fcell( channel["status"]?.to_s ), - ) - rescue ex - puts ex - pp follow - end - + JSON.parse( client.get_channel( ids: followersuids ) )["data"].as_a.each do |channel| + channels[ channel["broadcaster_id"].as_s.to_u64 ] = channel end - pp followersjson["pagination"] - -end - -def list_user_follows( settings, client ) - - id = client.user( ARGV[1] ).id - - winsz = get_winsz() - - # cell widths - celw=[0,0,0,0,0,0,0,0] - # cell index - celi=0 - - format = "%*.*s %-*.*s %*.*s %*.*s %*.*s %-*.*s %*.*s %-*.*s\n" - - # format overhead - foh = format.delete( "^ " ).size - - puts( "Last 100 followees:\n" ) - 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"] + JSON.parse( client.get_user( id: followersuids ) )["data"].as_a.each do |user| + users[ user["id"].as_s.to_u64 ] = user end - follows.as_a.each do |follow| + followers.as_a.each do |follow| - uid = follow["to_id"].as_s.to_u64 + uid = follow["from_id"].as_s.to_u64 - channel = ( channels[uid]? || JSON.parse( client.get_channel( uid ) ) ) + channel = ( channels[uid]? || JSON.parse( "{}" ) ) stream = ( streams[uid]? || JSON.parse( "{}" ) ) + user = ( users[uid]? || JSON.parse( "{}" ) ) celi=0 begin printf( format, - #*fcell( follow["followed_at"]?.to_s.[0..9] ), *fcell( follow["followed_at"]?.to_s ), - *fcell( follow["to_name"]?.to_s ), - *fcell( stream["viewers"]?.to_s ), - *fcell( channel["followers"]?.to_s ), - *fcell( channel["views"]?.to_s ), - *fcell( channel["game"]?.to_s ), + *fcell( follow["from_login"]?.to_s ), + *fcell( stream["viewer_count"]?.to_s ), + #*fcell( channel["followers"]?.to_s ), # How do we get this now? + *fcell( user["view_count"]?.to_s ), + *fcell( channel["game_name"]?.to_s ), *fcell( channel["broadcaster_language"]?.to_s ), - *fcell( channel["status"]?.to_s ), + *fcell( ( user["broadcaster_type"]?.to_s.presence || " " )[0].to_s ), + *fcell( channel["title"]?.to_s ), ) rescue ex puts( ex ) pp follow + pp channel + pp stream + pp user end end - # cell widths - celw=[0,0,0,0,0,0,0,0] - # cell index - celi=0 - - puts( "\nLast 100 followers:\n" ) - 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 - - 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( "{}" ) ) - - celi=0 - - begin - printf( format, - *fcell( follow["followed_at"]?.to_s.[0..15] ), - *fcell( follow["from_name"]?.to_s ), - *fcell( stream["viewers"]?.to_s ), - *fcell( channel["followers"]?.to_s ), - *fcell( channel["views"]?.to_s ), - *fcell( channel["game"]?.to_s ), - *fcell( channel["broadcaster_language"]?.to_s ), - *fcell( channel["status"]?.to_s ), - ) - rescue ex - puts ex - pp follow - end - - end + pp followersjson["pagination"] end -- cgit v1.2.3