Add typehints

This commit is contained in:
2022-12-25 19:16:27 +01:00
parent 63f494dc71
commit dd10436024
6 changed files with 78 additions and 67 deletions

View File

@ -13,13 +13,13 @@ def is_word_in_text(words: str, text: str) -> bool:
bool: If the word is in the text. bool: If the word is in the text.
""" """
# Split the word list into a list of words. # 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. # Check if each word is in the text.
for word in word_list: for word in word_list:
pattern = rf"(^|[^\w]){word}([^\w]|$)" look_for: str = rf"(^|[^\w]){word}([^\w]|$)"
pattern = re.compile(pattern, re.IGNORECASE) pattern: re.Pattern[str] = re.compile(look_for, re.IGNORECASE)
matches = re.search(pattern, text) matches: re.Match[str] | None = re.search(pattern, text)
if matches: if matches:
return True return True
return False return False
@ -39,9 +39,9 @@ def has_black_tags(custom_reader: Reader, feed: Feed) -> bool:
Returns: Returns:
bool: If the feed has any of the tags. bool: If the feed has any of the tags.
""" """
blacklist_title = get_blacklist_title(custom_reader, feed) blacklist_title: str = get_blacklist_title(custom_reader, feed)
blacklist_summary = get_blacklist_summary(custom_reader, feed) blacklist_summary: str = get_blacklist_summary(custom_reader, feed)
blacklist_content = get_blacklist_content(custom_reader, feed) blacklist_content: str = get_blacklist_content(custom_reader, feed)
if blacklist_title or blacklist_summary or blacklist_content: if blacklist_title or blacklist_summary or blacklist_content:
return True return True
@ -59,9 +59,9 @@ def should_be_skipped(custom_reader: Reader, entry: Entry) -> bool:
bool: If the entry is in the blacklist. bool: If the entry is in the blacklist.
""" """
feed: Feed = entry.feed feed: Feed = entry.feed
blacklist_title = get_blacklist_title(custom_reader, feed) blacklist_title: str = get_blacklist_title(custom_reader, feed)
blacklist_summary = get_blacklist_summary(custom_reader, feed) blacklist_summary: str = get_blacklist_summary(custom_reader, feed)
blacklist_content = get_blacklist_content(custom_reader, feed) blacklist_content: str = get_blacklist_content(custom_reader, feed)
# TODO: Fix content # TODO: Fix content
# TODO: Check author # 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): if is_word_in_text(blacklist_summary, entry.summary):
return True return True
return False
# if blacklist_content.lower() in entry.content.lower(): # 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. 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. str: The blacklist_content tag.
""" """
try: try:
blacklist_content = custom_reader.get_tag(feed, "blacklist_content") blacklist_content: str = custom_reader.get_tag(feed, "blacklist_content") # type: ignore
except TagNotFoundError: except TagNotFoundError:
blacklist_content = "" blacklist_content: str = ""
except ValueError: except ValueError:
blacklist_content = "" blacklist_content: str = ""
return blacklist_content 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. 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. str: The blacklist_summary tag.
""" """
try: try:
blacklist_summary = custom_reader.get_tag(feed, "blacklist_summary") blacklist_summary: str = custom_reader.get_tag(feed, "blacklist_summary") # type: ignore
except TagNotFoundError: except TagNotFoundError:
blacklist_summary = "" blacklist_summary: str = ""
except ValueError: except ValueError:
blacklist_summary = "" blacklist_summary: str = ""
return blacklist_summary 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. 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. str: The blacklist_title tag.
""" """
try: try:
blacklist_title = custom_reader.get_tag(feed, "blacklist_title") blacklist_title: str = custom_reader.get_tag(feed, "blacklist_title") # type: ignore
except TagNotFoundError: except TagNotFoundError:
blacklist_title = "" blacklist_title: str = ""
except ValueError: except ValueError:
blacklist_title = "" blacklist_title: str = ""
return blacklist_title return blacklist_title

View File

