Skip to content

Commit

Permalink
feat(api): add get_podcasts method for batch retrieval
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bendikrb committed Jul 26, 2024
1 parent fcdc84e commit 148276d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions nrk_psapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions nrk_psapi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 148276d

Please sign in to comment.