diff --git a/discord_rss_bot/feeds.py b/discord_rss_bot/feeds.py index 46c6e50..83ac2fd 100644 --- a/discord_rss_bot/feeds.py +++ b/discord_rss_bot/feeds.py @@ -3,7 +3,9 @@ from __future__ import annotations import datetime import logging import pprint +import re from typing import TYPE_CHECKING +from urllib.parse import ParseResult, urlparse from discord_webhook import DiscordEmbed, DiscordWebhook from fastapi import HTTPException @@ -29,6 +31,57 @@ if TYPE_CHECKING: logger: logging.Logger = logging.getLogger(__name__) +def extract_domain(url: str) -> str: # noqa: PLR0911 + """Extract the domain name from a URL. + + Args: + url: The URL to extract the domain from. + + Returns: + str: The domain name, formatted for display. + """ + # Check for empty URL first + if not url: + return "Other" + + try: + # Special handling for YouTube feeds + if "youtube.com/feeds/videos.xml" in url: + return "YouTube" + + # Special handling for Reddit feeds + if "reddit.com" in url or (".rss" in url and "r/" in url): + return "Reddit" + + # Parse the URL and extract the domain + parsed_url: ParseResult = urlparse(url) + domain: str = parsed_url.netloc + + # If we couldn't extract a domain, return "Other" + if not domain: + return "Other" + + # Remove www. prefix if present + domain = re.sub(r"^www\.", "", domain) + + # Special handling for common domains + domain_mapping: dict[str, str] = {"github.com": "GitHub"} + + if domain in domain_mapping: + return domain_mapping[domain] + + # For other domains, capitalize the first part before the TLD + parts: list[str] = domain.split(".") + min_domain_parts = 2 + if len(parts) >= min_domain_parts: + return parts[0].capitalize() + + return domain.capitalize() + except (ValueError, AttributeError, TypeError) as e: + logger.warning("Error extracting domain from %s: %s", url, e) + return "Other" + + def send_entry_to_discord(entry: Entry, custom_reader: Reader | None = None) -> str | None: """Send a single entry to Discord. diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py index 00349ac..7ae706f 100644 --- a/discord_rss_bot/main.py +++ b/discord_rss_bot/main.py @@ -37,7 +37,7 @@ from discord_rss_bot.custom_message import ( replace_tags_in_text_message, save_embed, ) -from discord_rss_bot.feeds import create_feed, send_entry_to_discord, send_to_discord +from discord_rss_bot.feeds import create_feed, extract_domain, send_entry_to_discord, send_to_discord from discord_rss_bot.missing_tags import add_missing_tags from discord_rss_bot.search import create_html_for_search_results from discord_rss_bot.settings import get_reader @@ -875,11 +875,12 @@ def make_context_index(request: Request): broken_feeds = [] feeds_without_attached_webhook = [] + # Get all feeds and organize them feeds: Iterable[Feed] = reader.get_feeds() for feed in feeds: try: webhook = reader.get_tag(feed.url, "webhook") - feed_list.append({"feed": feed, "webhook": webhook}) + feed_list.append({"feed": feed, "webhook": webhook, "domain": extract_domain(feed.url)}) except TagNotFoundError: broken_feeds.append(feed) continue diff --git a/discord_rss_bot/templates/index.html b/discord_rss_bot/templates/index.html index 3db4a50..f9dfc0d 100644 --- a/discord_rss_bot/templates/index.html +++ b/discord_rss_bot/templates/index.html @@ -28,45 +28,66 @@ {{ entry_count.averages[2]|round(1) }})
- + + {% for hook_from_context in webhooks %} -No feeds associated with this webhook.
+ {% endif %}
Hello there!
+
You need to add a webhook here to get started. After that, you can
add feeds here. You can find both of these links in the navigation bar
above.
@@ -79,6 +100,7 @@
Thanks!