diff --git a/discord_rss_bot/feeds.py b/discord_rss_bot/feeds.py index 67ea569..d9b7248 100644 --- a/discord_rss_bot/feeds.py +++ b/discord_rss_bot/feeds.py @@ -83,8 +83,7 @@ def add_feed(feed_url: str, webhook: str, exist_ok=False, allow_invalid_url=Fals Returns: IfFeedError: Error or not. """ - # Remove spaces from feed_url - feed_url = feed_url.strip() + logger.debug(f"Adding feed: {feed_url}") try: reader.add_feed(feed_url, exist_ok=exist_ok, allow_invalid_url=allow_invalid_url) @@ -98,7 +97,6 @@ def add_feed(feed_url: str, webhook: str, exist_ok=False, allow_invalid_url=Fals webhook=webhook, exception=error.message, ) - except InvalidFeedURLError as error: error_msg = "Invalid feed URL" logger.error(error_msg, exc_info=True) @@ -109,6 +107,7 @@ def add_feed(feed_url: str, webhook: str, exist_ok=False, allow_invalid_url=Fals webhook=webhook, exception=error.message, ) + logger.debug(f"Successfully added feed: {feed_url}") return IfFeedError(error=False, feed_url=feed_url, webhook=webhook) diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py index 108dd1f..c4f9370 100644 --- a/discord_rss_bot/main.py +++ b/discord_rss_bot/main.py @@ -26,7 +26,6 @@ Functions: startup() Runs on startup. """ -import enum import sys from functools import cache from typing import Any, Iterable @@ -37,13 +36,13 @@ from fastapi import FastAPI, Form, HTTPException, Request from fastapi.responses import FileResponse, HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates -from reader import EntryCounts, Feed, FeedCounts, FeedNotFoundError, ReaderError, ResourceNotFoundError, StorageError +from reader import EntryCounts, Feed, FeedCounts, FeedNotFoundError, ReaderError, ResourceNotFoundError, StorageError, \ + TagNotFoundError from starlette.templating import _TemplateResponse from tomlkit.toml_document import TOMLDocument from discord_rss_bot.feeds import IfFeedError, add_feed, send_to_discord, update_feed from discord_rss_bot.settings import logger, read_settings_file, reader -from discord_rss_bot.webhooks import set_hook_by_name app: FastAPI = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") @@ -73,6 +72,11 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()) -> Returns: dict: The feed that was added. """ + + # Remove spaces from feed_url + feed_url = feed_url.strip() + logger.debug(f"Stripped feed_url: {feed_url}") + logger.info(f"Add feed: {feed_url}") logger.info(f"Webhook: {webhook_dropdown}") @@ -91,29 +95,33 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()) -> } return HTTPException(status_code=500, detail=error_dict) - # Check if set_hook_by_name() was successful. - if isinstance( - set_hook_by_name(name=webhook_dropdown, feed_url=feed_url), - ResourceNotFoundError, - ): - return set_hook_by_name(name=webhook_dropdown, feed_url=feed_url) + settings: TOMLDocument = read_settings_file() - new_tag: str = str(reader.get_tag(feed_url, "webhook")) + logger.debug(f"Webhook name: {webhook_dropdown} with URL: {settings['webhooks'][webhook_dropdown]}") + webhook_url: str = str(settings["webhooks"][webhook_dropdown]) + try: + reader.set_tag(feed_url, "webhook", webhook_url) + except ResourceNotFoundError as e: + error_msg: str = f"ResourceNotFoundError: Could not set webhook: {e}" + logger.error(error_msg, exc_info=True) + return HTTPException(status_code=500, detail=error_msg) + + new_tag = reader.get_tag(feed_url, "webhook") logger.info(f"New tag: {new_tag}") return {"feed_url": str(feed_url), "status": "added"} -def create_list_of_webhooks() -> enum.EnumMeta: +def create_list_of_webhooks() -> list[dict[str, str]]: """List with webhooks.""" logger.info("Creating list with webhooks.") settings: TOMLDocument = read_settings_file() - list_of_webhooks = dict() + list_of_webhooks = [] for hook in settings["webhooks"]: logger.info(f"Webhook name: {hook} with URL: {settings['webhooks'][hook]}") - list_of_webhooks[hook] = settings["webhooks"][hook] - + list_of_webhooks.append({"name": hook, "url": settings['webhooks'][hook]}) + logger.debug(f"Hook: {hook}, URL: {settings['webhooks'][hook]}") logger.info(f"List of webhooks: {list_of_webhooks}") - return enum.Enum("DiscordWebhooks", list_of_webhooks) + return list_of_webhooks @cache @@ -154,6 +162,7 @@ async def get_feed(feed_url: str, request: Request) -> _TemplateResponse: logger.info(f"Got feed: {feed_url}") feed: Feed = reader.get_feed(feed_url) + # Get entries from the feed. entries: Iterable[EntryCounts] = reader.get_entries(feed=feed_url) @@ -191,14 +200,16 @@ def make_context_index(request) -> dict: dict: The context. """ - hooks: enum.EnumMeta = create_list_of_webhooks() - for hook in hooks: - logger.info(f"Webhook name: {hook.name}") - - feed_list: list[Feed] = list() + hooks = create_list_of_webhooks() + feed_list = [] feeds: Iterable[Feed] = reader.get_feeds() for feed in feeds: - feed_list.append(feed) + logger.debug(f"Feed: {feed}") + try: + hook = reader.get_tag(feed.url, "webhook") + except TagNotFoundError: + hook = "None" + feed_list.append({"feed": feed, "webhook": hook}) feed_count: FeedCounts = reader.get_feed_counts() entry_count: EntryCounts = reader.get_entry_counts() diff --git a/discord_rss_bot/templates/add.html b/discord_rss_bot/templates/add.html index 854592f..2cb6e80 100644 --- a/discord_rss_bot/templates/add.html +++ b/discord_rss_bot/templates/add.html @@ -1,13 +1,21 @@ {% extends "base.html" %} {% block title %}Index{% endblock %} {% block content %} + {# Home / Add #} + +
{# Feed URL #}
-
diff --git a/discord_rss_bot/templates/base.html b/discord_rss_bot/templates/base.html index 0801bd9..351c801 100644 --- a/discord_rss_bot/templates/base.html +++ b/discord_rss_bot/templates/base.html @@ -1,14 +1,13 @@ - {% block head %} - - - - RSS - {% block title %}{% endblock %} - {% endblock %} + + + + RSS - {% block title %}{% endblock %} + {% block head %}{% endblock %} {% include "nav.html" %} @@ -21,13 +20,17 @@ .com/TheLovinator1">TheLovinator

diff --git a/discord_rss_bot/templates/feed.html b/discord_rss_bot/templates/feed.html index ff88846..41f8c82 100644 --- a/discord_rss_bot/templates/feed.html +++ b/discord_rss_bot/templates/feed.html @@ -13,6 +13,22 @@ {% if feed.last_exception %} Last exception: {{ feed.last_exception }}
{% endif %} {% if not feed.updates_enabled %} Updates disabled!
{% endif %} +
+
+ + + +
+
+
+ +
+
+
{% for entry in entries %} {# Only add
if the entry is not the first one #} @@ -65,17 +81,5 @@ {% if entry.read_modified %} Read modified: {{ entry.read_modified.strftime('%Y-%m-%d, %T') }} {% endif %} {% endfor %} - -
- -
- -
- -
{% endblock %} \ No newline at end of file diff --git a/discord_rss_bot/templates/index.html b/discord_rss_bot/templates/index.html index acdd148..6b37fe9 100644 --- a/discord_rss_bot/templates/index.html +++ b/discord_rss_bot/templates/index.html @@ -1,59 +1,60 @@ {% extends "base.html" %} {% block title %}Index{% endblock %} +{% block head %} + +{% endblock %} {% block content %} + - {% for tag in tags %} - {{ tag }} - {% endfor %} - - - -
- - - - {% endblock %} \ No newline at end of file diff --git a/discord_rss_bot/webhooks.py b/discord_rss_bot/webhooks.py deleted file mode 100644 index 71b3c90..0000000 --- a/discord_rss_bot/webhooks.py +++ /dev/null @@ -1,27 +0,0 @@ -from fastapi import HTTPException -from reader import ResourceNotFoundError -from tomlkit.toml_document import TOMLDocument - -from discord_rss_bot.settings import logger, read_settings_file, reader - - -def set_hook_by_name(name: str, feed_url: str) -> HTTPException: - """Set a webhook by name. - - Args: - name: The name of the webhook. - feed_url: The feed to set the webhook for. - - Returns: - HTTPException: The HTTP exception if the webhook was not found, otherwise None. - """ - settings: TOMLDocument = read_settings_file() - logger.debug(f"Webhook name: {name} with URL: {settings['webhooks'][name]}") - webhook_url: str = str(settings["webhooks"][name]) - try: - reader.set_tag(feed_url, "webhook", webhook_url) - - except ResourceNotFoundError as e: - error_msg: str = f"ResourceNotFoundError: Could not set webhook: {e}" - logger.error(error_msg, exc_info=True) - return HTTPException(status_code=500, detail=error_msg)