@ -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_message: str = f":robot: :mega: {entry.title}\n{entry.link}"
webhook: DiscordWebhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True) webhook: DiscordWebhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True)
blacklisted = should_be_skipped(reader, entry) blacklisted: bool = should_be_skipped(reader, entry)
whitelisted = should_be_sent(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 # Check if the entry has a whitelist
if if_whitelist_tags: if if_whitelist_tags:

View File

@ -6,14 +6,14 @@ import sys
import requests import requests
def healthcheck(): def healthcheck() -> None:
"""Check if the website is up. """Check if the website is up.
sys.exit(0): success - the container is healthy and ready for use. sys.exit(0): success - the container is healthy and ready for use.
sys.exit(1): unhealthy - the container is not working correctly.""" sys.exit(1): unhealthy - the container is not working correctly."""
# TODO: We should check more than just that the website is up. # TODO: We should check more than just that the website is up.
try: try:
r = requests.get("http://localhost:5000") r: requests.Response = requests.get("http://localhost:5000")
if r.ok: if r.ok:
sys.exit(0) sys.exit(0)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:

View File

@ -75,7 +75,8 @@ def encode_url(url_to_quote: str) -> str:
""" """
if url_to_quote: if url_to_quote:
return urllib.parse.quote(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: 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 has_white_tags(reader, entry_to_check.feed):
if should_be_sent(reader, entry_to_check): if should_be_sent(reader, entry_to_check):
return True return True
return False
def entry_is_blacklisted(entry_to_check: Entry) -> bool: 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 has_black_tags(reader, entry_to_check.feed):
if should_be_skipped(reader, entry_to_check): if should_be_skipped(reader, entry_to_check):
return True return True
return False
templates.env.filters["encode_url"] = encode_url 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) webhooks.append(new_webhook)
# Add our new list of webhooks to the database. # 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) 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() clean_webhook_url: str = webhook_url.strip()
# Get current webhooks from the database if they exist otherwise use an empty list. # 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. # Only add the webhook if it doesn't already exist.
for webhook in webhooks: 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']}.") print(f"Removed webhook {webhook['name']}.")
# Add our new list of webhooks to the database. # 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) return RedirectResponse(url="/", status_code=303)
@ -212,15 +215,15 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()) ->
except TagNotFoundError: except TagNotFoundError:
hooks = [] hooks = []
webhook_url = None webhook_url = ""
if len(hooks) > 0: if len(hooks) > 0:
# Get the webhook URL from the dropdown. # Get the webhook URL from the dropdown.
for hook in hooks: for hook in hooks:
if hook["name"] == webhook_dropdown: if hook["name"] == webhook_dropdown:
webhook_url = hook["url"] webhook_url: str = hook["url"]
break break
if webhook_url is None: if not webhook_url:
# TODO: Show this error on the page. # TODO: Show this error on the page.
return {"error": "No webhook URL found."} return {"error": "No webhook URL found."}
@ -260,7 +263,7 @@ async def set_whitelist(
whitelist_summary: str = Form(None), whitelist_summary: str = Form(None),
whitelist_content: str = Form(None), whitelist_content: str = Form(None),
feed_url: str = Form(), feed_url: str = Form(),
): ) -> RedirectResponse:
# Add the whitelist to the feed. # Add the whitelist to the feed.
if whitelist_title: if whitelist_title:
@ -283,9 +286,9 @@ async def get_whitelist(feed_url: str, request: Request) -> _TemplateResponse:
feed: Feed = reader.get_feed(url) feed: Feed = reader.get_feed(url)
try: try:
whitelist = reader.get_tag(url, "whitelist") whitelist: str = reader.get_tag(url, "whitelist")
except TagNotFoundError: except TagNotFoundError:
whitelist = "" whitelist: str = ""
context = {"request": request, "feed": feed, "whitelist": whitelist} context = {"request": request, "feed": feed, "whitelist": whitelist}
return templates.TemplateResponse("whitelist.html", context) return templates.TemplateResponse("whitelist.html", context)
@ -297,7 +300,7 @@ async def set_blacklist(
blacklist_summary: str = Form(None), blacklist_summary: str = Form(None),
blacklist_content: str = Form(None), blacklist_content: str = Form(None),
feed_url: str = Form(), feed_url: str = Form(),
): ) -> RedirectResponse:
# Add the blacklist to the feed. # Add the blacklist to the feed.
if blacklist_title: if blacklist_title:
@ -320,9 +323,9 @@ async def get_blacklist(feed_url: str, request: Request) -> _TemplateResponse:
feed: Feed = reader.get_feed(url) feed: Feed = reader.get_feed(url)
try: try:
blacklist = reader.get_tag(url, "blacklist") blacklist: str = reader.get_tag(url, "blacklist")
except TagNotFoundError: except TagNotFoundError:
blacklist = "" blacklist: str = ""
context = {"request": request, "feed": feed, "blacklist": blacklist} context = {"request": request, "feed": feed, "blacklist": blacklist}
return templates.TemplateResponse("blacklist.html", context) return templates.TemplateResponse("blacklist.html", context)
@ -417,12 +420,12 @@ def make_context_index(request) -> dict:
except TagNotFoundError: except TagNotFoundError:
hooks = [] hooks = []
feed_list = [] feed_list: list[dict[str, Any]] = []
broken_feeds = [] broken_feeds: list[Feed] = []
feeds: Iterable[Feed] = reader.get_feeds() feeds: Iterable[Feed] = reader.get_feeds()
for feed in feeds: for feed in feeds:
try: 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}) feed_list.append({"feed": feed, "webhook": hook})
except TagNotFoundError: except TagNotFoundError:
broken_feeds.append(feed) broken_feeds.append(feed)

