sung.util¶
Utility functions for the sung package.
Module Attributes
Environment variable naming the file that caches the Spotify OAuth token. |
Functions
|
Convert a Spotify track key between different formats. |
|
|
|
Convert a date string to a standard format (YYYY-MM-DD). |
Where the Spotify OAuth token is cached: |
|
|
|
|
|
|
|
|
|
|
Ensure that a playlist ID is in the correct format. |
|
|
|
Get the value of a configuration variable. |
|
Get a Spotify client. |
|
|
|
A |
|
|
|
|
|
The |
|
Returns a copy of df with given columns in the back, in the order given. |
|
Returns a copy of df with given columns in the front, in the order given. |
|
|
|
Return a new dictionary with all string values stripped. |
- sung.util.TOKEN_CACHE_ENV = 'SUNG_SPOTIFY_TOKEN_CACHE'¶
Environment variable naming the file that caches the Spotify OAuth token.
- sung.util.cast_track_key(track_key, target_kind='uri', *, src_kind=None)[source]¶
Convert a Spotify track key between different formats.
- Parameters:
convert (- track_key - the track key to)
to (- target_kind - the format to convert)
key (- src_kind - the format of the input track)
- Return type:
- Returns:
The track key in the target format.
Examples
>>> cast_track_key("4iV5W9uYEdYUVa79Axb7Rh") 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key("spotify:track:4iV5W9uYEdYUVa79Axb7Rh", target_kind="id") '4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key("4iV5W9uYEdYUVa79Axb7Rh", "url") 'https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key('https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh', "href") 'https://api.spotify.com/v1/tracks/4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key("4iV5W9uYEdYUVa79Axb7Rh", "uri") 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh'
- sung.util.convert_date(date_str, read_formats=('%Y-%m-%d', '%Y', '%Y-%m'))[source]¶
Convert a date string to a standard format (YYYY-MM-DD).
- sung.util.default_token_cache_path()[source]¶
Where the Spotify OAuth token is cached:
$SUNG_SPOTIFY_TOKEN_CACHE, else~/.local/share/sung/spotify_token.json.Never the current directory: spotipy’s own default is
.cachein the working directory, which leaves a refresh token in every folder sung is run from (including synced and shared ones).>>> default_token_cache_path().endswith("spotify_token.json") or bool(os.environ.get(TOKEN_CACHE_ENV)) True
- Return type:
- sung.util.ensure_playlist_id(playlist_spec)[source]¶
Ensure that a playlist ID is in the correct format.
- Parameters:
id (- playlist_spec - a playlist)
uri
href (url or)
- Return type:
- Returns:
The playlist ID. Query strings (e.g.
?si=...) are dropped; a string matching no known format is returned as is (assumed to be an id).
Examples
>>> ensure_playlist_id("37i9dQZF1DXcBWIGoYBM5M") '37i9dQZF1DXcBWIGoYBM5M' >>> ensure_playlist_id("spotify:playlist:37i9dQZF1DXcBWIGoYBM5M") '37i9dQZF1DXcBWIGoYBM5M' >>> ensure_playlist_id("https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M") '37i9dQZF1DXcBWIGoYBM5M' >>> ensure_playlist_id("https://api.spotify.com/v1/playlists/37i9dQZF1DXcBWIGoYBM5M") '37i9dQZF1DXcBWIGoYBM5M' >>> ensure_playlist_id("https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M?si=abc") '37i9dQZF1DXcBWIGoYBM5M'
- sung.util.ensure_track_id(track_key, *, target_kind='id', src_kind=None)¶
Convert a Spotify track key between different formats.
- Parameters:
convert (- track_key - the track key to)
to (- target_kind - the format to convert)
key (- src_kind - the format of the input track)
- Return type:
- Returns:
The track key in the target format.
Examples
>>> cast_track_key("4iV5W9uYEdYUVa79Axb7Rh") 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key("spotify:track:4iV5W9uYEdYUVa79Axb7Rh", target_kind="id") '4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key("4iV5W9uYEdYUVa79Axb7Rh", "url") 'https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key('https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh', "href") 'https://api.spotify.com/v1/tracks/4iV5W9uYEdYUVa79Axb7Rh' >>> cast_track_key("4iV5W9uYEdYUVa79Axb7Rh", "uri") 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh'
- sung.util.get_spotify_client(client=None, *, ensure_scope='', scope='', auth=None, requests_session=True, client_credentials_manager=None, oauth_manager=None, auth_manager=None, proxies=None, requests_timeout=5, status_forcelist=None, retries=3, status_retries=3, backoff_factor=0.3, language=None, client_id=None, client_secret=None, redirect_uri=None, state=None, cache_path=None, username=None, show_dialog=False, open_browser=True, cache_handler=None)[source]¶
Get a Spotify client.
- Parameters:
client (- **kwargs - additional arguments to pass to the Spotify)
auth_manager
client
client
The purpose of having scope and ensure_scope is to allow you to ensure some needed scopes exist when a code block is asking for a Spotify client with it’s own scope desires.
- sung.util.get_spotify_oauth_creds(**oauth_kwargs)[source]¶
A
SpotifyOAuth; its token cache defaults todefault_token_cache_path().Pass
cache_handlerorcache_pathto choose another place.- Return type:
SpotifyOAuth
- sung.util.model_field_descriptions(model)[source]¶
The
{field_name: description}of a pydantic model (blank ones skipped).>>> from pydantic import BaseModel, Field >>> class M(BaseModel): ... a: int = Field(0, description=" the a ") ... b: int = 0 >>> model_field_descriptions(M) {'a': 'the a'}
- sung.util.move_columns_to_back(df, columns, *, allow_excess=True)[source]¶
Returns a copy of df with given columns in the back, in the order given.
- Parameters:
modify (- df - the DataFrame to)
back (- columns - the columns to move to the)
DataFrame (- allow_excess - whether to allow columns not in the)
- Return type:
DataFrame- Returns:
The modified DataFrame.
- sung.util.move_columns_to_front(df, columns, *, allow_excess=True)[source]¶
Returns a copy of df with given columns in the front, in the order given.
- Parameters:
modify (- df - the DataFrame to)
front (- columns - the columns to move to the)
DataFrame (- allow_excess - whether to allow columns not in the)
- Return type:
DataFrame- Returns:
The modified DataFrame.