|
| 1 | +import asyncio |
| 2 | +from asyncio import TaskGroup |
| 3 | +from pprint import pprint |
| 4 | + |
| 5 | +import httpx |
| 6 | +from typing import List, Optional |
| 7 | + |
| 8 | +import pydantic |
| 9 | +from pydantic import BaseModel |
| 10 | + |
| 11 | + |
| 12 | +class SearchItem(BaseModel): |
| 13 | + category: str |
| 14 | + id: Optional[int] |
| 15 | + item_id: Optional[int] |
| 16 | + url: str |
| 17 | + title: str |
| 18 | + description: str |
| 19 | + |
| 20 | + |
| 21 | +class SearchResponse(BaseModel): |
| 22 | + elapsed_ms: float |
| 23 | + keywords: List[str] |
| 24 | + results: List[SearchItem] = pydantic.Field(default_factory=list) |
| 25 | + episodes: List[SearchItem] = pydantic.Field(default_factory=list) |
| 26 | + |
| 27 | + |
| 28 | +def cleanup(resp: SearchResponse): |
| 29 | + # Unfortunately, Talk Python's and Python Bytes' APIs vary slightly. |
| 30 | + # This method will unify them. |
| 31 | + resp.episodes = resp.episodes or resp.results |
| 32 | + resp.results = resp.episodes or resp.results |
| 33 | + |
| 34 | + for r in resp.results: |
| 35 | + r.id = r.id or r.item_id |
| 36 | + |
| 37 | + |
| 38 | +async def search_podcast(text: str, server: str) -> list[str]: |
| 39 | + search_url = f'https://{server}/api/search?q={text}' |
| 40 | + async with httpx.AsyncClient() as client: |
| 41 | + resp = await client.get(search_url) |
| 42 | + resp.raise_for_status() |
| 43 | + |
| 44 | + data = resp.json() |
| 45 | + resp = SearchResponse(**data) |
| 46 | + cleanup(resp) |
| 47 | + |
| 48 | + return [f'{r.id}: {r.title}' for r in resp.results if r.category.lower() == 'episode'] |
| 49 | + |
| 50 | + |
| 51 | +async def search(): |
| 52 | + # pydantic beanie mongodb odm <-- is a good search string |
| 53 | + text = input("What keyword to you want to look for? ").strip().lower() |
| 54 | + |
| 55 | + async with TaskGroup() as tg: |
| 56 | + tp = tg.create_task(search_podcast(text, 'search.talkpython.fm')) |
| 57 | + pb = tg.create_task(search_podcast(text, 'search.pythonbytes.fm')) |
| 58 | + |
| 59 | + # results = await asyncio.gather(tp, pb) |
| 60 | + # pprint(results) |
| 61 | + # return |
| 62 | + |
| 63 | + print("Done with search.") |
| 64 | + print() |
| 65 | + print("Results from Talk Python:") |
| 66 | + for title in tp.result(): |
| 67 | + print(f'* {title}') |
| 68 | + print() |
| 69 | + print("Results from Python Bytes:") |
| 70 | + for title in pb.result(): |
| 71 | + print(f'* {title}') |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + asyncio.run(search()) |
0 commit comments