Utils

ayt_api.utils.extract_video_id(url: str) str | None[source]

This should work for every url listed here: https://gist.github.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486#file-activeyoutubeurlformats-txt and more such as i.ytimg.com urls.

Parameters:

url (str) – The url to strip the id from.

Returns:

The video id with the rest of the url removed.

Return type:

Optional[str]

ayt_api.utils.extract_playlist_id(url: str) str | None[source]

This should work for every url listed here: https://github.com/Revnoplex/ayt-api/blob/main/test-playlist-urls.txt Don’t expect this to work on YouTube mixes.

Parameters:

url (str) – The url to strip the id from.

Returns:

The playlist id with the rest of the url remove.

Return type:

Optional[str]

ayt_api.utils.extract_channel_id(url: str) str | None[source]

This should work for every url listed here: https://github.com/Revnoplex/ayt-api/blob/main/test-channel-urls.txt

Parameters:

url (str) – The url to strip the id from.

Returns:

The channel id with the rest of the url removed.

Return type:

Optional[str]

ayt_api.utils.extract_comment_id(url: str) str | None[source]

This should work for every url listed here: https://github.com/Revnoplex/ayt-api/blob/main/test-comment-urls.txt

Parameters:

url (str) – The url to strip the id from.

Returns:

The comment id with the rest of the url removed.

Return type:

Optional[str]

ayt_api.utils.id_str_to_int(youtube_id: str) int[source]

Converts a base 64 YouTube ID string into an integer.

Parameters:

youtube_id (str) – The YouTube ID as a base 64 string.

Returns:

The YouTube ID as an integer.

Return type:

int

Raises:

ValueError – There were invalid characters in the YouTube ID.

ayt_api.utils.camel_to_snake(string: str) str[source]

Converts words in the camel case convention to the snake case convention.

e.g. Converts fooBar to foo_bar.

Parameters:

string (str) – The words in the camel case convention.

Returns:

The words in the snake case convention.

Return type:

str

ayt_api.utils.snake_to_camel(string: str) str[source]

Converts words in the snake case convention to the camel case convention.

e.g. Converts foo_bar to fooBar.

Parameters:

string (str) – The words in the snake case convention.

Returns:

The words in the camel case convention.

Return type:

str

ayt_api.utils.snake_keys(dictionary: dict) dict[source]

Converts keys in a dictionary from camel case to snake case.

Parameters:

dictionary (dict) – The dictionary with keys using the camel case convention.

Returns:

The dictionary with keys using the snake case convention.

Return type:

dict

ayt_api.utils.censor_key(call_url: str) str[source]

Censors the api key in an api call url.

Parameters:

call_url (str) – The api call url containing the uncensored api key.

Returns:

The url with the api key censored.

Return type:

str

ayt_api.utils.censor_token(call_url: str) str[source]

Alias of censor_key

Deprecated since version 0.4.0: Use censor_key() instead

Parameters:

call_url (str) – The api call url containing the uncensored api key.

Returns:

The url with the api key censored.

Return type:

str

ayt_api.utils.basic_html_page(title: str, description: str) str[source]

Builds a basic html page

Added in version 0.4.0.

This is used in ayt_api.api.AsyncYoutubeAPI.with_oauth_flow_generator()

Parameters:
  • title (str) – The title and heading for the page

  • description (str) – The description that will be displayed on the page

Returns:

The html page

Return type:

str

ayt_api.utils.use_existing(existing_value: Any, argument: Any) Any[source]

A check used in the updated functions to decide when to use the existing value if the argument has a value of EXISTING or use the value of the argument.

Added in version 0.4.0.

Parameters:
  • existing_value (Any) – The existing value that will be used if argument is EXISTING.

  • argument (Any) – The value to overwrite existing_value if not EXISTING.

Returns:

The existing value or argument.

Return type:

Any

ayt_api.utils.ensure_missing_keys(original: dict, minimised: dict) dict[source]

Ensure a dictionary with possible missing keys from the first dictionary includes them if the value for the key was None or an empty value.

Added in version 0.4.0.

Note

This util will only check the first layer of keys and will not check any deeper nested keys.

Parameters:
  • original (dict) – The original dictionary with the full set of keys.

  • minimised (dict) – The version of the dictionary that had keys with empty values removed.

Returns:

The minimised version of the dictionary with values added back from the original depending on if they

were empty values.

Return type:

dict