Helper functions#

async twitchAPI.helper.first(gen)#

Returns the first value of the given AsyncGenerator

Example:

user = await first(twitch.get_users())
Parameters:

gen (

Default:) – The generator from which you want the first value

Return type:

Default:

async twitchAPI.helper.limit(gen, num)#

Limits the number of entries from the given AsyncGenerator to up to num.

This example will give you the currently 5 most watched streams:

async for stream in limit(twitch.get_streams(), 5):
    print(stream.title)
Parameters:
  • gen (

    Default:) – The generator from which you want the first n values

  • num (

    Default:) – the number of entries you want

Raises:

ValueError – if num is less than 1

Return type:

Default:

twitchAPI.helper.TWITCH_API_BASE_URL: str = 'https://api.twitch.tv/helix/'#

The base url to the Twitch API endpoints

twitchAPI.helper.TWITCH_AUTH_BASE_URL: str = 'https://id.twitch.tv/oauth2/'#

The base url to the twitch authentication endpoints

twitchAPI.helper.TWITCH_PUB_SUB_URL: str = 'wss://pubsub-edge.twitch.tv'#

The url to the Twitch PubSub websocket

twitchAPI.helper.TWITCH_CHAT_URL: str = 'wss://irc-ws.chat.twitch.tv:443'#

The url to the Twitch Chat websocket

twitchAPI.helper.TWITCH_EVENT_SUB_WEBSOCKET_URL: str = 'wss://eventsub.wss.twitch.tv/ws'#

The url to the Twitch EventSub websocket

twitchAPI.helper.build_url(url, params, remove_none=False, split_lists=False, enum_value=True)#

Build a valid url string

Parameters:
  • url (

    Default:) – base URL

  • params (

    Default:) – dictionary of URL parameter

  • remove_none (

    Default:) – if set all params that have a None value get removed
    Default: False

  • split_lists (

    Default:) – if set all params that are a list will be split over multiple url parameter with the same name
    Default: False

  • enum_value (

    Default:) – if true, automatically get value string from Enum values
    Default: True

Return type:

Default:

Returns:

URL

twitchAPI.helper.get_uuid()#
Return type:

Default:

Returns a random UUID

twitchAPI.helper.build_scope(scopes)#

Builds a valid scope string from list

Parameters:

scopes (

Default:) – list of AuthScope

Return type:

Default:

Returns:

the valid auth scope string

twitchAPI.helper.fields_to_enum(data, fields, _enum, default)#

Iterates a dict or list and tries to replace every dict entry with key in fields with the correct Enum value

Parameters:
  • data (

    Default:) – dict or list

  • fields (

    Default:) – list of keys to be replaced

  • _enum (

    Default:) – Type of Enum to be replaced

  • default (

    Default:) – The default value if _enum does not contain the field value

Return type:

Default:

twitchAPI.helper.make_enum(data, _enum, default)#

Takes in a value and maps it to the given Enum. If the value is not valid it will take the default.

Parameters:
  • data (

    Default:) – the value to map from

  • _enum (

    Default:) – the Enum type to map to

  • default (

    Default:) – the default value

Return type:

Default:

twitchAPI.helper.enum_value_or_none(enum)#

Returns the value of the given Enum member or None

Parameters:

enum (

Default:) – the Enum member

Return type:

Default:

twitchAPI.helper.datetime_to_str(dt)#

ISO-8601 formats the given datetime, returns None if datetime is None

Parameters:

dt (

Default:) – the datetime to format

Return type:

Default:

twitchAPI.helper.remove_none_values(d)#

Removes items where the value is None from the dict. This returns a new dict and does not manipulate the one given.

Parameters:

d (

Default:) – the dict from which the None values should be removed

Return type:

Default:

enum twitchAPI.helper.ResultType(value)#

Bases: Enum

An enumeration.

Valid values are as follows:

RETURN_TYPE = <ResultType.RETURN_TYPE: 0>#
STATUS_CODE = <ResultType.STATUS_CODE: 1>#
TEXT = <ResultType.TEXT: 2>#
class twitchAPI.helper.RateLimitBucket#

Bases: object

Handler used for chat rate limiting

__init__(bucket_length, bucket_size, scope, logger=None)#
Parameters:
  • bucket_length (

    Default:) – time in seconds the bucket is valid for

  • bucket_size (

    Default:) – the number of entries that can be put into the bucket

  • scope (

    Default:) – the scope of this bucket (used for logging)

  • logger (

    Default:) – the logger to be used. If None the default logger is used

get_delta(num)#
Return type:

Default:

left()#
Return type:

Default:

Returns the space left in the current bucket

async put(num=1)#

Puts num usees into the current bucket and waits if rate limit is hit

Parameters:

num (

Default:) – the number of uses put into the current bucket

twitchAPI.helper.done_task_callback(logger, task)#

helper function used as a asyncio task done callback