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 #} + +
No feeds yet. Add one here.
- {% endif %} -{{ feed_count.total }} feeds {% if feed_count.broken %} - - {{ feed_count.broken }} broken + + {% else %} + {% endif %} + - {{ feed_count.broken }} broken {% if feed_count.total != feed_count.updates_enabled %} - - {{ feed_count.updates_enabled }} enabled + + {% else %} + {% endif %} + - {{ feed_count.updates_enabled }} enabled + + - {{ entry_count.total }} entries + + ({{ entry_count.averages[0]|round(1) }}, + {{ entry_count.averages[1]|round(1) }}, + {{ entry_count.averages[2]|round(1) }}) +
-+ No feeds yet. Add one here. +
+ {% endif %}{{ entry_count.total }} entries total
- {% if entry_count.read != entry_count.total %} -{{ entry_count.total - entry_count.read }} unread entries
- {% endif %} - {% if entry_count.important %} -Important: {{ entry_count.important }} entries
- {% endif %} - {% if entry_count.has_enclosures %} -Has enclosures: {{ entry_count.has_enclosures }} entries
- {% endif %} -1 Month average: {{ entry_count.averages[0]|round(2) }} entries per day
-3 Months average: {{ entry_count.averages[1]|round(2) }} entries per day
-12 Months average: {{ entry_count.averages[2]|round(2) }} entries per day
-