Fix minor things that broke in last commit

This commit is contained in:
2023-01-23 00:18:48 +01:00
parent 160cfdc401
commit 5a6482b583
6 changed files with 40 additions and 22 deletions

View File

@ -12,7 +12,7 @@ from discord_rss_bot.settings import get_reader
class CustomEmbed: class CustomEmbed:
title: str title: str
description: str description: str
color: str color: int
author_name: str author_name: str
author_url: str author_url: str
author_icon_url: str author_icon_url: str
@ -68,7 +68,7 @@ def try_to_replace(custom_message: str, template: str, replace_with: str) -> str
""" """
try: try:
return custom_message.replace(template, replace_with) return custom_message.replace(template, replace_with)
except TypeError: except (TypeError, AttributeError, ValueError):
return custom_message return custom_message
@ -101,6 +101,8 @@ def replace_tags_in_text_message(feed: Feed, entry: Entry) -> str:
else: else:
first_image = "" first_image = ""
entry_text: str = content or summary
list_of_replacements = [ list_of_replacements = [
{"{{feed_author}}": feed.author}, {"{{feed_author}}": feed.author},
{"{{feed_added}}": feed.added}, {"{{feed_added}}": feed.added},
@ -126,6 +128,7 @@ def replace_tags_in_text_message(feed: Feed, entry: Entry) -> str:
{"{{entry_read_modified}}": entry.read_modified}, {"{{entry_read_modified}}": entry.read_modified},
{"{{entry_summary}}": summary}, {"{{entry_summary}}": summary},
{"{{entry_summary_raw}}": entry.summary or ""}, {"{{entry_summary_raw}}": entry.summary or ""},
{"{{entry_text}}": entry_text},
{"{{entry_title}}": entry.title}, {"{{entry_title}}": entry.title},
{"{{entry_updated}}": entry.updated}, {"{{entry_updated}}": entry.updated},
{"{{image_1}}": first_image}, {"{{image_1}}": first_image},
@ -168,6 +171,8 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
else: else:
first_image = "" first_image = ""
entry_text: str = content or summary
list_of_replacements = [ list_of_replacements = [
{"{{feed_author}}": feed.author}, {"{{feed_author}}": feed.author},
{"{{feed_added}}": feed.added}, {"{{feed_added}}": feed.added},
@ -194,6 +199,7 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
{"{{entry_summary}}": summary}, {"{{entry_summary}}": summary},
{"{{entry_summary_raw}}": entry.summary or ""}, {"{{entry_summary_raw}}": entry.summary or ""},
{"{{entry_title}}": entry.title}, {"{{entry_title}}": entry.title},
{"{{entry_text}}": entry_text},
{"{{entry_updated}}": entry.updated}, {"{{entry_updated}}": entry.updated},
{"{{image_1}}": first_image}, {"{{image_1}}": first_image},
] ]
@ -202,7 +208,6 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
for template, replace_with in replacement.items(): for template, replace_with in replacement.items():
embed.title = try_to_replace(embed.title, template, replace_with) embed.title = try_to_replace(embed.title, template, replace_with)
embed.description = try_to_replace(embed.description, template, replace_with) embed.description = try_to_replace(embed.description, template, replace_with)
embed.color = try_to_replace(embed.color, template, replace_with)
embed.author_name = try_to_replace(embed.author_name, template, replace_with) embed.author_name = try_to_replace(embed.author_name, template, replace_with)
embed.author_url = try_to_replace(embed.author_url, template, replace_with) embed.author_url = try_to_replace(embed.author_url, template, replace_with)
embed.author_icon_url = try_to_replace(embed.author_icon_url, template, replace_with) embed.author_icon_url = try_to_replace(embed.author_icon_url, template, replace_with)
@ -242,10 +247,10 @@ def save_embed(custom_reader: Reader, feed: Feed, embed: CustomEmbed) -> None:
feed: The feed to set the tag in. feed: The feed to set the tag in.
embed: The embed to set. embed: The embed to set.
""" """
embed_dict: dict[str, str] = { embed_dict: dict[str, str | int] = {
"title": embed.title, "title": embed.title,
"description": embed.description, "description": embed.description,
"color": embed.color.replace("#", "").replace("0x", ""), "color": embed.color,
"author_name": embed.author_name, "author_name": embed.author_name,
"author_url": embed.author_url, "author_url": embed.author_url,
"author_icon_url": embed.author_icon_url, "author_icon_url": embed.author_icon_url,
@ -268,27 +273,24 @@ def get_embed(custom_reader: Reader, feed: Feed) -> CustomEmbed:
Returns: Returns:
Returns the contents from the embed tag. Returns the contents from the embed tag.
""" """
embed_json: dict[str, str] = {}
try: try:
embed: str = str(custom_reader.get_tag(feed, "embed")) embed: str = custom_reader.get_tag(feed, "embed") # type: ignore
except TagNotFoundError: except TagNotFoundError:
embed = "" embed = ""
except ValueError: except ValueError:
embed = "" embed = ""
if embed: if embed:
try: if type(embed) == str:
embed_json = json.loads(embed) embed_data: dict[str, str | int] = json.loads(embed)
except json.decoder.JSONDecodeError: return get_embed_data(embed_data)
embed_json = "" # type: ignore else:
return get_embed_data(embed)
if embed_json:
return get_embed_data(embed_json)
return CustomEmbed( return CustomEmbed(
title="", title="",
description="", description="",
color="", color=32896,
author_name="", author_name="",
author_url="", author_url="",
author_icon_url="", author_icon_url="",
@ -310,7 +312,7 @@ def get_embed_data(embed_data) -> CustomEmbed:
""" """
title: str = embed_data.get("title", "") title: str = embed_data.get("title", "")
description: str = embed_data.get("description", "") description: str = embed_data.get("description", "")
color: str = embed_data.get("color", "") color: int = embed_data.get("color", 32896)
author_name: str = embed_data.get("author_name", "") author_name: str = embed_data.get("author_name", "")
author_url: str = embed_data.get("author_url", "") author_url: str = embed_data.get("author_url", "")
author_icon_url: str = embed_data.get("author_icon_url", "") author_icon_url: str = embed_data.get("author_icon_url", "")

View File

@ -69,6 +69,10 @@ def create_embed_webhook(webhook_url: str, entry: Entry) -> DiscordWebhook:
discord_embed: DiscordEmbed = DiscordEmbed() discord_embed: DiscordEmbed = DiscordEmbed()
# Remove # from the color if it exists.
if type(custom_embed.color) is str and custom_embed.color.startswith("#"):
custom_embed.color = custom_embed.color[1:]
if custom_embed.title: if custom_embed.title:
discord_embed.set_title(custom_embed.title) discord_embed.set_title(custom_embed.title)
if custom_embed.description: if custom_embed.description:

View File

@ -53,7 +53,7 @@ def convert_html_to_md(html: str) -> str:
tag.replace_with(tag.text) tag.replace_with(tag.text)
# Remove all leading and trailing whitespace # Remove all leading and trailing whitespace
soup_text = soup.text soup_text: str = soup.text
return soup_text.strip() return soup_text.strip()

View File

@ -9,12 +9,10 @@ os.makedirs(data_dir, exist_ok=True)
# TODO: Add default things to the database and make the edible. # TODO: Add default things to the database and make the edible.
default_custom_message: str = "{{entry_title}}\n{{entry_link}}" default_custom_message: str = "{{entry_title}}\n{{entry_link}}"
default_custom_embed = { default_custom_embed: dict[str, str] = {
"title": "{{entry_title}}", "title": "{{entry_title}}",
"description": "{{entry_content}}", "description": "{{entry_text}}",
"url": "{{entry_link}}", "image_url": "{{image_1}}",
"image": "{{entry_image}}",
"color": 0x008080,
} }

View File

@ -195,6 +195,13 @@
{% endraw %} {% endraw %}
</code>{{entry.title}} </code>{{entry.title}}
</li> </li>
<li>
<code>
{% raw %}
{{entry_text}}
{% endraw %}
</code> Same as entry_content if it exists, otherwise entry_summary
</li>
<li> <li>
<code> <code>
{% raw %} {% raw %}

View File

@ -191,6 +191,13 @@
{% endraw %} {% endraw %}
</code>{{entry.title}} </code>{{entry.title}}
</li> </li>
<li>
<code>
{% raw %}
{{entry_text}}
{% endraw %}
</code> Same as entry_content if it exists, otherwise entry_summary
</li>
<li> <li>
<code> <code>
{% raw %} {% raw %}