Fix Ruff warnings

This commit is contained in:
2024-08-12 16:28:07 +02:00
parent 66ae04a0b3
commit 4480593f49
5 changed files with 75 additions and 26 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import json
import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING
from bs4 import BeautifulSoup
from markdownify import markdownify
@ -12,9 +11,6 @@ from reader import Entry, Feed, Reader, TagNotFoundError
from discord_rss_bot.is_url_valid import is_url_valid
from discord_rss_bot.settings import get_reader
if TYPE_CHECKING:
from reader.types import JSONType
logger: logging.Logger = logging.getLogger(__name__)
@ -117,8 +113,7 @@ def replace_tags_in_text_message(entry: Entry) -> str:
for template, replace_with in replacement.items():
custom_message = try_to_replace(custom_message, template, replace_with)
our_custom_message = custom_message.replace("\\n", "\n")
return our_custom_message # noqa: RET504
return custom_message.replace("\\n", "\n")
def get_first_image(summary: str | None, content: str | None) -> str:
@ -236,19 +231,29 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
]
for replacement in list_of_replacements:
for template, replace_with in replacement.items():
embed.title = try_to_replace(embed.title, template, replace_with)
embed.description = try_to_replace(embed.description, template, replace_with)
embed.author_name = try_to_replace(embed.author_name, template, replace_with)
embed.author_url = try_to_replace(embed.author_url, template, replace_with)
embed.author_icon_url = try_to_replace(embed.author_icon_url, template, replace_with)
embed.image_url = try_to_replace(embed.image_url, template, replace_with)
embed.thumbnail_url = try_to_replace(embed.thumbnail_url, template, replace_with)
embed.footer_text = try_to_replace(embed.footer_text, template, replace_with)
embed.footer_icon_url = try_to_replace(embed.footer_icon_url, template, replace_with)
_replace_embed_tags(embed, template, replace_with)
return embed
def _replace_embed_tags(embed: CustomEmbed, template: str, replace_with: str) -> None:
"""Replace tags in embed.
Args:
embed: The embed to replace tags in.
template: The tag to replace.
replace_with: What to replace the tag with.
"""
embed.title = try_to_replace(embed.title, template, replace_with)
embed.description = try_to_replace(embed.description, template, replace_with)
embed.author_name = try_to_replace(embed.author_name, template, replace_with)
embed.author_url = try_to_replace(embed.author_url, template, replace_with)
embed.author_icon_url = try_to_replace(embed.author_icon_url, template, replace_with)
embed.image_url = try_to_replace(embed.image_url, template, replace_with)
embed.thumbnail_url = try_to_replace(embed.thumbnail_url, template, replace_with)
embed.footer_text = try_to_replace(embed.footer_text, template, replace_with)
embed.footer_icon_url = try_to_replace(embed.footer_icon_url, template, replace_with)
def get_custom_message(custom_reader: Reader, feed: Feed) -> str:
"""Get custom_message tag from feed.
@ -303,8 +308,7 @@ def get_embed(custom_reader: Reader, feed: Feed) -> CustomEmbed:
Returns:
Returns the contents from the embed tag.
"""
embed: str | JSONType = custom_reader.get_tag(feed, "embed", "")
if embed:
if embed := custom_reader.get_tag(feed, "embed", ""):
if not isinstance(embed, str):
return get_embed_data(embed) # type: ignore
embed_data: dict[str, str | int] = json.loads(embed)

View File

