|
1 | 1 | import asyncio |
2 | 2 | import logging |
3 | 3 | import types |
4 | | -from datetime import date |
| 4 | +from datetime import date, datetime |
5 | 5 |
|
6 | 6 | from aiohttp import ( |
7 | 7 | ClientSession, |
@@ -44,11 +44,37 @@ class SimpleClubSchema(BaseModel): |
44 | 44 | name: str |
45 | 45 |
|
46 | 46 |
|
| 47 | +class ClubProfileSchema(SimpleClubSchema): |
| 48 | + logo: str | None = None |
| 49 | + url: str |
| 50 | + |
| 51 | + |
47 | 52 | class ClubSearchResultSchema(BaseModel): |
48 | 53 | count: int |
49 | 54 | results: list[SimpleClubSchema] |
50 | 55 |
|
51 | 56 |
|
| 57 | +class NewsSchema(BaseModel): |
| 58 | + id: int |
| 59 | + title: str |
| 60 | + summary: str |
| 61 | + is_published: bool |
| 62 | + club: ClubProfileSchema |
| 63 | + url: str |
| 64 | + |
| 65 | + |
| 66 | +class NewsDateSchema(BaseModel): |
| 67 | + id: int |
| 68 | + start_date: datetime |
| 69 | + end_date: datetime |
| 70 | + news: NewsSchema |
| 71 | + |
| 72 | + |
| 73 | +class NewsDateResultSchema(BaseModel): |
| 74 | + count: int |
| 75 | + results: list[NewsDateSchema] |
| 76 | + |
| 77 | + |
52 | 78 | class SithClient(ClientSession): |
53 | 79 | def __init__(self): |
54 | 80 | self.logger = logging.getLogger("sith") |
@@ -83,6 +109,21 @@ async def search_clubs(self, search: str) -> list[SimpleClubSchema] | None: |
83 | 109 | except ValidationError as e: |
84 | 110 | self.logger.error(str(e)) |
85 | 111 |
|
| 112 | + async def search_news( |
| 113 | + self, after: datetime | None = None, before: datetime | None = None |
| 114 | + ) -> list[NewsDateSchema] | None: |
| 115 | + params = {"is_published": "true"} |
| 116 | + if after: |
| 117 | + params["after"] = after.isoformat() |
| 118 | + if before: |
| 119 | + params["before"] = before.isoformat() |
| 120 | + async with self.get("/api/news/date", params=params) as res: |
| 121 | + content = await res.read() |
| 122 | + try: |
| 123 | + return NewsDateResultSchema.model_validate_json(content).results |
| 124 | + except ValidationError as e: |
| 125 | + self.logger.error(str(e)) |
| 126 | + |
86 | 127 |
|
87 | 128 | async def request_logging_start( |
88 | 129 | _session: SithClient, |
|
0 commit comments