API Reference
Main modules
Cli
fetch_recently_played(spotify_client, num_tracks=5)
Fetch recently played tracks for a user using spotipy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spotify_client
|
Spotify
|
Authenticated spotipy client. |
required |
num_tracks
|
int
|
Number of tracks to fetch (max 50). Defaults to 5. |
5
|
Returns:
Type | Description |
---|---|
List[Dict]
|
A list of track dictionaries with relevant information including |
List[Dict]
|
name, artist, album, popularity, external_urls, preview_url, |
List[Dict]
|
played_at timestamp, and album_cover_url. |
Raises:
Type | Description |
---|---|
Exception
|
If the Spotify API request fails or returns invalid data. |
Source code in spoteamfy/src/cli.py
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
format_tracks_for_teams(username, tracks)
Format track information as an adaptive card for Teams webhook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
Spotify username to include in the message. |
required |
tracks
|
List[Dict]
|
List of track dictionaries to format. |
required |
Returns:
Type | Description |
---|---|
Dict
|
A Teams message payload dictionary with an adaptive card containing |
Dict
|
formatted track information and album cover art from the most recent track. |
Dict
|
If no tracks are provided, returns a simple message card. |
Source code in spoteamfy/src/cli.py
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 220 221 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
get_users_json_path(cli_path=None)
Determine the path to the users.json file.
Priority: CLI argument > .env USERS_JSON_PATH > default ./config/users.json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cli_path
|
str
|
Optional path provided via CLI argument. |
None
|
Returns:
Type | Description |
---|---|
str
|
The resolved path to the users.json file. |
Source code in spoteamfy/src/cli.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
get_webhook_url(cli_webhook=None)
Determine the webhook URL to use.
Priority: CLI argument > .env WEBHOOK_URL > raise error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cli_webhook
|
str
|
Optional webhook URL provided via CLI argument. |
None
|
Returns:
Type | Description |
---|---|
str
|
The resolved webhook URL. |
Raises:
Type | Description |
---|---|
ValueError
|
If no webhook URL is provided via CLI or environment variable. |
Source code in spoteamfy/src/cli.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
load_users_from_json(json_path)
Load user credentials from a JSON file.
Each user must have: username, client_id, client_secret, redirect_uri, refresh_token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_path
|
str
|
Path to the JSON file containing user credentials. |
required |
Returns:
Type | Description |
---|---|
List[Dict]
|
A list of dictionaries containing user credential information. |
Raises:
Type | Description |
---|---|
ValueError
|
If a user entry is missing required keys. |
FileNotFoundError
|
If the JSON file cannot be found. |
JSONDecodeError
|
If the JSON file is malformed. |
Source code in spoteamfy/src/cli.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
main(num_tracks, users_json, teams_webhook)
CLI entry point for Spotify Teams utility.
Fetches recently played tracks from Spotify for configured users and posts formatted messages to Microsoft Teams via webhook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_tracks
|
int
|
Number of recently played tracks to fetch per user. |
required |
users_json
|
Optional[str]
|
Path to JSON file containing user credentials. |
required |
teams_webhook
|
Optional[str]
|
Teams webhook URL for posting messages. |
required |
Source code in spoteamfy/src/cli.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
|
post_to_teams(webhook_url, message)
Post message to Microsoft Teams using webhook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
webhook_url
|
str
|
Teams webhook URL to post to. |
required |
message
|
Dict
|
Message payload dictionary to send. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the message was posted successfully, False otherwise. |
Source code in spoteamfy/src/cli.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
Spotify auth
SpotifyAuthError
Bases: Exception
Custom exception for Spotify authentication errors.
Source code in spoteamfy/src/spotify_auth.py
7 8 9 10 |
|
authenticate_user(user_credentials)
Authenticate a Spotify user using Spotipy and return a Spotipy client instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_credentials
|
Dict[str, Any]
|
Dictionary containing user authentication credentials. Must include 'client_id', 'client_secret', 'redirect_uri', and 'refresh_token' keys. |
required |
Returns:
Type | Description |
---|---|
Spotify
|
An authenticated spotipy.Spotify client instance. |
Raises:
Type | Description |
---|---|
SpotifyAuthError
|
If authentication fails due to invalid credentials, missing required fields, or API errors. |
Source code in spoteamfy/src/spotify_auth.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 76 77 78 |
|
Utility scripts
Get access token
Script to fetch Spotify access tokens using spotipy. This script handles both getting initial refresh tokens and refreshing access tokens.
get_client_credentials_token(client_id, client_secret)
Get an access token using Client Credentials Flow (app-only, no user context).
This is useful for app-only requests but won't work for user-specific data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Spotify app client ID. |
required |
client_secret
|
str
|
Spotify app client secret. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
Access token string if successful, None otherwise. |
Source code in scripts/get_access_token.py
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 |
|
get_initial_auth_for_user(username, client_id, client_secret, redirect_uri)
Get initial authorization for a user using Authorization Code Flow.
This will open a browser and require user interaction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
The username for display purposes. |
required |
client_id
|
str
|
Spotify app client ID. |
required |
client_secret
|
str
|
Spotify app client secret. |
required |
redirect_uri
|
str
|
Redirect URI configured in Spotify app. |
required |
Returns:
Type | Description |
---|---|
Optional[Dict]
|
Token info dictionary containing access_token, refresh_token, expires_at, |
Optional[Dict]
|
and scope if successful, None otherwise. |
Source code in scripts/get_access_token.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
main()
Main function to handle token operations.
Provides an interactive menu for managing Spotify API tokens including getting initial authorization, refreshing tokens, testing tokens, and obtaining client credentials tokens.
Source code in scripts/get_access_token.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
refresh_access_token(client_id, client_secret, refresh_token)
Refresh an access token using a refresh token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Spotify app client ID. |
required |
client_secret
|
str
|
Spotify app client secret. |
required |
refresh_token
|
str
|
Valid refresh token to use for getting new access token. |
required |
Returns:
Type | Description |
---|---|
Optional[Dict]
|
Refreshed token info dictionary if successful, None otherwise. |
Source code in scripts/get_access_token.py
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 |
|
test_access_token(access_token)
Test if an access token works by making a simple API call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
str
|
Access token to test. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the token is valid and works, False otherwise. |
Source code in scripts/get_access_token.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
Auth validator
Simple authentication validation script for Spotify users.
validate_user_auth(username)
Validate authentication for a specific user and fetch their profile info.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
The username to validate authentication for. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if authentication is successful, False otherwise. |
Source code in scripts/auth_validator.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 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 |
|