Utils

utility functions

lyricsgenius.utils.auth_from_environment()

Gets credentials from environment variables.

Uses the following env vars: GENIUS_CLIENT_ID, GENIUS_REDIRECT_URI and GENIUS_CLIENT_SECRET.

Returns

client ID, redirect URI and client secret. Replaces variables that are not present with None.

Return type

tuple

lyricsgenius.utils.clean_str(s)

Cleans a string to help with string comparison.

Removes punctuation and returns a stripped, NFKC normalized string in lowercase.

Parameters

s (str) – A string.

Returns

Cleaned string.

Return type

str

lyricsgenius.utils.convert_to_datetime(f)

Converts argument to a datetime object.

Parameters

f (str`| :obj:`dict) – string or dictionary containing date components.

Returns

datetime object.

Return type

datetime

lyricsgenius.utils.parse_redirected_url(url, flow)

Parse a URL for parameter ‘code’/’token’.

Parameters
  • url (str) – The redirect URL.

  • flow (str) – authorization flow (‘code’ or ‘token’)

Returns

value of ‘code’/’token’.

Return type

str

Raises

KeyError – if ‘code’/’token’ is not available or has multiple values.

lyricsgenius.utils.safe_unicode(s)

Encodes and decodes string based on user’s STDOUT.

Encodes string to utf-8 and then decodes it based on the user’s STDOUT’s encoding, replacing erros in the process.

Parameters

s (str) – a string.

Returns

str

lyricsgenius.utils.sanitize_filename(f)

Removes invalid characters from file name.

Parameters

f (str) – file name to sanitize.

Returns

sanitized file name including only alphanumeric characters, spaces, dots or underlines.

Return type

str