diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/twitch.cr | 185 |
1 files changed, 108 insertions, 77 deletions
diff --git a/util/twitch.cr b/util/twitch.cr index 1e63736..dea1ef1 100644 --- a/util/twitch.cr +++ b/util/twitch.cr @@ -7,7 +7,7 @@ require "pretty_print" settings = Hash(String, String).new settings["home"] = Path.home.to_s -["access_token", "channel", "channel_id", "client_id", "client_id_twitch", "client_secret", "app_access_token", "eventsub_secret"].each do |key| +["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 @@ -20,9 +20,7 @@ command = ARGV[0] client = Twitcr::Client.new( settings ) case command -when "videos", "lsvideo", "lsvideos" - list_videos_kraken( settings, client ) -when "videos_helix" # smaller json, less information +when "videos", "lsvideo", "lsvideos", "videos_helix" # smaller json, less information list_videos_helix( settings, client ) when "follows", "lsfollow", "lsfollows" list_user_follows( settings, client ) @@ -37,21 +35,23 @@ when "follow" when "unfollow" pp client.unfollow( settings["channel_id"], ARGV[1] ) when "streams_followed" - list_streams_followed( settings, client ) + list_streams_followed( settings, client, ARGV[1]? || settings["channel_id"].to_u64 ) when "clips" pp JSON.parse( client.get_clips( ARGV[1] ) ) when "game" - pp client.get_games( ARGV[1] ) + pp JSON.parse( client.get_games( ARGV[1].to_u64? || ARGV[1] ) ) when "streams" - list_streams_v5( settings, client, ARGV[1] ) -when "streams_helix" list_streams( settings, JSON.parse( client.get_streams( game_id = client.game_id( ARGV[1] ) ) ) ) +when "streams_kraken" + list_streams_v5( settings, client, ARGV[1] ) when "hosts" list_hosts( settings, client ) when "user" pp JSON.parse( client.get_user( ARGV[1] ) ) when "subs" - pp JSON.parse( client.get_subs( client.user( ARGV[1] ).id ) ) + list_subs( settings, client, ARGV[1] ) + + #pp JSON.parse( client.get_subs( client.user( ARGV[1] ).id ) ) when "eventsubs" pp JSON.parse( client.get_eventsubs( ) ) when "eventsub" @@ -60,20 +60,20 @@ when "eventunsub" pp client.delete_eventsubs!( ARGV[1] ) when "panels" pp JSON.parse( client.get_panels( ARGV[1] ) ) +when "authorize" + pp client.authorize() when "validate" pp JSON.parse( client.get_token_validation() ) when "app_token" pp JSON.parse( client.get_app_token() ) +when "token" + pp client.get_token( ARGV[1] ) when "reset_key" pp JSON.parse( client.delete_key!( settings["channel_id"].to_u64 ) ) when "update" update_channel( settings, client, settings["channel_id"].to_u64 ) when "channel" - if ARGV.size > 1 - pp JSON.parse( client.get_channel( client.user_id( ARGV[1] ) ) ) - else - pp JSON.parse( client.get_channel( ) ) - end + pp JSON.parse( client.get_channel( client.user_id( ARGV[1]? || settings["channel_id"] ) ) ) else puts "ARGV[0] must be one of videos, follows" end @@ -89,14 +89,18 @@ macro fcell( data ) if celi == 0 # preserve integrity of primary index at expense of last cell celw[-1] = celw[-1] - ( celw.sum( foh ) - winsz ) - elsif celi != celw.size - celw[celw.size] + elsif celi != ( celw.size - 1 ) + celw[( celw.size - 1 ) ] else celw[celi] = celw[celi] - ( celw.sum( foh ) - winsz ) - ( {{data}}.bytesize - {{data}}.size ) + if celw[celi] < 0 + celw[celi] = 0 + end end end celi+=1 { celw[celi-1], celw[celi-1], {{data}} } + {{debug()}} end macro get_winsz() @@ -111,6 +115,7 @@ def list_videos_helix( settings, client ) id = client.user( ARGV[1] ).id response = JSON.parse( client.get_videos( id ) ) + pp response winsz = get_winsz() @@ -129,61 +134,40 @@ def list_videos_helix( settings, client ) celi=0 printf( format, - *fcell( video["url"].as_s[12..] ), - *fcell( video["created_at"].as_s.[0..15] ), + *fcell( video["url"].as_s ), + *fcell( video["created_at"].as_s[0..15] ), *fcell( video["type"].as_s[0].to_s ), *fcell( video["duration"].as_s[ /([0-9]+[a-z]+)/, 1 ] ), *fcell( video["view_count"].as_i.to_s ), - # helix doesn't report game?! - # *fcell( video["game"].as_s ), - *fcell( video["title"].as_s.chomp ), + *fcell( video["title"].as_s.chomp), *fcell( video["description"].as_s.chomp ), - ) - - end - -end - -def list_videos_kraken( settings, client ) - - # FIXME: handle UInt32 more gracefully - # also: implement Twitcr::User so we can restrict valid strings - id = client.user( ARGV[1] ).id - - response = JSON.parse( client.get_channels_videos( 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 - - response["videos"].as_a.each do |video| - - celi=0 - - begin - printf( format, - *fcell( video["url"].as_s[12..] ), - *fcell( video["created_at"].as_s[0..15] ), - *fcell( video["broadcast_type"].as_s[0].to_s ), - *fcell( video["length"].as_i.to_s ), - *fcell( video["views"].as_i.to_s ), - *fcell( video["game"].as_s ), - *fcell( video["title"].to_s.chomp ), - *fcell( video["description"].to_s.chomp ), - ) - rescue ex - puts ex.message - pp video - end - +) +#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 @@ -544,7 +528,49 @@ def list_streams( settings, response ) end -def list_streams_followed( settings, client ) +def list_subs( settings, client, user_id ) + + user_id = client.user( user_id ).id + + winsz = get_winsz() + + # cell widths + celw=[0,0,0] + # cell index + celi=0 + + format = "%-*.*s %-*.*s %-*.*s\n" + + # format overhead + foh = format.delete( "^ " ).size + + response = JSON.parse( client.get_subs( user_id ) ) + + pp response + +# response["data"].as_a.sort{ |a, b| b["viewer_count"].as_i <=> a["viewer_count"].as_i }.each do |stream| + response["data"].as_a.each do |stream| + + celi=0 + + begin + printf( format, + *fcell( stream["plan_name"].as_s[0].to_s ), + *fcell( stream["gifter_login"].as_s ), + *fcell( stream["user_login"].to_s ), + ) + rescue ex + pp ex + pp stream + end + + end + puts response["points"] + +end +def list_streams_followed( settings, client, user_id ) + + user_id = client.user( user_id ).id winsz = get_winsz() @@ -558,22 +584,25 @@ def list_streams_followed( settings, client ) # format overhead foh = format.delete( "^ " ).size - response = JSON.parse( client.get_streams_followed() ) + response = JSON.parse( client.get_streams_followed( user_id ) ) pp response - response["streams"].as_a.sort{ |a, b| b["viewers"].as_i <=> a["viewers"].as_i }.each do |stream| +# response["data"].as_a.sort{ |a, b| b["viewer_count"].as_i <=> a["viewer_count"].as_i }.each do |stream| + response["data"].as_a.each do |stream| celi=0 begin printf( format, - *fcell( stream["channel"]["url"].as_s[12..] ), - *fcell( stream["stream_type"].as_s[0].to_s ), - *fcell( stream["viewers"].as_i.to_s ), - *fcell( stream["channel"]["followers"].as_i.to_s ), - *fcell( stream["game"].as_s ), - *fcell( stream["channel"]["status"].to_s ), + #*fcell( stream["user_login"].as_s[12..] ), + *fcell( "twitch.tv/#{ stream["user_login"].as_s }" ), + *fcell( stream["type"].as_s[0].to_s ), + *fcell( stream["viewer_count"].as_i.to_s ), + #*fcell( stream["channel"]["followers"].as_i.to_s ), + *fcell( "0" ), + *fcell( stream["game_name"].as_s ), + *fcell( stream["title"].to_s ), ) rescue ex pp ex @@ -586,6 +615,8 @@ end def update_channel( settings, client, id ) - pp JSON.parse( client.put_channel!( id, game: ARGV[1], status: ARGV[2] ) ) + response = client.put_channel!( id, game: ARGV[1], title: ARGV[2] ) + pp response + pp JSON.parse( client.get_channel( settings["channel_id"].to_u64 ) ) end |