@ -73,7 +73,7 @@ def set_description(custom_embed: custom_message.CustomEmbed, discord_embed: Dis
max_description_length: int = 2000
embed_description: str = custom_embed.description
embed_description = (
embed_description[:max_description_length] + "..."
f"{embed_description[:max_description_length]}..."
if len(embed_description) > max_description_length
else embed_description
)
@ -90,7 +90,7 @@ def set_title(custom_embed: custom_message.CustomEmbed, discord_embed: DiscordEm
# Its actually 256, but we will use 200 to be safe.
max_title_length: int = 200
embed_title: str = custom_embed.title
embed_title = embed_title[:max_title_length] + "..." if len(embed_title) > max_title_length else embed_title
embed_title = f"{embed_title[:max_title_length]}..." if len(embed_title) > max_title_length else embed_title
discord_embed.set_title(embed_title) if embed_title else None
@ -168,9 +168,6 @@ def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = Non
custom_reader: If we should use a custom reader instead of the default one.
feed: The feed to send to Discord.
do_once: If we should only send one entry. This is used in the test.
Returns:
Response: The response from the webhook.
"""
# Get the default reader if we didn't get a custom one.
reader: Reader = get_reader() if custom_reader is None else custom_reader
@ -214,7 +211,7 @@ def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = Non
# Its actually 4096, but we will use 4000 to be safe.
max_content_length: int = 4000
webhook_message = (
webhook_message[:max_content_length] + "..."
f"{webhook_message[:max_content_length]}..."
if len(webhook_message) > max_content_length
else webhook_message
)

View File

@ -125,6 +125,9 @@ async def post_add_webhook(
Args:
webhook_name: The name of the webhook.
webhook_url: The url of the webhook.
Returns:
RedirectResponse: Redirect to the index page.
"""
add_webhook(reader, webhook_name, webhook_url)
return RedirectResponse(url="/", status_code=303)
@ -136,6 +139,9 @@ async def post_delete_webhook(webhook_url: Annotated[str, Form()]) -> RedirectRe
Args:
webhook_url: The url of the webhook.
Returns:
RedirectResponse: Redirect to the index page.
"""
# TODO(TheLovinator): Check if the webhook is in use by any feeds before deleting it.
remove_webhook(reader, webhook_url)
@ -152,6 +158,9 @@ async def post_create_feed(
Args:
feed_url: The feed to add.
webhook_dropdown: The webhook to use.
Returns:
RedirectResponse: Redirect to the feed page.
"""
clean_feed_url: str = feed_url.strip()
create_feed(reader, feed_url, webhook_dropdown)
@ -164,6 +173,9 @@ async def post_pause_feed(feed_url: Annotated[str, Form()]) -> RedirectResponse:
Args:
feed_url: The feed to pause.
Returns:
RedirectResponse: Redirect to the feed page.
"""
clean_feed_url: str = feed_url.strip()
reader.disable_feed_updates(clean_feed_url)
@ -176,6 +188,9 @@ async def post_unpause_feed(feed_url: Annotated[str, Form()]) -> RedirectRespons
Args:
feed_url: The Feed to unpause.
Returns:
RedirectResponse: Redirect to the feed page.
"""
clean_feed_url: str = feed_url.strip()
reader.enable_feed_updates(clean_feed_url)
@ -198,6 +213,9 @@ async def post_set_whitelist(
whitelist_content: Whitelisted words for when checking the content.
whitelist_author: Whitelisted words for when checking the author.
feed_url: The feed we should set the whitelist for.
Returns:
RedirectResponse: Redirect to the feed page.
"""
clean_feed_url: str = feed_url.strip() if feed_url else ""
reader.set_tag(clean_feed_url, "whitelist_title", whitelist_title) # type: ignore[call-overload]
@ -215,6 +233,9 @@ async def get_whitelist(feed_url: str, request: Request):
Args:
feed_url: What feed we should get the whitelist for.
request: The request object.
Returns:
HTMLResponse: The whitelist page.
"""
clean_feed_url: str = feed_url.strip()
feed: Feed = reader.get_feed(urllib.parse.unquote(clean_feed_url))
@ -255,6 +276,9 @@ async def post_set_blacklist(
blacklist_content: Blacklisted words for when checking the content.
blacklist_author: Blacklisted words for when checking the author.
feed_url: What feed we should set the blacklist for.
Returns:
RedirectResponse: Redirect to the feed page.
"""
clean_feed_url: str = feed_url.strip() if feed_url else ""
reader.set_tag(clean_feed_url, "blacklist_title", blacklist_title) # type: ignore[call-overload]
@ -305,6 +329,9 @@ async def post_set_custom(
Args:
custom_message: The custom message.
feed_url: The feed we should set the custom message for.
Returns:
RedirectResponse: Redirect to the feed page.
"""
our_custom_message: JSONType | str = custom_message.strip()
our_custom_message = typing.cast(JSONType, our_custom_message)
@ -328,6 +355,9 @@ async def get_custom(feed_url: str, request: Request):
Args:
feed_url: What feed we should get the custom message for.
request: The request object.
Returns:
HTMLResponse: The custom message page.
"""
feed: Feed = reader.get_feed(urllib.parse.unquote(feed_url.strip()))
@ -351,6 +381,9 @@ async def get_embed_page(feed_url: str, request: Request):
Args:
feed_url: What feed we should get the custom message for.
request: The request object.
Returns:
HTMLResponse: The embed page.
"""
feed: Feed = reader.get_feed(urllib.parse.unquote(feed_url.strip()))
@ -466,7 +499,14 @@ async def post_use_text(feed_url: Annotated[str, Form()]) -> RedirectResponse:
@app.get("/add", response_class=HTMLResponse)
def get_add(request: Request):
"""Page for adding a new feed."""
"""Page for adding a new feed.
Args:
request: The request object.
Returns:
HTMLResponse: The add feed page.
"""
context = {
"request": request,
"webhooks": reader.get_tag((), "webhooks", []),
@ -559,6 +599,9 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
Args:
entries: The entries to create HTML for.
Returns:
str: The HTML for the search results.
"""
html: str = ""
for entry in entries:
@ -802,6 +845,9 @@ def modify_webhook(old_hook: Annotated[str, Form()], new_hook: Annotated[str, Fo
Raises:
HTTPException: Webhook could not be modified.
Returns:
RedirectResponse: Redirect to the webhook page.
"""
# Get current webhooks from the database if they exist otherwise use an empty list.
webhooks = list(reader.get_tag((), "webhooks", []))

View File

@ -31,6 +31,8 @@ def get_reader(custom_location: Path | None = None) -> Reader:
Args:
custom_location: The location of the database file.
Returns:
The reader.
"""
db_location: Path = custom_location or Path(data_dir) / "db.sqlite"