Conversions

Conversions between Spotify IDs, URIs and URLs.

to_uri

Convert an ID to an URI of the appropriate type.

to_url

Convert an ID to an URL of the appropriate type.

from_uri

Parse type and ID from an URI.

from_url

Parse type and ID from an URL.

ConversionError

Error in conversion.

IdentifierType

Valid types of Spotify IDs.

check_id

Validate resource ID to be base 62.

check_type

Validate type of an ID.

import tekore as tk

# Create ULR for opening an album in the browser
mountain = '3RBULTZJ97bvVzZLpxcB0j'
m_url = tk.to_url('album', mountain)

# Parse input
type_, id_ = tk.from_url(m_url)
print(f'Got type `{type_}` with ID `{id_}`')
tekore.check_id(id_)

Validate resource ID to be base 62.

Note that user IDs can have special characters, so they cannot be validated.

Raises:

ConversionError – When ID is invalid.

Parameters:

id_ (str) –

Return type:

None

tekore.check_type(type_)

Validate type of an ID.

Raises:

ConversionError – When type is invalid.

Parameters:

type_ (str | IdentifierType) –

Return type:

None

class tekore.ConversionError

Bases: Exception

Error in conversion.

tekore.from_uri(uri)

Parse type and ID from an URI.

Parameters:

uri (str) – URI to parse

Returns:

type and ID parsed from the URI

Return type:

Tuple[str, str]

Raises:

ConversionError – On invalid format, prefix, type or ID.

tekore.from_url(url)

Parse type and ID from an URL.

Any parameters in the URL will be ignored.

Parameters:

url (str) – URL to parse

Returns:

type and ID parsed from the URL

Return type:

Tuple[str, str]

Raises:

ConversionError – On invalid format, prefix, type or ID.

tekore.to_uri(type_, id_)

Convert an ID to an URI of the appropriate type.

Parameters:
Returns:

converted URI

Return type:

str

Raises:

ConversionError – On invalid type or ID.

tekore.to_url(type_, id_)

Convert an ID to an URL of the appropriate type.

Parameters:
Returns:

converted URL

Return type:

str

Raises:

ConversionError – On invalid type or ID.

class tekore.IdentifierType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: StrEnum

Valid types of Spotify IDs.

album = 'album'
artist = 'artist'
episode = 'episode'
playlist = 'playlist'
show = 'show'
track = 'track'
user = 'user'