From 4836c2428bd7490ef4da2b363d76583ef565fc59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Helle=C5=9Ben?= Date: Wed, 18 Mar 2026 05:33:37 +0100 Subject: [PATCH] Add default embed settings when creating the feed --- discord_rss_bot/feeds.py | 5 +++++ discord_rss_bot/main.py | 37 ++++++++++++++++++++++++++----------- tests/conftest.py | 8 ++++++++ 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/discord_rss_bot/feeds.py b/discord_rss_bot/feeds.py index 5510516..225e7ff 100644 --- a/discord_rss_bot/feeds.py +++ b/discord_rss_bot/feeds.py @@ -1,6 +1,7 @@ from __future__ import annotations import datetime +import json import logging import os import pprint @@ -36,6 +37,7 @@ from discord_rss_bot.hoyolab_api import extract_post_id_from_hoyolab_url from discord_rss_bot.hoyolab_api import fetch_hoyolab_post from discord_rss_bot.hoyolab_api import is_c3kay_feed from discord_rss_bot.is_url_valid import is_url_valid +from discord_rss_bot.settings import default_custom_embed from discord_rss_bot.settings import default_custom_message from discord_rss_bot.settings import get_reader @@ -570,5 +572,8 @@ def create_feed(reader: Reader, feed_url: str, webhook_dropdown: str) -> None: # This is the default message that will be sent to Discord. reader.set_tag(clean_feed_url, "custom_message", default_custom_message) # pyright: ignore[reportArgumentType] + # Set the default embed tag when creating the feed + reader.set_tag(clean_feed_url, "embed", json.dumps(default_custom_embed)) + # Update the full-text search index so our new feed is searchable. reader.update_search() diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py index 594ab3e..4359f1c 100644 --- a/discord_rss_bot/main.py +++ b/discord_rss_bot/main.py @@ -657,16 +657,29 @@ async def post_embed( feed: Feed = reader.get_feed(urllib.parse.unquote(clean_feed_url)) custom_embed: CustomEmbed = get_embed(reader, feed) - custom_embed.title = title - custom_embed.description = description - custom_embed.color = color - custom_embed.image_url = image_url - custom_embed.thumbnail_url = thumbnail_url - custom_embed.author_name = author_name - custom_embed.author_url = author_url - custom_embed.author_icon_url = author_icon_url - custom_embed.footer_text = footer_text - custom_embed.footer_icon_url = footer_icon_url + # Only overwrite fields that the user provided. This prevents accidental + # clearing of previously saved embed data when the form submits empty + # values for fields the user did not change. + if title: + custom_embed.title = title + if description: + custom_embed.description = description + if color: + custom_embed.color = color + if image_url: + custom_embed.image_url = image_url + if thumbnail_url: + custom_embed.thumbnail_url = thumbnail_url + if author_name: + custom_embed.author_name = author_name + if author_url: + custom_embed.author_url = author_url + if author_icon_url: + custom_embed.author_icon_url = author_icon_url + if footer_text: + custom_embed.footer_text = footer_text + if footer_icon_url: + custom_embed.footer_icon_url = footer_icon_url # Save the data. save_embed(reader, feed, custom_embed) @@ -1047,7 +1060,9 @@ def create_html_for_feed( # noqa: C901, PLR0914 for content_item in entry.content: content: str = content_item.value - first_image = get_first_image(summary, content) + first_image, _ = get_first_image(summary, content) + + logging.getLogger("discord_rss_bot.main").info(f"main.py: entry_id={entry.id}, first_image={first_image}") text: str = replace_tags_in_text_message(entry, reader=reader) or ( "
No content available.
" diff --git a/tests/conftest.py b/tests/conftest.py index eab2847..4aa791d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,11 +4,14 @@ import os import shutil import sys import tempfile +import warnings from contextlib import suppress from pathlib import Path from typing import TYPE_CHECKING from typing import Any +from bs4 import MarkupResemblesLocatorWarning + if TYPE_CHECKING: import pytest @@ -34,6 +37,11 @@ def pytest_sessionstart(session: pytest.Session) -> None: os.environ["DISCORD_RSS_BOT_DATA_DIR"] = str(worker_data_dir) + # Tests call markdownify which may invoke BeautifulSoup on strings that look + # like URLs; that triggers MarkupResemblesLocatorWarning from bs4. Silence + # that warning during tests to avoid noisy output. + warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning) + # If modules were imported before this hook (unlikely), force them to use # the worker-specific location. settings_module: Any = sys.modules.get("discord_rss_bot.settings")