Basic example of printing albums

This commit is contained in:
2021-07-03 19:29:50 -04:00
parent 96262e2397
commit a62cb87575
10 changed files with 884 additions and 2 deletions

13
spotify_actions/echo.py Normal file
View File

@ -0,0 +1,13 @@
"""
Methods for printing results to console; primarily useful when developing/debugging pipelines to
check results before committing.
"""
from typing import AsyncIterable
from spotify import Album
async def echo_album(albums: AsyncIterable[Album]) -> None:
"Print album metadata"
async for album in albums:
print(album.name)

13
spotify_actions/search.py Normal file
View File

@ -0,0 +1,13 @@
"""
Utility methods for the Spotify query API
"""
from typing import AsyncIterable
from spotify import Album, Client
async def search_album(client: Client, name: str) -> AsyncIterable[Album]:
"Search for a specific album by name"
results = await client.search(name, types=["album"])
for album in results.albums:
yield album

15
spotify_actions/util.py Normal file
View File

@ -0,0 +1,15 @@
"""
Utility methods for working with the Spotify API
"""
from asyncio import AbstractEventLoop
from pathlib import Path
import yaml
from spotify import Client
def read_credentials(path: Path, loop: AbstractEventLoop) -> Client:
"Read credentials from a YAML file and construct a Spotify client"
with open(path, "r") as credentials_file:
credentials = yaml.safe_load(credentials_file)
return Client(credentials["client_id"], credentials["client_secret"], loop=loop)