Refactor code
This commit is contained in:
@ -22,37 +22,29 @@ class CustomEmbed:
|
|||||||
footer_icon_url: str
|
footer_icon_url: str
|
||||||
|
|
||||||
|
|
||||||
def get_images_from_entry(entry: Entry):
|
def return_image(found_images) -> list[tuple[str, str]] | None:
|
||||||
"""Get images from a entry.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
entry: The entry to get the images from.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Returns a list of images.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def return_image(found_images):
|
|
||||||
soup: BeautifulSoup = BeautifulSoup(found_images, features="lxml")
|
soup: BeautifulSoup = BeautifulSoup(found_images, features="lxml")
|
||||||
images = soup.find_all("img")
|
images = soup.find_all("img")
|
||||||
for image in images:
|
for image in images:
|
||||||
image_src = image["src"] or ""
|
image_src: str = str(image["src"]) or ""
|
||||||
image_alt: str = "Link to image"
|
image_alt: str = "Link to image"
|
||||||
if image.get("alt"):
|
if image.get("alt"):
|
||||||
image_alt = image.get("alt")
|
image_alt = image.get("alt")
|
||||||
return [(image_src, image_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
|
def get_first_image_html(html: str):
|
||||||
if entry.content:
|
"""Get images from a entry.
|
||||||
images = return_image(entry.content[0].value)
|
|
||||||
|
|
||||||
# No images found
|
Args:
|
||||||
return images
|
html: The HTML to get the images from.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Returns a list of 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:
|
def try_to_replace(custom_message: str, template: str, replace_with: str) -> str:
|
||||||
@ -85,24 +77,18 @@ def replace_tags_in_text_message(entry: Entry) -> str:
|
|||||||
custom_reader: Reader = get_reader()
|
custom_reader: Reader = get_reader()
|
||||||
custom_message: str = get_custom_message(feed=feed, custom_reader=custom_reader)
|
custom_message: str = get_custom_message(feed=feed, custom_reader=custom_reader)
|
||||||
|
|
||||||
summary = ""
|
|
||||||
content = ""
|
content = ""
|
||||||
if entry.summary:
|
|
||||||
summary: str = entry.summary
|
|
||||||
summary = convert_html_to_md(summary)
|
|
||||||
|
|
||||||
if entry.content:
|
if entry.content:
|
||||||
for content_item in entry.content:
|
for content_item in entry.content:
|
||||||
content: str = content_item.value
|
content: str = content_item.value
|
||||||
|
|
||||||
|
summary: str = entry.summary or ""
|
||||||
|
|
||||||
|
first_image = get_image(summary, content)
|
||||||
|
|
||||||
|
summary = convert_html_to_md(summary)
|
||||||
content = convert_html_to_md(content)
|
content = convert_html_to_md(content)
|
||||||
|
|
||||||
if images := get_images_from_entry(entry=entry):
|
|
||||||
first_image: str = images[0][0]
|
|
||||||
else:
|
|
||||||
first_image = ""
|
|
||||||
|
|
||||||
entry_text: str = content or summary
|
|
||||||
|
|
||||||
list_of_replacements = [
|
list_of_replacements = [
|
||||||
{"{{feed_author}}": feed.author},
|
{"{{feed_author}}": feed.author},
|
||||||
{"{{feed_added}}": feed.added},
|
{"{{feed_added}}": feed.added},
|
||||||
@ -128,7 +114,7 @@ def replace_tags_in_text_message(entry: Entry) -> str:
|
|||||||
{"{{entry_read_modified}}": entry.read_modified},
|
{"{{entry_read_modified}}": entry.read_modified},
|
||||||
{"{{entry_summary}}": summary},
|
{"{{entry_summary}}": summary},
|
||||||
{"{{entry_summary_raw}}": entry.summary or ""},
|
{"{{entry_summary_raw}}": entry.summary or ""},
|
||||||
{"{{entry_text}}": entry_text},
|
{"{{entry_text}}": content or summary},
|
||||||
{"{{entry_title}}": entry.title},
|
{"{{entry_title}}": entry.title},
|
||||||
{"{{entry_updated}}": entry.updated},
|
{"{{entry_updated}}": entry.updated},
|
||||||
{"{{image_1}}": first_image},
|
{"{{image_1}}": first_image},
|
||||||
@ -141,6 +127,25 @@ def replace_tags_in_text_message(entry: Entry) -> str:
|
|||||||
return custom_message.replace("\\n", "\n")
|
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:
|
def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
|
||||||
"""Replace tags in embed.
|
"""Replace tags in embed.
|
||||||
|
|
||||||
@ -155,21 +160,17 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
|
|||||||
custom_reader: Reader = get_reader()
|
custom_reader: Reader = get_reader()
|
||||||
embed: CustomEmbed = get_embed(feed=feed, custom_reader=custom_reader)
|
embed: CustomEmbed = get_embed(feed=feed, custom_reader=custom_reader)
|
||||||
|
|
||||||
summary = ""
|
|
||||||
content = ""
|
content = ""
|
||||||
if entry.summary:
|
|
||||||
summary: str = entry.summary
|
|
||||||
summary = convert_html_to_md(summary)
|
|
||||||
|
|
||||||
if entry.content:
|
if entry.content:
|
||||||
for content_item in entry.content:
|
for content_item in entry.content:
|
||||||
content: str = content_item.value
|
content: str = content_item.value
|
||||||
content = convert_html_to_md(content)
|
|
||||||
|
|
||||||
if images := get_images_from_entry(entry=entry):
|
summary: str = entry.summary or ""
|
||||||
first_image: str = images[0][0]
|
|
||||||
else:
|
first_image = get_image(summary, content)
|
||||||
first_image = ""
|
|
||||||
|
summary = convert_html_to_md(summary)
|
||||||
|
content = convert_html_to_md(content)
|
||||||
|
|
||||||
entry_text: str = content or summary
|
entry_text: str = content or summary
|
||||||
|
|
||||||
@ -273,19 +274,12 @@ def get_embed(custom_reader: Reader, feed: Feed) -> CustomEmbed:
|
|||||||
Returns:
|
Returns:
|
||||||
Returns the contents from the embed tag.
|
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 embed := custom_reader.get_tag(feed, "embed", ""):
|
||||||
if type(embed) == str:
|
if type(embed) != str:
|
||||||
embed_data: dict[str, str | int] = json.loads(embed)
|
|
||||||
return get_embed_data(embed_data)
|
|
||||||
else:
|
|
||||||
return get_embed_data(embed)
|
return get_embed_data(embed)
|
||||||
|
embed_data: dict[str, str | int] = json.loads(embed) # type: ignore
|
||||||
|
return get_embed_data(embed_data)
|
||||||
|
|
||||||
return CustomEmbed(
|
return CustomEmbed(
|
||||||
title="",
|
title="",
|
||||||
|
@ -104,13 +104,8 @@ def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = Non
|
|||||||
# Check for new entries for every feed.
|
# Check for new entries for every feed.
|
||||||
reader.update_feeds()
|
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.
|
# Loop through the unread entries.
|
||||||
|
entries: Iterable[Entry] = reader.get_entries(feed=feed, read=False)
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
# Set the webhook to read, so we don't send it again.
|
# Set the webhook to read, so we don't send it again.
|
||||||
reader.set_entry_read(entry, True)
|
reader.set_entry_read(entry, True)
|
||||||
|
@ -22,7 +22,7 @@ from discord_rss_bot.custom_message import (
|
|||||||
CustomEmbed,
|
CustomEmbed,
|
||||||
get_custom_message,
|
get_custom_message,
|
||||||
get_embed,
|
get_embed,
|
||||||
get_images_from_entry,
|
get_image,
|
||||||
replace_tags_in_text_message,
|
replace_tags_in_text_message,
|
||||||
save_embed,
|
save_embed,
|
||||||
)
|
)
|
||||||
@ -396,10 +396,13 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
|
|||||||
html: str = ""
|
html: str = ""
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
first_image = ""
|
first_image = ""
|
||||||
first_image_text = ""
|
summary: str | None = entry.summary
|
||||||
if images := get_images_from_entry(entry=entry):
|
content = ""
|
||||||
first_image: str = images[0][0]
|
if entry.content:
|
||||||
first_image_text: str = images[0][1]
|
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>"
|
text: str = replace_tags_in_text_message(entry) or "<div class='text-muted'>No content available.</div>"
|
||||||
published = ""
|
published = ""
|
||||||
@ -416,7 +419,7 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
|
|||||||
|
|
||||||
entry_id: str = urllib.parse.quote(entry.id)
|
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>"
|
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">
|
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>
|
{blacklisted}{whitelisted}<a class="text-muted text-decoration-none" href="{entry.link}"><h2>{entry.title}</h2></a>
|
||||||
|
Reference in New Issue
Block a user