From 16319ed5e11878298881f8c5471553faf611f9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Fri, 13 Jan 2023 23:05:20 +0100 Subject: [PATCH] You can now customize the message sent to Discord --- discord_rss_bot/custom_message.py | 95 +++++++++++++++++++++++++++ discord_rss_bot/feeds.py | 9 ++- discord_rss_bot/main.py | 59 +++++++++++++++++ discord_rss_bot/templates/custom.html | 70 ++++++++++++++++++++ discord_rss_bot/templates/feed.html | 4 +- 5 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 discord_rss_bot/custom_message.py create mode 100644 discord_rss_bot/templates/custom.html diff --git a/discord_rss_bot/custom_message.py b/discord_rss_bot/custom_message.py new file mode 100644 index 0000000..c8f1441 --- /dev/null +++ b/discord_rss_bot/custom_message.py @@ -0,0 +1,95 @@ +from reader import Entry, Feed, Reader, TagNotFoundError + +from discord_rss_bot.settings import get_reader + + +def try_to_replace(custom_message: str, template: str, replace_with: str) -> str: + """Try to replace a tag in custom_message. + + Args: + custom_message: The custom_message to replace tags in. + feed: The feed to get the tags from. + entry: The entry to get the tags from. + tag: The tag to replace. + + Returns: + Returns the custom_message with the tag replaced. + """ + if not template: + return custom_message + if not replace_with: + return custom_message + try: + print(f"custom_message: {custom_message}") + print(f"template: {template}") + print(f"replace_with: {replace_with}") + return custom_message.replace(template, replace_with) + except TypeError: + return custom_message + + +def replace_tags(feed: Feed, entry: Entry) -> str: + """Replace tags in custom_message. + + Args: + feed: The feed to get the tags from. + entry: The entry to get the tags from. + + Returns: + Returns the custom_message with the tags replaced. + """ + custom_reader: Reader = get_reader() + custom_message: str = get_custom_message(feed=feed, custom_reader=custom_reader) + + list_of_replacements = [ + {"{{feed_author}}": feed.author}, + {"{{feed_added}}": feed.added}, + {"{{feed_last_exception}}": feed.last_exception}, + {"{{feed_last_updated}}": feed.last_updated}, + {"{{feed_link}}": feed.link}, + {"{{feed_subtitle}}": feed.subtitle}, + {"{{feed_title}}": feed.title}, + {"{{feed_updated}}": feed.updated}, + {"{{feed_updates_enabled}}": str(feed.updates_enabled)}, + {"{{feed_url}}": feed.url}, + {"{{feed_user_title}}": feed.user_title}, + {"{{feed_version}}": feed.version}, + {"{{entry_added}}": entry.added}, + {"{{entry_author}}": entry.author}, + {"{{entry_content}}": entry.content}, + {"{{entry_id}}": entry.id}, + {"{{entry_important}}": str(entry.important)}, + {"{{entry_link}}": entry.link}, + {"{{entry_published}}": entry.published}, + {"{{entry_read}}": str(entry.read)}, + {"{{entry_read_modified}}": entry.read_modified}, + {"{{entry_summary}}": entry.summary}, + {"{{entry_title}}": entry.title}, + {"{{entry_updated}}": entry.updated}, + ] + + for replacement in list_of_replacements: + for template, replace_with in replacement.items(): + custom_message: str = try_to_replace(custom_message, template, replace_with) + + print(f"custom_message: {custom_message}") + return custom_message + + +def get_custom_message(custom_reader: Reader, feed: Feed) -> str: + """Get custom_message tag from feed. + + Args: + custom_reader: What Reader to use. + feed: The feed to get the tag from. + + Returns: + Returns the contents from the custom_message tag. + """ + try: + custom_message: str = custom_reader.get_tag(feed, "custom_message") # type: ignore + except TagNotFoundError: + custom_message: str = "" + except ValueError: + custom_message: str = "" + return custom_message diff --git a/discord_rss_bot/feeds.py b/discord_rss_bot/feeds.py index c22cf56..e789772 100644 --- a/discord_rss_bot/feeds.py +++ b/discord_rss_bot/feeds.py @@ -4,10 +4,10 @@ from discord_webhook import DiscordWebhook from reader import Entry, Feed, Reader from requests import Response -from discord_rss_bot import settings +from discord_rss_bot import custom_message, settings from discord_rss_bot.filter.blacklist import should_be_skipped -from discord_rss_bot.settings import get_reader from discord_rss_bot.filter.whitelist import has_white_tags, should_be_sent +from discord_rss_bot.settings import get_reader def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = None, do_once: bool = False) -> None: @@ -50,6 +50,11 @@ def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = Non webhook: DiscordWebhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True) + if custom_message.get_custom_message(reader, entry.feed) != "": + print("Custom message found, replacing tags.") + webhook.content = custom_message.replace_tags(entry=entry, feed=entry.feed) + + print(f"Webhook content: {webhook.content}") if feed is not None and has_white_tags(reader, feed): # Only send the entry if it is whitelisted, otherwise, mark it as read and continue. if should_be_sent(reader, entry): diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py index f8663c5..c351501 100644 --- a/discord_rss_bot/main.py +++ b/discord_rss_bot/main.py @@ -12,6 +12,7 @@ from reader import Entry, EntryCounts, EntrySearchCounts, EntrySearchResult, Fee from starlette.responses import RedirectResponse from discord_rss_bot.custom_filters import encode_url, entry_is_blacklisted, entry_is_whitelisted +from discord_rss_bot.custom_message import get_custom_message from discord_rss_bot.feeds import send_to_discord from discord_rss_bot.filter.blacklist import get_blacklist_content, get_blacklist_summary, get_blacklist_title from discord_rss_bot.filter.whitelist import get_whitelist_content, get_whitelist_summary, get_whitelist_title @@ -115,6 +116,8 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()): """ clean_feed_url: str = feed_url.strip() + # TODO: Check if the feed is valid, if not return an error or fix it. + # For example, if the feed is missing the protocol, add it. reader.add_feed(clean_feed_url) reader.update_feed(clean_feed_url) @@ -304,6 +307,62 @@ async def get_blacklist(feed_url: str, request: Request): return templates.TemplateResponse("blacklist.html", context) # type: ignore +@app.post("/custom") +async def set_custom(custom_message: str = Form(""), feed_url: str = Form()): + """ + Set the custom message, this is used when sending the message. + + Args: + custom_message: The custom message. + feed_url: The feed we should set the custom message for. + + Returns: + Redirect to the feed. + """ + + # Add the custom_message to the feed. + if custom_message: + reader.set_tag(feed_url, "custom_message", custom_message) + print(f"Set custom message for {feed_url} to {custom_message}") + else: + print(f"Removing custom message for {feed_url}") + reader.delete_tag(feed_url, "custom_message", missing_ok=True) + + # Clean URL is used to redirect to the feed page. + clean_url: str = urllib.parse.quote(feed_url) + + return RedirectResponse(url=f"/feed/?feed_url={clean_url}", status_code=303) + + +@app.get("/custom", response_class=HTMLResponse) +async def get_custom(feed_url: str, request: Request): + """Get the custom message. This is used when sending the message to Discord. + + Args: + feed_url: What feed we should get the custom message for. + request: The HTTP request. + + Returns: + custom.html + """ + + # Make feed_url a valid URL. + url: str = urllib.parse.unquote(feed_url) + + feed: Feed = reader.get_feed(url) + + # Get previous data, this is used when creating the form. + custom_message: str = get_custom_message(reader, feed) + + # Get the first entry, this is used to show the user what the custom message will look like. + first_entry: Entry = reader.get_entries(feed=feed, limit=1) + for entry in first_entry: + first_entry = entry + + context = {"request": request, "feed": feed, "custom_message": custom_message, "entry": first_entry} + return templates.TemplateResponse("custom.html", context) # type: ignore + + @app.get("/add", response_class=HTMLResponse) def get_add(request: Request): """ diff --git a/discord_rss_bot/templates/custom.html b/discord_rss_bot/templates/custom.html new file mode 100644 index 0000000..e88d5a8 --- /dev/null +++ b/discord_rss_bot/templates/custom.html @@ -0,0 +1,70 @@ +{% extends "base.html" %}( +{% block title %} | Custom message{% endblock %} +{% block content %} +
+
+ +
+ +
+
+
    +
  • You can modify the message that is sent to Discord.
  • +
    +
  • {% raw %}{{feed_url}}{% endraw %} will be replaced with the feed URL. You can use \n for new lines.
  • +
    +
  • {% raw %}{{feed_author}}{% endraw %} - {{feed.author}}
  • +
  • {% raw %}{{feed_added}}{% endraw %} - {{feed.added}}
  • +
  • {% raw %}{{feed_last_exception}}{% endraw %} - {{feed.last_exception}}
  • +
  • {% raw %}{{feed_last_updated}}{% endraw %} - {{feed.last_updated}}
  • +
  • {% raw %}{{feed_link}}{% endraw %} - {{feed.link}}
  • +
  • {% raw %}{{feed_subtitle}}{% endraw %} - {{feed.subtitle}}
  • +
  • {% raw %}{{feed_title}}{% endraw %} - {{feed.title}}
  • +
  • {% raw %}{{feed_updated}}{% endraw %} - {{feed.updated}}
  • +
  • {% raw %}{{feed_updates_enabled}}{% endraw %} - {{feed.updates_enabled}}
  • +
  • {% raw %}{{feed_url}}{% endraw %} - {{feed.url}}
  • +
  • {% raw %}{{feed_user_title}}{% endraw %} - {{feed.user_title}}
  • +
  • {% raw %}{{feed_version}}{% endraw %} - {{feed.version}}
  • +
    +
  • {% raw %}{{entry_added}}{% endraw %} - {{entry.added}}
  • +
  • {% raw %}{{entry_author}}{% endraw %} - {{entry.author}}
  • +
  • {% raw %}{{entry_content}}{% endraw %} - {{entry.content[0].value}}
  • +
  • {% raw %}{{entry_id}}{% endraw %} - {{entry.id}}
  • +
  • {% raw %}{{entry_important}}{% endraw %} - {{entry.important}}
  • +
  • {% raw %}{{entry_link}}{% endraw %} - {{entry.link}}
  • +
  • {% raw %}{{entry_published}}{% endraw %} - {{entry.published}}
  • +
  • {% raw %}{{entry_read}}{% endraw %} - {{entry.read}}
  • +
  • {% raw %}{{entry_read_modified}}{% endraw %} - {{entry.read_modified}}
  • +
  • {% raw %}{{entry_summary}}{% endraw %} - {{entry.summary}}
  • +
  • {% raw %}{{entry_title}}{% endraw %} - {{entry.title}}
  • +
  • {% raw %}{{entry_updated}}{% endraw %} - {{entry.updated}}
  • +
+
    +
  • Examples:
  • +
  • {% raw %}Hello {{entry_author}}\n{{feed_title}}\n{{entry_read}}{% endraw %}
  • +
    +
  • Will become:
  • +
  • + Hello {{entry.author}} + {{feed.title}} + {{entry.read}} +
  • +
+
+ + + +
+
+ + + + + +
+ +
+
+
+{% endblock %} \ No newline at end of file diff --git a/discord_rss_bot/templates/feed.html b/discord_rss_bot/templates/feed.html index f5fee11..48efc1f 100644 --- a/discord_rss_bot/templates/feed.html +++ b/discord_rss_bot/templates/feed.html @@ -26,6 +26,7 @@ {% endif %} Whitelist Blacklist + Custom message {% for entry in entries %} @@ -46,7 +47,8 @@ {% if entry.published %} @ {{ entry.published.strftime('%Y-%m-%d, %T') }} {% endif %} - + + {# TODO: Only show one if both are the same #} {% if entry.summary %}
Summary