From dd10436024aeea74be819fe4ffdd59d5e01883e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sun, 25 Dec 2022 19:16:27 +0100 Subject: [PATCH] Add typehints --- discord_rss_bot/blacklist.py | 46 ++++++++++++++++++---------------- discord_rss_bot/feeds.py | 6 ++--- discord_rss_bot/healthcheck.py | 4 +-- discord_rss_bot/main.py | 35 ++++++++++++++------------ discord_rss_bot/settings.py | 8 ++++-- discord_rss_bot/whitelist.py | 46 ++++++++++++++++++---------------- 6 files changed, 78 insertions(+), 67 deletions(-) diff --git a/discord_rss_bot/blacklist.py b/discord_rss_bot/blacklist.py index 73b4c4e..e4bfa13 100644 --- a/discord_rss_bot/blacklist.py +++ b/discord_rss_bot/blacklist.py @@ -13,13 +13,13 @@ def is_word_in_text(words: str, text: str) -> bool: bool: If the word is in the text. """ # Split the word list into a list of words. - word_list = words.split(",") + word_list: list[str] = words.split(",") # Check if each word is in the text. for word in word_list: - pattern = rf"(^|[^\w]){word}([^\w]|$)" - pattern = re.compile(pattern, re.IGNORECASE) - matches = re.search(pattern, text) + look_for: str = rf"(^|[^\w]){word}([^\w]|$)" + pattern: re.Pattern[str] = re.compile(look_for, re.IGNORECASE) + matches: re.Match[str] | None = re.search(pattern, text) if matches: return True return False @@ -39,9 +39,9 @@ def has_black_tags(custom_reader: Reader, feed: Feed) -> bool: Returns: bool: If the feed has any of the tags. """ - blacklist_title = get_blacklist_title(custom_reader, feed) - blacklist_summary = get_blacklist_summary(custom_reader, feed) - blacklist_content = get_blacklist_content(custom_reader, feed) + blacklist_title: str = get_blacklist_title(custom_reader, feed) + blacklist_summary: str = get_blacklist_summary(custom_reader, feed) + blacklist_content: str = get_blacklist_content(custom_reader, feed) if blacklist_title or blacklist_summary or blacklist_content: return True @@ -59,9 +59,9 @@ def should_be_skipped(custom_reader: Reader, entry: Entry) -> bool: bool: If the entry is in the blacklist. """ feed: Feed = entry.feed - blacklist_title = get_blacklist_title(custom_reader, feed) - blacklist_summary = get_blacklist_summary(custom_reader, feed) - blacklist_content = get_blacklist_content(custom_reader, feed) + blacklist_title: str = get_blacklist_title(custom_reader, feed) + blacklist_summary: str = get_blacklist_summary(custom_reader, feed) + blacklist_content: str = get_blacklist_content(custom_reader, feed) # TODO: Fix content # TODO: Check author @@ -73,10 +73,12 @@ def should_be_skipped(custom_reader: Reader, entry: Entry) -> bool: if is_word_in_text(blacklist_summary, entry.summary): return True + return False + # if blacklist_content.lower() in entry.content.lower(): -def get_blacklist_content(custom_reader, feed) -> str: +def get_blacklist_content(custom_reader: Reader, feed: Feed) -> str: """ Get the blacklist_content tag from the feed. @@ -88,15 +90,15 @@ def get_blacklist_content(custom_reader, feed) -> str: str: The blacklist_content tag. """ try: - blacklist_content = custom_reader.get_tag(feed, "blacklist_content") + blacklist_content: str = custom_reader.get_tag(feed, "blacklist_content") # type: ignore except TagNotFoundError: - blacklist_content = "" + blacklist_content: str = "" except ValueError: - blacklist_content = "" + blacklist_content: str = "" return blacklist_content -def get_blacklist_summary(custom_reader, feed) -> str: +def get_blacklist_summary(custom_reader: Reader, feed: Feed) -> str: """ Get the blacklist_summary tag from the feed. @@ -108,15 +110,15 @@ def get_blacklist_summary(custom_reader, feed) -> str: str: The blacklist_summary tag. """ try: - blacklist_summary = custom_reader.get_tag(feed, "blacklist_summary") + blacklist_summary: str = custom_reader.get_tag(feed, "blacklist_summary") # type: ignore except TagNotFoundError: - blacklist_summary = "" + blacklist_summary: str = "" except ValueError: - blacklist_summary = "" + blacklist_summary: str = "" return blacklist_summary -def get_blacklist_title(custom_reader, feed) -> str: +def get_blacklist_title(custom_reader: Reader, feed: Feed) -> str: """ Get the blacklist_title tag from the feed. @@ -128,9 +130,9 @@ def get_blacklist_title(custom_reader, feed) -> str: str: The blacklist_title tag. """ try: - blacklist_title = custom_reader.get_tag(feed, "blacklist_title") + blacklist_title: str = custom_reader.get_tag(feed, "blacklist_title") # type: ignore except TagNotFoundError: - blacklist_title = "" + blacklist_title: str = "" except ValueError: - blacklist_title = "" + blacklist_title: str = "" return blacklist_title diff --git a/discord_rss_bot/feeds.py b/discord_rss_bot/feeds.py index 3336e5a..cc66a88 100644 --- a/discord_rss_bot/feeds.py +++ b/discord_rss_bot/feeds.py @@ -74,10 +74,10 @@ def send_to_discord(custom_reader: Reader | None = None, feed=None, do_once=Fals webhook_message: str = f":robot: :mega: {entry.title}\n{entry.link}" webhook: DiscordWebhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True) - blacklisted = should_be_skipped(reader, entry) - whitelisted = should_be_sent(reader, entry) + blacklisted: bool = should_be_skipped(reader, entry) + whitelisted: bool = should_be_sent(reader, entry) - if_whitelist_tags = has_white_tags(reader, feed) + if_whitelist_tags: bool = has_white_tags(reader, feed) # Check if the entry has a whitelist if if_whitelist_tags: diff --git a/discord_rss_bot/healthcheck.py b/discord_rss_bot/healthcheck.py index 620ba35..32fefc4 100644 --- a/discord_rss_bot/healthcheck.py +++ b/discord_rss_bot/healthcheck.py @@ -6,14 +6,14 @@ import sys import requests -def healthcheck(): +def healthcheck() -> None: """Check if the website is up. sys.exit(0): success - the container is healthy and ready for use. sys.exit(1): unhealthy - the container is not working correctly.""" # TODO: We should check more than just that the website is up. try: - r = requests.get("http://localhost:5000") + r: requests.Response = requests.get("http://localhost:5000") if r.ok: sys.exit(0) except requests.exceptions.RequestException as e: diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py index 2ae82f4..46389c1 100644 --- a/discord_rss_bot/main.py +++ b/discord_rss_bot/main.py @@ -75,7 +75,8 @@ def encode_url(url_to_quote: str) -> str: """ if url_to_quote: return urllib.parse.quote(url_to_quote) - print("url_to_quote is None") # TODO: Send error to Discord. + return "None" + # TODO: Send error to Discord. def entry_is_whitelisted(entry_to_check: Entry) -> bool: @@ -92,6 +93,7 @@ def entry_is_whitelisted(entry_to_check: Entry) -> bool: if has_white_tags(reader, entry_to_check.feed): if should_be_sent(reader, entry_to_check): return True + return False def entry_is_blacklisted(entry_to_check: Entry) -> bool: @@ -108,6 +110,7 @@ def entry_is_blacklisted(entry_to_check: Entry) -> bool: if has_black_tags(reader, entry_to_check.feed): if should_be_skipped(reader, entry_to_check): return True + return False templates.env.filters["encode_url"] = encode_url @@ -143,7 +146,7 @@ async def add_webhook(webhook_name: str = Form(), webhook_url: str = Form()) -> webhooks.append(new_webhook) # Add our new list of webhooks to the database. - reader.set_tag((), "webhooks", webhooks) + reader.set_tag((), "webhooks", webhooks) # type: ignore return RedirectResponse(url="/", status_code=303) @@ -166,7 +169,7 @@ async def delete_webhook(webhook_url: str = Form()) -> RedirectResponse | dict[s clean_webhook_url: str = webhook_url.strip() # Get current webhooks from the database if they exist otherwise use an empty list. - webhooks = list_webhooks(reader) + webhooks: list[dict[str, str]] = list_webhooks(reader) # Only add the webhook if it doesn't already exist. for webhook in webhooks: @@ -177,7 +180,7 @@ async def delete_webhook(webhook_url: str = Form()) -> RedirectResponse | dict[s print(f"Removed webhook {webhook['name']}.") # Add our new list of webhooks to the database. - reader.set_tag((), "webhooks", webhooks) + reader.set_tag((), "webhooks", webhooks) # type: ignore return RedirectResponse(url="/", status_code=303) @@ -212,15 +215,15 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()) -> except TagNotFoundError: hooks = [] - webhook_url = None + webhook_url = "" if len(hooks) > 0: # Get the webhook URL from the dropdown. for hook in hooks: if hook["name"] == webhook_dropdown: - webhook_url = hook["url"] + webhook_url: str = hook["url"] break - if webhook_url is None: + if not webhook_url: # TODO: Show this error on the page. return {"error": "No webhook URL found."} @@ -260,7 +263,7 @@ async def set_whitelist( whitelist_summary: str = Form(None), whitelist_content: str = Form(None), feed_url: str = Form(), -): +) -> RedirectResponse: # Add the whitelist to the feed. if whitelist_title: @@ -283,9 +286,9 @@ async def get_whitelist(feed_url: str, request: Request) -> _TemplateResponse: feed: Feed = reader.get_feed(url) try: - whitelist = reader.get_tag(url, "whitelist") + whitelist: str = reader.get_tag(url, "whitelist") except TagNotFoundError: - whitelist = "" + whitelist: str = "" context = {"request": request, "feed": feed, "whitelist": whitelist} return templates.TemplateResponse("whitelist.html", context) @@ -297,7 +300,7 @@ async def set_blacklist( blacklist_summary: str = Form(None), blacklist_content: str = Form(None), feed_url: str = Form(), -): +) -> RedirectResponse: # Add the blacklist to the feed. if blacklist_title: @@ -320,9 +323,9 @@ async def get_blacklist(feed_url: str, request: Request) -> _TemplateResponse: feed: Feed = reader.get_feed(url) try: - blacklist = reader.get_tag(url, "blacklist") + blacklist: str = reader.get_tag(url, "blacklist") except TagNotFoundError: - blacklist = "" + blacklist: str = "" context = {"request": request, "feed": feed, "blacklist": blacklist} return templates.TemplateResponse("blacklist.html", context) @@ -417,12 +420,12 @@ def make_context_index(request) -> dict: except TagNotFoundError: hooks = [] - feed_list = [] - broken_feeds = [] + feed_list: list[dict[str, Any]] = [] + broken_feeds: list[Feed] = [] feeds: Iterable[Feed] = reader.get_feeds() for feed in feeds: try: - hook = reader.get_tag(feed.url, "webhook") + hook: str = reader.get_tag(feed.url, "webhook") # type: ignore feed_list.append({"feed": feed, "webhook": hook}) except TagNotFoundError: broken_feeds.append(feed) diff --git a/discord_rss_bot/settings.py b/discord_rss_bot/settings.py index e2031ea..e99bd83 100644 --- a/discord_rss_bot/settings.py +++ b/discord_rss_bot/settings.py @@ -18,7 +18,8 @@ import os from platformdirs import user_data_dir from reader import Reader, make_reader -logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(funcName)s:%(lineno)d] %(message)s") +logging_format: str = "[%(asctime)s] [%(funcName)s:%(lineno)d] %(message)s" +logging.basicConfig(level=logging.DEBUG, format=logging_format) data_dir: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True) os.makedirs(data_dir, exist_ok=True) @@ -58,9 +59,12 @@ def list_webhooks(reader: Reader) -> list[dict[str, str]]: list[dict[str, str]]: The webhooks. """ webhooks: list[dict[str, str]] = [] + + # Get global tags if reader.get_tags(()) is not None: for tag in reader.get_tag_keys(()): + # Check if the tag is named webhooks if tag == "webhooks": - webhooks = reader.get_tag((), "webhooks") + webhooks = reader.get_tag((), "webhooks") # type: ignore break return webhooks diff --git a/discord_rss_bot/whitelist.py b/discord_rss_bot/whitelist.py index fb89328..7024053 100644 --- a/discord_rss_bot/whitelist.py +++ b/discord_rss_bot/whitelist.py @@ -13,13 +13,13 @@ def is_word_in_text(words: str, text: str) -> bool: bool: If the word is in the text. """ # Split the word list into a list of words. - word_list = words.split(",") + word_list: list[str] = words.split(",") # Check if each word is in the text. for word in word_list: - pattern = rf"(^|[^\w]){word}([^\w]|$)" - pattern = re.compile(pattern, re.IGNORECASE) - matches = re.search(pattern, text) + look_for: str = rf"(^|[^\w]){word}([^\w]|$)" + pattern: re.Pattern[str] = re.compile(look_for, re.IGNORECASE) + matches: re.Match[str] | None = re.search(pattern, text) if matches: return True return False @@ -39,12 +39,13 @@ def has_white_tags(custom_reader: Reader, feed: Feed) -> bool: Returns: bool: If the feed has any of the tags. """ - whitelist_title = get_whitelist_title(custom_reader, feed) - whitelist_summary = get_whitelist_summary(custom_reader, feed) - whitelist_content = get_whitelist_content(custom_reader, feed) + whitelist_title: str = get_whitelist_title(custom_reader, feed) + whitelist_summary: str = get_whitelist_summary(custom_reader, feed) + whitelist_content: str = get_whitelist_content(custom_reader, feed) if whitelist_title or whitelist_summary or whitelist_content: return True + return False def should_be_sent(custom_reader: Reader, entry: Entry) -> bool: @@ -59,9 +60,9 @@ def should_be_sent(custom_reader: Reader, entry: Entry) -> bool: bool: If the entry is in the whitelist. """ feed: Feed = entry.feed - whitelist_title = get_whitelist_title(custom_reader, feed) - whitelist_summary = get_whitelist_summary(custom_reader, feed) - whitelist_content = get_whitelist_content(custom_reader, feed) + whitelist_title: str = get_whitelist_title(custom_reader, feed) + whitelist_summary: str = get_whitelist_summary(custom_reader, feed) + whitelist_content: str = get_whitelist_content(custom_reader, feed) # TODO: Fix content # TODO: Check author @@ -73,10 +74,11 @@ def should_be_sent(custom_reader: Reader, entry: Entry) -> bool: if is_word_in_text(whitelist_summary, entry.summary): return True + return False # if whitelist_content.lower() in entry.content.lower(): -def get_whitelist_content(custom_reader, feed) -> str: +def get_whitelist_content(custom_reader: Reader, feed: Feed) -> str: """ Get the whitelist_content tag from the feed. @@ -88,15 +90,15 @@ def get_whitelist_content(custom_reader, feed) -> str: str: The whitelist_content tag. """ try: - whitelist_content = custom_reader.get_tag(feed, "whitelist_content") + whitelist_content: str = custom_reader.get_tag(feed, "whitelist_content") # type: ignore except TagNotFoundError: - whitelist_content = "" + whitelist_content: str = "" except ValueError: - whitelist_content = "" + whitelist_content: str = "" return whitelist_content -def get_whitelist_summary(custom_reader, feed) -> str: +def get_whitelist_summary(custom_reader: Reader, feed: Feed) -> str: """ Get the whitelist_summary tag from the feed. @@ -108,15 +110,15 @@ def get_whitelist_summary(custom_reader, feed) -> str: str: The whitelist_summary tag. """ try: - whitelist_summary = custom_reader.get_tag(feed, "whitelist_summary") + whitelist_summary: str = custom_reader.get_tag(feed, "whitelist_summary") # type: ignore except TagNotFoundError: - whitelist_summary = "" + whitelist_summary: str = "" except ValueError: - whitelist_summary = "" + whitelist_summary: str = "" return whitelist_summary -def get_whitelist_title(custom_reader, feed) -> str: +def get_whitelist_title(custom_reader: Reader, feed: Feed) -> str: """ Get the whitelist_title tag from the feed. @@ -128,9 +130,9 @@ def get_whitelist_title(custom_reader, feed) -> str: str: The whitelist_title tag. """ try: - whitelist_title = custom_reader.get_tag(feed, "whitelist_title") + whitelist_title: str = custom_reader.get_tag(feed, "whitelist_title") # type: ignore except TagNotFoundError: - whitelist_title = "" + whitelist_title: str = "" except ValueError: - whitelist_title = "" + whitelist_title: str = "" return whitelist_title