View File

@ -18,7 +18,8 @@ import os
from platformdirs import user_data_dir from platformdirs import user_data_dir
from reader import Reader, make_reader 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) data_dir: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True)
os.makedirs(data_dir, exist_ok=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. list[dict[str, str]]: The webhooks.
""" """
webhooks: list[dict[str, str]] = [] webhooks: list[dict[str, str]] = []
# Get global tags
if reader.get_tags(()) is not None: if reader.get_tags(()) is not None:
for tag in reader.get_tag_keys(()): for tag in reader.get_tag_keys(()):
# Check if the tag is named webhooks
if tag == "webhooks": if tag == "webhooks":
webhooks = reader.get_tag((), "webhooks") webhooks = reader.get_tag((), "webhooks") # type: ignore
break break
return webhooks return webhooks

View File

@ -13,13 +13,13 @@ def is_word_in_text(words: str, text: str) -> bool:
bool: If the word is in the text. bool: If the word is in the text.
""" """
# Split the word list into a list of words. # 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. # Check if each word is in the text.
for word in word_list: for word in word_list:
pattern = rf"(^|[^\w]){word}([^\w]|$)" look_for: str = rf"(^|[^\w]){word}([^\w]|$)"
pattern = re.compile(pattern, re.IGNORECASE) pattern: re.Pattern[str] = re.compile(look_for, re.IGNORECASE)
matches = re.search(pattern, text) matches: re.Match[str] | None = re.search(pattern, text)
if matches: if matches:
return True return True
return False return False
@ -39,12 +39,13 @@ def has_white_tags(custom_reader: Reader, feed: Feed) -> bool:
Returns: Returns:
bool: If the feed has any of the tags. bool: If the feed has any of the tags.
""" """
whitelist_title = get_whitelist_title(custom_reader, feed) whitelist_title: str = get_whitelist_title(custom_reader, feed)
whitelist_summary = get_whitelist_summary(custom_reader, feed) whitelist_summary: str = get_whitelist_summary(custom_reader, feed)
whitelist_content = get_whitelist_content(custom_reader, feed) whitelist_content: str = get_whitelist_content(custom_reader, feed)
if whitelist_title or whitelist_summary or whitelist_content: if whitelist_title or whitelist_summary or whitelist_content:
return True return True
return False
def should_be_sent(custom_reader: Reader, entry: Entry) -> bool: 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. bool: If the entry is in the whitelist.
""" """
feed: Feed = entry.feed feed: Feed = entry.feed
whitelist_title = get_whitelist_title(custom_reader, feed) whitelist_title: str = get_whitelist_title(custom_reader, feed)
whitelist_summary = get_whitelist_summary(custom_reader, feed) whitelist_summary: str = get_whitelist_summary(custom_reader, feed)
whitelist_content = get_whitelist_content(custom_reader, feed) whitelist_content: str = get_whitelist_content(custom_reader, feed)
# TODO: Fix content # TODO: Fix content
# TODO: Check author # 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): if is_word_in_text(whitelist_summary, entry.summary):
return True return True
return False
# if whitelist_content.lower() in entry.content.lower(): # 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. 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. str: The whitelist_content tag.
""" """
try: try:
whitelist_content = custom_reader.get_tag(feed, "whitelist_content") whitelist_content: str = custom_reader.get_tag(feed, "whitelist_content") # type: ignore
except TagNotFoundError: except TagNotFoundError:
whitelist_content = "" whitelist_content: str = ""
except ValueError: except ValueError:
whitelist_content = "" whitelist_content: str = ""
return whitelist_content 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. 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. str: The whitelist_summary tag.
""" """
try: try:
whitelist_summary = custom_reader.get_tag(feed, "whitelist_summary") whitelist_summary: str = custom_reader.get_tag(feed, "whitelist_summary") # type: ignore
except TagNotFoundError: except TagNotFoundError:
whitelist_summary = "" whitelist_summary: str = ""
except ValueError: except ValueError:
whitelist_summary = "" whitelist_summary: str = ""
return whitelist_summary 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. 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. str: The whitelist_title tag.
""" """
try: try:
whitelist_title = custom_reader.get_tag(feed, "whitelist_title") whitelist_title: str = custom_reader.get_tag(feed, "whitelist_title") # type: ignore
except TagNotFoundError: except TagNotFoundError:
whitelist_title = "" whitelist_title: str = ""
except ValueError: except ValueError:
whitelist_title = "" whitelist_title: str = ""
return whitelist_title return whitelist_title