diff --git a/examples/albums.py b/examples/albums.py deleted file mode 100755 index 2594e4f..0000000 --- a/examples/albums.py +++ /dev/null @@ -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() diff --git a/examples/label_recent_releases.py b/examples/recent_releases.py similarity index 74% rename from examples/label_recent_releases.py rename to examples/recent_releases.py index 6b226cb..f607969 100644 --- a/examples/label_recent_releases.py +++ b/examples/recent_releases.py @@ -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) diff --git a/spotify_actions/search.py b/spotify_actions/search.py index 10aa45e..371ea50 100644 --- a/spotify_actions/search.py +++ b/spotify_actions/search.py @@ -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) diff --git a/spotify_actions/temporal.py b/spotify_actions/temporal.py index 548d13b..df929f3 100644 --- a/spotify_actions/temporal.py +++ b/spotify_actions/temporal.py @@ -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.