Consolidate album search selectors

This commit is contained in:
Bradlee Speice 2021-07-04 00:22:12 -04:00
parent bb1ae761be
commit 78009dfbe6
4 changed files with 20 additions and 46 deletions

View File

@ -1,24 +0,0 @@
# pylint: disable=missing-module-docstring, missing-function-docstring
from argparse import ArgumentParser
from spotify_actions.echo import echo_album
from spotify_actions.search import search_albums
from spotify_actions.util import read_credentials
def main() -> None:
parser = ArgumentParser()
parser.add_argument("-c", "--credentials", required=True)
parser.add_argument("search_string")
cmdline = parser.parse_args()
client = read_credentials(cmdline.config)
albums = search_albums(client, cmdline.search_string)
echo_album(albums)
if __name__ == "__main__":
main()

View File

@ -5,8 +5,8 @@ from datetime import date, timedelta
from spotify_actions.echo import echo_album
from spotify_actions.join import join_albums
from spotify_actions.search import search_albums_label
from spotify_actions.temporal import album_released_after
from spotify_actions.search import search_albums
from spotify_actions.temporal import temporal_released_after
from spotify_actions.util import read_credentials
@ -22,9 +22,9 @@ def main() -> None:
client = read_credentials(cmdline.credentials)
label_albums = [search_albums_label(client, l) for l in cmdline.label]
label_albums = [search_albums(client, label=l) for l in cmdline.label]
albums = join_albums(*label_albums)
albums_recent = album_released_after(albums, today - four_weeks)
albums_recent = temporal_released_after(albums, today - four_weeks)
echo_album(albums_recent)

View File

@ -1,7 +1,7 @@
"""
Utility methods for the Spotify query API
"""
from typing import Iterable
from typing import Iterable, Optional
from spotify_model import Paging, SearchAlbum
from spotipy import Spotify
@ -16,22 +16,20 @@ def _search_albums(client: Spotify, search_str: str) -> Paging:
return _search
def search_albums(client: Spotify, search_str: str) -> Iterable[SearchAlbum]:
def search_albums(
client: Spotify, search_str: Optional[str] = None, artist: Optional[str] = None, label: Optional[str] = None
) -> Iterable[SearchAlbum]:
"Display albums from a search string"
for item in exhaust(_search_albums(client, search_str)):
yield SearchAlbum(**item)
def search_albums_artist(client: Spotify, artist_name: str) -> Iterable[SearchAlbum]:
"Display albums from a particular artist"
for item in exhaust(_search_albums(client, f'artist:"{artist_name}"')):
yield SearchAlbum(**item)
def search_albums_label(client: Spotify, label_name: str) -> Iterable[SearchAlbum]:
"Display albums from a particular label"
for item in exhaust(_search_albums(client, f'label:"{label_name}"')):
query_items = [
search_str,
f'artist:"{artist}"' if artist is not None else None,
f'label:"{label}"' if label is not None else None,
]
query_str = " ".join([i for i in query_items if i is not None])
if not query_str:
return
for item in exhaust(_search_albums(client, query_str)):
yield SearchAlbum(**item)

View File

@ -10,7 +10,7 @@ from typing import Iterable
from spotify_model import ReleaseDatePrecision, SearchAlbum
def album_released_after(albums: Iterable[SearchAlbum], released_after: date) -> Iterable[SearchAlbum]:
def temporal_released_after(albums: Iterable[SearchAlbum], released_after: date) -> Iterable[SearchAlbum]:
"""
Filter albums to after a specific release date.