diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/twitcr/client.cr | 15 | ||||
-rw-r--r-- | src/twitcr/mappings/game.cr | 13 | ||||
-rw-r--r-- | src/twitcr/rest.cr | 64 |
3 files changed, 55 insertions, 37 deletions
diff --git a/src/twitcr/client.cr b/src/twitcr/client.cr index 1a02a40..a0d4be2 100644 --- a/src/twitcr/client.cr +++ b/src/twitcr/client.cr @@ -3,12 +3,13 @@ require "./mappings/*" class Twitcr::Client - getter token : String? - getter client_id : String - getter client_secret : String + #getter token : String? + #getter client_id : String + #getter client_secret : String - def initialize(@client_secret, @client_id) - @token = nil + #def initialize(@client_secret, @client_id) + # @token = nil + #end include REST @@ -48,11 +49,11 @@ class Twitcr::Client end def game_id( name : String ) - name.to_u64{ game(name).id } + game(name).id end def game_id( id : UInt64 ) - + self end def unfollow( unfollower : ( String | UInt64 ), unfollowee : ( String | UInt64 ) ) diff --git a/src/twitcr/mappings/game.cr b/src/twitcr/mappings/game.cr index 2915eb5..d0818cd 100644 --- a/src/twitcr/mappings/game.cr +++ b/src/twitcr/mappings/game.cr @@ -2,15 +2,16 @@ require "./converters" module Twitcr struct GameList - JSON.mapping({data: Array(Game)}) + include JSON::Serializable + property data : Array(Game) end struct Game - JSON.mapping({ - id: {type: UInt64, converter: ID::Converter}, - name: String, - box_art_url: String, - }) + include JSON::Serializable + @[JSON::Field(converter: ID::Converter)] + property id : UInt64 + property name : String + property box_art_url : String end end diff --git a/src/twitcr/rest.cr b/src/twitcr/rest.cr index 0fc59c1..ae958a7 100644 --- a/src/twitcr/rest.cr +++ b/src/twitcr/rest.cr @@ -143,27 +143,43 @@ module Twitcr::REST if ! query_string.empty?; query_string = "?" + query_string end request( "GET", Api::Kraken, "/streams" + query_string ) end - def get_streams_followed( ); request( "GET", Api::Kraken, "/streams/followed?limit=100&stream_type=all" ) end + #deprecated + #def get_streams_followed( ); request( "GET", Api::Kraken, "/streams/followed?limit=100&stream_type=all" ) end + def get_streams_followed( user_id : UInt64 ); request( "GET", Api::Helix, "/streams/followed?user_id=#{user_id}" ) end # FIXME: Deal with pagination def get_banned( broadcaster_id : UInt64 ); request( "GET", Api::Helix, "/moderation/banned?broadcaster_id=#{broadcaster_id.to_s}" ) end def get_banned( broadcaster_id : UInt64, user_id : UInt64 ); request( "GET", Api::Helix, "/moderation/banned?broadcaster_id=#{broadcaster_id.to_s}&user_id=#{user_id}" ) end def get_banned( broadcaster_id : UInt64, user_id : Array( UInt64 ) ); request( "GET", Api::Helix, "/moderation/banned?broadcaster_id=#{broadcaster_id.to_s}&user_id=#{user_id.join("&user_id=")}" ) end def get_banned_events( broadcaster_id : UInt64, first : UInt64 = 100 ); request( "GET", Api::Helix, "/moderation/banned/events?broadcaster_id=#{broadcaster_id.to_s}&first=#{first.to_s}" ) end def get_clips( name : String ); request( "GET", Api::Kraken, "/clips/top?channel=#{name}&first=100&period=all" ) end +# def get_videos( *, id : UInt64, user_id : UInt64, game_id : UInt64 ) +# params = Hash( String, String ).new +# params["first"] = "100" +# params["id"] = id.to_s +# params["user_id"] = user_id.to_s +# params["game_id"] = game_id.to_s +# if after ; params["after"] = after end +# query_string = HTTP::Params.encode( params ) +# if ! query_string.empty?; query_string = "?" + query_string end +# request( "GET", Api::Helix, "/videos" + query_string ) +# end 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( id : UInt64 ); request( "GET", Api::Kraken, "/channels/#{id.to_s}" ) end - def put_channel!( id : UInt64, *, delay : String? = nil, game : String? = nil, status : String? = nil); - body = Hash( String, Hash( String, String ) ).new - body["channel"] = Hash( String, String).new - if game ; body["channel"]["game"] = game end - if status ; body["channel"]["status"] = status end - if delay ; body["channel"]["delay"] = delay end - request( "PUT", Api::Kraken, "/channels/#{id.to_s}", body.to_json, contenttype: "application/json") end + #def get_channel( ); request( "GET", Api::Kraken, "/channel" ) 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 + body = Hash( String, String ).new + #body["channel"] = Hash( String, String).new + game && ( body["game_id"] = game_id( game ).to_s ) + lang && ( body["lang"] = lang ) + title && ( body["title"] = title ) + delay && ( body["delay"] = delay ) + pp body.to_json + request( "PATCH", Api::Helix, "/channels/?broadcaster_id=#{id.to_s}", body.to_json, contenttype: "application/json") end def delete_key!( id : UInt64); request( "DELETE", Api::Kraken, "/channels/#{id.to_s}/stream_key" ) end def get_hosts( id : UInt64 ); request( "GET", Api::Kraken, "/channels/#{id.to_s}/hosts" ) end - def get_subs( id : UInt64 ); request( "GET", Api::Kraken, "/channels/#{id.to_s}/subscriptions" ) end + def get_subs( id : UInt64 ); request( "GET", Api::Helix, "/subscriptions?first=100&broadcaster_id=#{id}" ) end def get_eventsubs( ); request( "GET", Api::Helix, "/eventsub/subscriptions" ) end def delete_eventsubs!( id : String ); request( "DELETE", Api::Helix, "/eventsub/subscriptions?id=#{id}" ) end @@ -172,14 +188,11 @@ module Twitcr::REST varname = "broadcaster_user_id" case type when "channel.raid" - version = "beta" varname = "to_broadcaster_user_id" when "user.update" varname = "user_id" end - body = Hash( String, String | Hash( String, String ) ).new - - body = { + body = Hash( String, Bool | String | Hash( String, String ) ){ "type" => type, "version" => version, "condition" => { varname => id.to_s }, @@ -189,8 +202,11 @@ module Twitcr::REST "secret" => @settings["eventsub_secret"] # FIXME }, } + if type == "drop.entitlement.grant" + body["is_batching_enabled"] = true + end request( "POST", Api::Helix, "/eventsub/subscriptions", body.to_json, contenttype: "application/json" ) end - def get_app_token( ); request( "POST", Api::Oauth2, "/token?client_id=#{@settings["client_id"].not_nil!}&client_secret=#{@settings["client_secret"].not_nil!}&grant_type=client_credentials" ) end + def get_app_token( ); request( "POST", Api::Oauth2, "/token?client_id=#{@settings["client_id"].not_nil!}&client_secret=#{@settings["client_secret"].not_nil!}&grant_type=client_credentials&scope=#{ @settings["scopes"].split("\n").join("%20") }" ) end def get_token_validation( ); request( "GET", Api::Oauth2, "/validate" ) end # These APIs might require the official Twitch.com web Client-ID now? def get_panels( login : String ); request( "GET", Api::Api, "/channels/#{login}/panels" ) end @@ -199,7 +215,7 @@ module Twitcr::REST def unfriend!( friender : UInt64, friendee : UInt64 ); request( "DELETE", Api::Kraken, "/users/#{friender.to_s}/friends/#{friendee.to_s}" ) end def get_friends( friender : UInt64 ); request( "GET", Api::Kraken, "/users/#{friender.to_s}/friends" ) end - def parse_game( game : UInt64 | String ) + def parse_game( game : UInt64 | String ) list = GameList.from_json( get_games( game ) ) raise EMPTY_RESULT if list.data.empty? list.data.first @@ -211,15 +227,15 @@ module Twitcr::REST list.data.first end - def get_token + def authorize( ) response = HTTP::Client.exec( - "POST", - "https://id.twitch.tv/oauth2/token?client_id=#{@settings["client_id"].not_nil!}&client_secret=#{@settings["client_secret"].not_nil!}&grant_type=client_credentials", + "GET", + "https://id.twitch.tv/oauth2/authorize?client_id=#{@settings["client_id"].not_nil!}&scope=#{ @settings["scopes"].split("\n").join("%20") }&redirect_uri=#{@settings["redirect_uri"]}&response_type=code", tls: SSL_CONTEXT ) - - access_token = Token.from_json(response.body).access_token - - @token = "Bearer #{access_token}" + response.headers + end + def get_token( code : String ) + request("POST", Api::Oauth2, "/token?client_id=#{@settings["client_id"].not_nil!}&client_secret=#{@settings["client_secret"].not_nil!}&code=#{code}&grant_type=authorization_code&redirect_uri=#{@settings["redirect_uri"]}" ) end end |