From 148276dd7f3283fd5d9417a9f8af75ad57e44255 Mon Sep 17 00:00:00 2001 From: "Bendik R. Brenne" Date: Fri, 26 Jul 2024 17:49:56 +0200 Subject: [PATCH] feat(api): add get_podcasts method for batch retrieval MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a new method `get_podcasts` to the `NrkPodcastAPI` class, allowing for batch retrieval of multiple podcasts. The CLI has been updated to support fetching multiple podcasts in a single command. Changes include: • New `get_podcasts` method in api.py using `asyncio.gather` for concurrent requests • Modified `get_podcast` CLI command to accept multiple podcast IDs • Updated CLI help text and function description for `get_podcast` • Adjusted `get_podcast` CLI function to use the new batch retrieval method --- nrk_psapi/api.py | 4 ++++ nrk_psapi/cli.py | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/nrk_psapi/api.py b/nrk_psapi/api.py index 1700195..9b449cb 100644 --- a/nrk_psapi/api.py +++ b/nrk_psapi/api.py @@ -178,6 +178,10 @@ async def get_podcast(self, podcast_id: str) -> Podcast: result = await self._request(f"radio/catalog/podcast/{podcast_id}") return Podcast.from_dict(result) + async def get_podcasts(self, podcast_ids: list[str]) -> list[Podcast]: + results: list[Podcast] = await asyncio.gather(*[self.get_podcast(podcast_id) for podcast_id in podcast_ids]) + return results + async def get_podcast_episodes(self, podcast_id: str) -> list[Episode]: result = await self._request_paged_all( f"radio/catalog/podcast/{podcast_id}/episodes", diff --git a/nrk_psapi/cli.py b/nrk_psapi/cli.py index 8749e7a..9511e57 100644 --- a/nrk_psapi/cli.py +++ b/nrk_psapi/cli.py @@ -22,8 +22,8 @@ def main_parser() -> argparse.ArgumentParser: get_podcasts_parser = subparsers.add_parser('get_all_podcasts', description='Get all podcasts.') get_podcasts_parser.set_defaults(func=get_all_podcasts) - get_podcast_parser = subparsers.add_parser('get_podcast', description='Get podcast.') - get_podcast_parser.add_argument('podcast_id', type=str, help='The podcast id.') + get_podcast_parser = subparsers.add_parser('get_podcast', description='Get podcast(s).') + get_podcast_parser.add_argument('podcast_id', type=str, nargs="+", help='The podcast id(s).') get_podcast_parser.set_defaults(func=get_podcast) get_episode_parser = subparsers.add_parser('get_episode', description='Get episode.') @@ -53,10 +53,11 @@ async def get_all_podcasts(): async def get_podcast(args): - """Get podcast.""" + """Get podcast(s).""" async with NrkPodcastAPI() as client: - podcast = await client.get_podcast(args.podcast_id) - rprint(podcast) + podcasts = await client.get_podcasts(args.podcast_id) + for podcast in podcasts: + rprint(podcast) async def get_episode(args):