Spotify
This module contains the configuration object for the spotify package. The attributes of this configuration object correspond with the "spotify" key of config.yaml
SpotifyConfig
Bases: BaseConfigFormatter
Configuration object for the spotify package.
Source code in djtools/spotify/config.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
__init__(*args, **kwargs)
Constructor.
Raises:
Type | Description |
---|---|
RuntimeError
|
Spotify API credentials must exit. |
RuntimeError
|
Spotify API credentials must be valid. |
RuntimeError
|
Reddit API credentials must exist. |
Source code in djtools/spotify/config.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
SubredditConfig
Bases: BaseModel
Configuration object for spotify_playlists.
Source code in djtools/spotify/config.py
66 67 68 69 70 71 72 |
|
This module is responsible for creating or updating Spotify playlists. This can be done in a couple of ways.
The first way is by using the posted output from
djtools.sync.sync_operations.upload_music
. When another user uploads music to
the Beatcloud, you may want to generate a Spotify playlist from those tracks so
they may be previewed before you decide whether or not to download them.
The second way is by querying subreddit posts. Posts are first checked to see if they are direct links to a Spotify track. If this is not the case, then the post title is parsed in an attempt to interpret it as either 'ARTIST NAME - TRACK TITLE' or 'TRACK TITLE - ARTIST NAME'. These components are then used to search the Spotify API for tracks. The resulting tracks have their title and artist fields compared with the reddit post title and are added to the respective playlist if the Levenshtein similarity passes a threshold.
async_spotify_playlists(config)
async
This function updates the contents of one or more Spotify playlists with the posts of one or more subreddits (currently only supports one subreddit per playlist).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Source code in djtools/spotify/playlist_builder.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
spotify_playlist_from_upload(config)
Generates a Spotify playlist using a Discord webhook output.
If "upload_output", a path to a text file containing the pasted output of the upload_music Discord webhook output, is provided, this is used to generate a Spotify playlist of those uploaded tracks. If this is not provided, then the clipboard contents are used instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
Output from an upload_music Discord webhook must be copied to the system's clipboard |
Source code in djtools/spotify/playlist_builder.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
spotify_playlists(config)
This function asynchronously updates Spotify playlists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Source code in djtools/spotify/playlist_builder.py
180 181 182 183 184 185 186 |
|
This module contains helper functions used by the "spotify" module.
filter_results(spotify, results, threshold, title, artist)
Helper function for applying filtering logic to find tracks that match the submission title closely enough.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spotify
|
Spotify
|
Spotify client. |
required |
results
|
List[Dict]
|
Spotify search results. |
required |
threshold
|
float
|
Minimum Levenshtein distance. |
required |
title
|
str
|
Potential title of a track. |
required |
artist
|
str
|
Potential artist of a track. |
required |
Returns:
Type | Description |
---|---|
Tuple[Dict[str, Any], float]
|
Tuple of track object and Levenshtein distance. |
Source code in djtools/spotify/helpers.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
get_playlist_ids()
Load Spotify playlist names -> IDs lookup.
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Dictionary of Spotify playlist names mapped to playlist IDs. |
Source code in djtools/spotify/helpers.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
get_reddit_client(config)
Instantiate a Reddit API client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Returns:
Type | Description |
---|---|
Reddit
|
Reddit API client. |
Source code in djtools/spotify/helpers.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
get_spotify_client(config)
Instantiate a Spotify API client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Union[BaseConfig, SpotifyConfig]
|
Configuration object. |
required |
Returns:
Type | Description |
---|---|
Spotify
|
Spotify API client. |
Source code in djtools/spotify/helpers.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
get_subreddit_posts(spotify, reddit, subreddit, config, praw_cache)
async
Filters the submissions for the provided subreddit and tries to resolve each into a Spotify track until all the submissions are parsed or the track limit has been met.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spotify
|
Spotify
|
Spotify client. |
required |
reddit
|
Reddit
|
Reddit client. |
required |
subreddit
|
SubredditConfig
|
SubredditConfig object. |
required |
config
|
BaseConfig
|
Configuration object. |
required |
praw_cache
|
Dict[str, bool]
|
Cached praw submissions. |
required |
Returns:
Type | Description |
---|---|
Tuple[List[Tuple[str]], Dict[str, Union[str, int]]]
|
List of Spotify track ("id", "name") tuples and SubredditConfig as a dictionary. |
Source code in djtools/spotify/helpers.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
populate_playlist(playlist_name, playlist_ids, spotify_username, spotify, tracks, playlist_limit=sys.maxsize, verbosity=0)
Inserts tracks into either a new playlist or an existing one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
playlist_name
|
str
|
Name of the playlist. |
required |
playlist_ids
|
Dict[str, str]
|
Lookup of playlist IDs. |
required |
spotify_username
|
str
|
Spotify user's username. |
required |
spotify
|
Spotify
|
Spotify client. |
required |
tracks
|
List[Tuple[str]]
|
List of tracks. |
required |
playlist_limit
|
Optional[int]
|
Maximum number of tracks allowed in a playlist. |
maxsize
|
verbosity
|
Optional[int]
|
Logging verbosity level. |
0
|
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Updated playlist IDs. |
Source code in djtools/spotify/helpers.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
write_playlist_ids(playlist_ids)
Write playlist IDs to file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
playlist_ids
|
Dict[str, str]
|
Dictionary of Spotify playlist names mapped to playlist IDs. |
required |
Source code in djtools/spotify/helpers.py
275 276 277 278 279 280 281 282 283 284 285 286 |
|