From 4846cbb9d354c14ccc831e6ff6f0df31b6ec3aa0 Mon Sep 17 00:00:00 2001 From: Calvin Date: Sat, 7 Dec 2024 16:58:32 +0100 Subject: [PATCH] made summary field optional --- src/hoyolabrssfeeds/hoyolab.py | 4 +++- src/hoyolabrssfeeds/loaders.py | 4 +++- src/hoyolabrssfeeds/models.py | 2 +- src/hoyolabrssfeeds/writers.py | 7 +++++-- tests/conftest.py | 6 +++--- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/hoyolabrssfeeds/hoyolab.py b/src/hoyolabrssfeeds/hoyolab.py index 531b04c..095a5d1 100644 --- a/src/hoyolabrssfeeds/hoyolab.py +++ b/src/hoyolabrssfeeds/hoyolab.py @@ -205,11 +205,13 @@ async def get_feed_item( "title": post["post"]["subject"], "author": post["user"]["nickname"], "content": post["post"]["content"], - "summary": post["post"]["desc"], "category": post["post"]["official_type"], "published": post["post"]["created_at"], } + if "desc" in post["post"] and len(post["post"]["desc"]) > 0: + item["summary"] = post["post"]["desc"] + if post["last_modify_time"] > 0: item["updated"] = post["last_modify_time"] diff --git a/src/hoyolabrssfeeds/loaders.py b/src/hoyolabrssfeeds/loaders.py index 9b3aadb..0dcbd8b 100644 --- a/src/hoyolabrssfeeds/loaders.py +++ b/src/hoyolabrssfeeds/loaders.py @@ -96,11 +96,13 @@ async def get_feed_items(self) -> List[FeedItem]: "title": item["title"], "author": item["authors"][0]["name"], "content": item["content_html"], - "summary": item["summary"], "category": category, "published": item["date_published"], } + if "summary" in item: + item_dict["summary"] = item["summary"] + if "date_modified" in item: item_dict["updated"] = item["date_modified"] diff --git a/src/hoyolabrssfeeds/models.py b/src/hoyolabrssfeeds/models.py index c98db81..82f62a1 100644 --- a/src/hoyolabrssfeeds/models.py +++ b/src/hoyolabrssfeeds/models.py @@ -104,11 +104,11 @@ class FeedItem(MyBaseModel): title: str author: str content: str - summary: str category: FeedItemCategory published: datetime updated: Optional[datetime] = None image: Optional[HttpUrl] = None + summary: Optional[str] = None class FeedItemMeta(MyBaseModel): diff --git a/src/hoyolabrssfeeds/writers.py b/src/hoyolabrssfeeds/writers.py index 392eba5..a80219b 100644 --- a/src/hoyolabrssfeeds/writers.py +++ b/src/hoyolabrssfeeds/writers.py @@ -95,10 +95,12 @@ def create_json_feed_item(item: FeedItem) -> Dict[str, Any]: "authors": [{"name": item.author}], "tags": [item.category.name.title()], "content_html": item.content, - "summary": item.summary, "date_published": item.published.astimezone().isoformat(), } + if item.summary is not None: + json_item["summary"] = item.summary + if item.updated is not None: json_item["date_modified"] = item.updated.astimezone().isoformat() @@ -212,7 +214,8 @@ def create_atom_feed_entries( item.content ) - ElementTree.SubElement(entry, "summary").text = item.summary + if item.summary is not None: + ElementTree.SubElement(entry, "summary").text = item.summary entries.append(entry) diff --git a/tests/conftest.py b/tests/conftest.py index 64345d0..ce645e2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -318,9 +318,6 @@ def validate_hoyolab_post(post: Dict[str, Any], is_full_post: bool) -> None: assert type(post["post"]["content"]) is str assert len(post["post"]["content"]) > 0 - assert type(post["post"]["desc"]) is str - assert len(post["post"]["desc"]) > 0 - assert type(post["post"]["structured_content"]) is str assert len(post["post"]["structured_content"]) > 0 @@ -329,3 +326,6 @@ def validate_hoyolab_post(post: Dict[str, Any], is_full_post: bool) -> None: assert type(post["cover_list"]) is list assert len(post["cover_list"]) >= 0 + + assert type(post["post"]["desc"]) is str + assert len(post["post"]["desc"]) >= 0