pytwitcherapi.session.TwitchSession¶
-
class
pytwitcherapi.session.TwitchSession[source]¶ Bases:
pytwitcherapi.session.OAuthSessionSession for making requests to the twitch api
Use
TwitchSession.kraken_request(),TwitchSession.usher_request(),TwitchSession.oldapi_request()to make easier calls to the api directly.To get authorization, the user has to grant PyTwitcher access. The workflow goes like this:
- Start the login server with
TwitchSession.start_login_server(). - User should visit
TwitchSession.get_auth_url()in his browser and follow insturctions (e.g Login and Allow PyTwitcher). - Check if the session is authorized with
TwitchSession.authorized(). - Shut the login server down with
TwitchSession.shutdown_login_server().
Now you can use methods that need authorization.
Methods
__init__()Initialize a new TwitchSession authorization_url(url[, state])Form an authorization URL. close()Closes all adapters and as such the session delete(url, **kwargs)Sends a DELETE request. fetch_token(token_url[, code, ...])Generic method for fetching an access token from the token endpoint. fetch_viewers(game)Query the viewers and channels of the given game and followed_streams(*args, **kwargs)Return the streams the current user follows. get(url, **kwargs)Sends a GET request. get_adapter(url)Returns the appropriate connection adapter for the given URL. get_auth_url()Return the url for the user to authorize PyTwitcher get_channel(name)Return the channel for the given name get_channel_access_token(channel)Return the token and sig for the given channel get_chat_server(channel)Get an appropriate chat server for the given channel get_emote_picture(emote[, size])Return the picture for the given emote get_game(name)Get the game instance for a game name get_playlist(channel)Return the playlist for the given channel get_quality_options(channel)Get the available quality options for streams of the given channel get_redirect_target(resp)Receives a Response. get_stream(channel)Return the stream of the given channel get_streams([game, channels, limit, offset])Return a list of streams queried by a number of parameters get_user(name)Get the user for the given name head(url, **kwargs)Sends a HEAD request. kraken_request(method, endpoint, **kwargs)Make a request to one of the kraken api endpoints. merge_environment_settings(url, proxies, ...)Check the environment and merge it with some settings. mount(prefix, adapter)Registers a connection adapter to a prefix. new_state()Generates a state string to be used in authorizations. oldapi_request(method, endpoint, **kwargs)Make a request to one of the old api endpoints. options(url, **kwargs)Sends a OPTIONS request. patch(url[, data])Sends a PATCH request. post(url[, data, json])Sends a POST request. prepare_request(request)Constructs a PreparedRequestfor transmission and returns it.put(url[, data])Sends a PUT request. query_login_user(*args, **kwargs)Query and return the currently logined user rebuild_auth(prepared_request, response)When being redirected we may want to strip authentication from the request to avoid leaking credentials. rebuild_method(prepared_request, response)When being redirected we may want to change the method of the request based on certain specs or browser behavior. rebuild_proxies(prepared_request, proxies)This method re-evaluates the proxy configuration by considering the environment variables. refresh_token(token_url[, refresh_token, ...])Fetch a new access token using a refresh token. register_compliance_hook(hook_type, hook)Register a hook for request/response tweaking. request(method, url, **kwargs)Constructs a requests.Request, prepares it and sends it.resolve_redirects(resp, req[, stream, ...])Receives a Response. search_channels(query[, limit, offset])Search for channels and return them search_games(query[, live])Search for games that are similar to the query search_streams(query[, hls, limit, offset])Search for streams and return them send(request, **kwargs)Send a given PreparedRequest. shutdown_login_server()Shutdown the login server and thread start_login_server()Start a server that will get a request from a user logging in. token_from_fragment(authorization_response)Parse token from the URI fragment, used by MobileApplicationClients. top_games([limit, offset])Return the current top games usher_request(method, endpoint, **kwargs)Make a request to one of the usher api endpoints. Attributes
access_tokenauthorizedBoolean that indicates whether this session has an OAuth token or not. client_idtokenReturn the oauth token -
baseurl= None¶ The baseurl that gets prepended to every request url
-
current_user= None¶ The currently logined user.
-
kraken_request(method, endpoint, **kwargs)[source]¶ Make a request to one of the kraken api endpoints.
Headers are automatically set to accept
TWITCH_HEADER_ACCEPT. Also the client id fromCLIENT_IDwill be set. The url will be constructed ofTWITCH_KRAKENURLand the given endpoint.Parameters: - method (
str) – the request method - endpoint (
str) – the endpoint of the kraken api. The base url is automatically provided. - kwargs – keyword arguments of
requests.Session.request()
Returns: a resonse object
Return type: Raises: - method (
-
usher_request(method, endpoint, **kwargs)[source]¶ Make a request to one of the usher api endpoints.
The url will be constructed of
TWITCH_USHERURLand the given endpoint.Parameters: - method (
str) – the request method - endpoint (
str) – the endpoint of the usher api. The base url is automatically provided. - kwargs – keyword arguments of
requests.Session.request()
Returns: a resonse object
Return type: Raises: - method (
-
oldapi_request(method, endpoint, **kwargs)[source]¶ Make a request to one of the old api endpoints.
The url will be constructed of
TWITCH_APIURLand the given endpoint.Parameters: - method (
str) – the request method - endpoint (
str) – the endpoint of the old api. The base url is automatically provided. - kwargs – keyword arguments of
requests.Session.request()
Returns: a resonse object
Return type: Raises: - method (
-
fetch_viewers(game)[source]¶ Query the viewers and channels of the given game and set them on the object
Returns: the given game Return type: models.GameRaises: None
-
search_games(query, live=True)[source]¶ Search for games that are similar to the query
Parameters: Returns: A list of games
Return type: listofmodels.GameinstancesRaises: None
-
top_games(limit=10, offset=0)[source]¶ Return the current top games
Parameters: Returns: a list of top games
Return type: listofmodels.GameRaises: None
-
get_game(name)[source]¶ Get the game instance for a game name
Parameters: name ( str) – the name of the gameReturns: the game instance Return type: models.Game| NoneRaises: None
-
get_channel(name)[source]¶ Return the channel for the given name
Parameters: name ( str) – the channel nameReturns: the model instance Return type: models.ChannelRaises: None
-
search_channels(query, limit=25, offset=0)[source]¶ Search for channels and return them
Parameters: Returns: A list of channels
Return type: listofmodels.ChannelinstancesRaises: None
-
get_stream(channel)[source]¶ Return the stream of the given channel
Parameters: channel ( str|models.Channel) – the channel that is broadcasting. Either name or models.Channel instanceReturns: the stream or None, if the channel is offline Return type: models.Stream| NoneRaises: None
-
get_streams(game=None, channels=None, limit=25, offset=0)[source]¶ Return a list of streams queried by a number of parameters sorted by number of viewers descending
Parameters: Returns: A list of streams
Return type: listofmodels.StreamRaises: None
-
search_streams(query, hls=False, limit=25, offset=0)[source]¶ Search for streams and return them
Parameters: Returns: A list of streams
Return type: listofmodels.StreaminstancesRaises: None
-
followed_streams(*args, **kwargs)[source]¶ Return the streams the current user follows.
Needs authorization
user_read.Parameters: Returns: A list of streams
Return type: list`of :class:`models.StreaminstancesRaises: exceptions.NotAuthorizedError
-
get_user(name)[source]¶ Get the user for the given name
Parameters: name ( str) – The usernameReturns: the user instance Return type: models.UserRaises: None
-
query_login_user(*args, **kwargs)[source]¶ Query and return the currently logined user
Returns: The user instance Return type: models.UserRaises: exceptions.NotAuthorizedError
-
get_playlist(channel)[source]¶ Return the playlist for the given channel
Parameters: channel ( models.Channel|str) – the channelReturns: the playlist Return type: m3u8.M3U8Raises: requests.HTTPErrorif channel is offline.
-
get_quality_options(channel)[source]¶ Get the available quality options for streams of the given channel
Possible values in the list:
- source
- high
- medium
- low
- mobile
- audio
Parameters: channel ( models.Channel|str) – the channel or channel nameReturns: list of quality options Return type: listofstrRaises: requests.HTTPErrorif channel is offline.
-
get_channel_access_token(channel)[source]¶ Return the token and sig for the given channel
Parameters: channel ( channel|str) – the channel or channel name to get the access token forReturns: The token and sig for the given channel Return type: ( unicode,unicode)Raises: None
-
get_chat_server(channel)[source]¶ Get an appropriate chat server for the given channel
Usually the server is irc.twitch.tv. But because of the delicate twitch chat, they use a lot of servers. Big events are on special event servers. This method tries to find a good one.
Parameters: channel ( models.Channel) – the channel with the chatReturns: the server address and port Return type: ( str,int)Raises: None
-
get_emote_picture(emote, size=1.0)[source]¶ Return the picture for the given emote
Parameters: - emote (
pytwitcherapi.chat.message.Emote) – the emote object - size (
float) – the size of the picture. Choices are: 1.0, 2.0, 3.0
Returns: A string resembling the picturedata of the emote
Return type: Raises: None
- emote (
- Start the login server with