Refactor code

This commit is contained in:
2023-01-29 01:08:40 +01:00
parent 155b94cff2
commit ae702fe492
3 changed files with 61 additions and 69 deletions

View File

@ -22,37 +22,29 @@ class CustomEmbed:
footer_icon_url: str
def get_images_from_entry(entry: Entry):
def return_image(found_images) -> list[tuple[str, str]] | None:
soup: BeautifulSoup = BeautifulSoup(found_images, features="lxml")
images = soup.find_all("img")
for image in images:
image_src: str = str(image["src"]) or ""
image_alt: str = "Link to image"
if image.get("alt"):
image_alt = image.get("alt")
return [(image_src, image_alt)]
def get_first_image_html(html: str):
"""Get images from a entry.
Args:
entry: The entry to get the images from.
html: The HTML to get the images from.
Returns:
Returns a list of images.
"""
def return_image(found_images):
soup: BeautifulSoup = BeautifulSoup(found_images, features="lxml")
images = soup.find_all("img")
for image in images:
image_src = image["src"] or ""
image_alt: str = "Link to image"
if image.get("alt"):
image_alt = image.get("alt")
return [(image_src, image_alt)]
images = []
# Get the images from the summary with beautiful soup
if entry.summary:
images = return_image(entry.summary)
# Get the images from the content with beautiful soup
if entry.content:
images = return_image(entry.content[0].value)
# No images found
return images
if images := BeautifulSoup(html, features="lxml").find_all("img"):
return images[0].attrs["src"]
return None
def try_to_replace(custom_message: str, template: str, replace_with: str) -> str:
@ -85,23 +77,17 @@ def replace_tags_in_text_message(entry: Entry) -> str:
custom_reader: Reader = get_reader()
custom_message: str = get_custom_message(feed=feed, custom_reader=custom_reader)
summary = ""
content = ""
if entry.summary:
summary: str = entry.summary
summary = convert_html_to_md(summary)
if entry.content:
for content_item in entry.content:
content: str = content_item.value
content = convert_html_to_md(content)
if images := get_images_from_entry(entry=entry):
first_image: str = images[0][0]
else:
first_image = ""
summary: str = entry.summary or ""
entry_text: str = content or summary
first_image = get_image(summary, content)
summary = convert_html_to_md(summary)
content = convert_html_to_md(content)
list_of_replacements = [
{"{{feed_author}}": feed.author},
@ -128,7 +114,7 @@ def replace_tags_in_text_message(entry: Entry) -> str:
{"{{entry_read_modified}}": entry.read_modified},
{"{{entry_summary}}": summary},
{"{{entry_summary_raw}}": entry.summary or ""},
{"{{entry_text}}": entry_text},
{"{{entry_text}}": content or summary},
{"{{entry_title}}": entry.title},
{"{{entry_updated}}": entry.updated},
{"{{image_1}}": first_image},
@ -141,6 +127,25 @@ def replace_tags_in_text_message(entry: Entry) -> str:
return custom_message.replace("\\n", "\n")
def get_image(summary, content):
"""Get image from summary or content
Args:
summary: The summary from the entry
content: The content from the entry
Returns:
The first image
"""
if content:
if images := BeautifulSoup(content, features="lxml").find_all("img"):
return images[0].attrs["src"]
if summary:
if images := BeautifulSoup(summary, features="lxml").find_all("img"):
return images[0].attrs["src"]
return ""
def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
"""Replace tags in embed.
@ -155,21 +160,17 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
custom_reader: Reader = get_reader()
embed: CustomEmbed = get_embed(feed=feed, custom_reader=custom_reader)
summary = ""
content = ""
if entry.summary:
summary: str = entry.summary
summary = convert_html_to_md(summary)
if entry.content:
for content_item in entry.content:
content: str = content_item.value
content = convert_html_to_md(content)
if images := get_images_from_entry(entry=entry):
first_image: str = images[0][0]
else:
first_image = ""
summary: str = entry.summary or ""
first_image = get_image(summary, content)
summary = convert_html_to_md(summary)
content = convert_html_to_md(content)
entry_text: str = content or summary
@ -273,19 +274,12 @@ def get_embed(custom_reader: Reader, feed: Feed) -> CustomEmbed:
Returns:
Returns the contents from the embed tag.
"""
try:
embed: str = custom_reader.get_tag(feed, "embed") # type: ignore
except TagNotFoundError:
embed = ""
except ValueError:
embed = ""
if embed:
if type(embed) == str:
embed_data: dict[str, str | int] = json.loads(embed)
return get_embed_data(embed_data)
else:
if embed := custom_reader.get_tag(feed, "embed", ""):
if type(embed) != str:
return get_embed_data(embed)
embed_data: dict[str, str | int] = json.loads(embed) # type: ignore
return get_embed_data(embed_data)
return CustomEmbed(
title="",

View File

@ -104,13 +104,8 @@ def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = Non
# Check for new entries for every feed.
reader.update_feeds()
# If feed is not None we will only get the entries for that feed.
if feed is None:
entries: Iterable[Entry] = reader.get_entries(read=False)
else:
entries = reader.get_entries(feed=feed, read=False)
# Loop through the unread entries.
entries: Iterable[Entry] = reader.get_entries(feed=feed, read=False)
for entry in entries:
# Set the webhook to read, so we don't send it again.
reader.set_entry_read(entry, True)

View File

@ -22,7 +22,7 @@ from discord_rss_bot.custom_message import (
CustomEmbed,
get_custom_message,
get_embed,
get_images_from_entry,
get_image,
replace_tags_in_text_message,
save_embed,
)
@ -396,10 +396,13 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
html: str = ""
for entry in entries:
first_image = ""
first_image_text = ""
if images := get_images_from_entry(entry=entry):
first_image: str = images[0][0]
first_image_text: str = images[0][1]
summary: str | None = entry.summary
content = ""
if entry.content:
for content_item in entry.content:
content: str = content_item.value
first_image = get_image(summary, content)
text: str = replace_tags_in_text_message(entry) or "<div class='text-muted'>No content available.</div>"
published = ""
@ -416,7 +419,7 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
entry_id: str = urllib.parse.quote(entry.id)
to_disord_html: str = f"<a class='text-muted' href='/post_entry?entry_id={entry_id}'>Send to Discord</a>"
image_html: str = f"<img src='{first_image}' class='img-fluid' alt='{first_image_text}'>" if first_image else ""
image_html: str = f"<img src='{first_image}' class='img-fluid'>" if first_image else ""
html += f"""<div class="p-2 mb-2 border border-dark">
{blacklisted}{whitelisted}<a class="text-muted text-decoration-none" href="{entry.link}"><h2>{entry.title}</h2></a>