Add support for webhook embeds

This commit is contained in:
2023-01-22 23:00:03 +01:00
parent 4bc2a7dc6f
commit 269948303e
9 changed files with 804 additions and 42 deletions

View File

@ -13,11 +13,19 @@ from starlette.responses import RedirectResponse
from discord_rss_bot import settings
from discord_rss_bot.custom_filters import encode_url, entry_is_blacklisted, entry_is_whitelisted
from discord_rss_bot.custom_message import get_custom_message, get_images_from_entry, replace_tags
from discord_rss_bot.custom_message import (
CustomEmbed,
get_custom_message,
get_embed,
get_images_from_entry,
replace_tags_in_text_message,
save_embed,
)
from discord_rss_bot.feeds import get_entry_from_id, send_entry_to_discord, send_to_discord
from discord_rss_bot.filter.blacklist import get_blacklist_content, get_blacklist_summary, get_blacklist_title
from discord_rss_bot.filter.whitelist import get_whitelist_content, get_whitelist_summary, get_whitelist_title
from discord_rss_bot.markdown import convert_html_to_md
from discord_rss_bot.missing_tags import add_missing_tags
from discord_rss_bot.search import create_html_for_search_results
from discord_rss_bot.settings import default_custom_message, get_reader, list_webhooks
@ -367,6 +375,125 @@ async def get_custom(feed_url, request: Request):
return templates.TemplateResponse("custom.html", context)
@app.get("/embed", response_class=HTMLResponse)
async def get_embed_page(feed_url, request: Request):
"""Get the custom message. This is used when sending the message to Discord.
Args:
feed_url: What feed we should get the custom message for.
request: The HTTP request.
Returns:
custom.html
"""
# Make feed_url a valid URL.
url: str = urllib.parse.unquote(feed_url)
feed: Feed = reader.get_feed(url)
# Get previous data, this is used when creating the form.
embed: CustomEmbed = get_embed(reader, feed)
context = {
"request": request,
"feed": feed,
"title": embed.title,
"description": embed.description,
"color": embed.color,
"image_url": embed.image_url,
"thumbnail_url": embed.thumbnail_url,
"author_name": embed.author_name,
"author_url": embed.author_url,
"author_icon_url": embed.author_icon_url,
"footer_text": embed.footer_text,
"footer_icon_url": embed.footer_icon_url,
}
# Get the first entry, this is used to show the user what the custom message will look like.
entries: Iterable[Entry] = reader.get_entries(feed=feed, limit=1)
if custom_embed := get_embed(reader, feed_url):
context["custom_embed"] = custom_embed
for entry in entries:
# Append to context.
context["entry"] = entry
return templates.TemplateResponse("embed.html", context)
@app.post("/embed", response_class=HTMLResponse)
async def set_embed_page(
feed_url=Form(),
title=Form(""),
description=Form(""),
color=Form(""),
image_url=Form(""),
thumbnail_url=Form(""),
author_name=Form(""),
author_url=Form(""),
author_icon_url=Form(""),
footer_text=Form(""),
footer_icon_url=Form(""),
):
"""Set the embed settings.
Args:
feed_url: What feed we should get the custom message for.
request: The HTTP request.
Returns:
custom.html
"""
# Make feed_url a valid URL.
url: str = urllib.parse.unquote(feed_url)
feed: Feed = reader.get_feed(url)
custom_embed: CustomEmbed = get_embed(reader, feed)
# Get the data from the form.
custom_embed.title = title
custom_embed.description = description
custom_embed.color = color
custom_embed.image_url = image_url
custom_embed.thumbnail_url = thumbnail_url
custom_embed.author_name = author_name
custom_embed.author_url = author_url
custom_embed.author_icon_url = author_icon_url
custom_embed.footer_text = footer_text
custom_embed.footer_icon_url = footer_icon_url
# Save the data.
save_embed(reader, feed_url, custom_embed)
# Clean URL is used to redirect to the feed page.
clean_url: str = urllib.parse.quote(feed_url)
return RedirectResponse(url=f"/feed/?feed_url={clean_url}", status_code=303)
@app.post("/use_embed")
async def set_should_use_embed(feed_url=Form()):
url: str = urllib.parse.unquote(feed_url)
print(f"Setting should_send_embed to True for {url}")
feed: Feed = reader.get_feed(url)
reader.set_tag(feed, "should_send_embed", True) # type: ignore
return RedirectResponse(url=f"/feed/?feed_url={feed_url}", status_code=303)
@app.post("/use_text")
async def set_should_use_text(feed_url=Form()):
url: str = urllib.parse.unquote(feed_url)
print(f"Setting should_send_embed to False for {url}")
feed: Feed = reader.get_feed(url)
reader.set_tag(feed, "should_send_embed", False) # type: ignore
return RedirectResponse(url=f"/feed/?feed_url={feed_url}", status_code=303)
@app.get("/add", response_class=HTMLResponse)
def get_add(request: Request):
"""
@ -408,7 +535,16 @@ async def get_feed(feed_url, request: Request):
# Create the html for the entries.
html: str = create_html_for_feed(entries)
context = {"request": request, "feed": feed, "entries": entries, "feed_counts": feed_counts, "html": html}
should_send_embed: bool = bool(reader.get_tag(feed, "should_send_embed"))
context = {
"request": request,
"feed": feed,
"entries": entries,
"feed_counts": feed_counts,
"html": html,
"should_send_embed": should_send_embed,
}
return templates.TemplateResponse("feed.html", context)
@ -433,7 +569,7 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
first_image_text: str = images[0][1]
# Get the text from the entry.
text = replace_tags(entry.feed, entry)
text = replace_tags_in_text_message(entry.feed, entry)
if not text:
text = "<div class='text-muted'>No content available.</div>"
@ -611,23 +747,7 @@ def startup() -> None:
"""This is called when the server starts.
It reads the settings file and starts the scheduler."""
# Add default feed message if it doesn't exist.
# This was added in version 0.2.0.
for feed in reader.get_feeds():
try:
reader.get_tag(feed, "custom_message")
except TagNotFoundError:
reader.set_tag(feed.url, "custom_message", default_custom_message) # type: ignore
reader.set_tag(feed.url, "has_custom_message", True) # type: ignore
# Add has_custom_message tag if it doesn't exist.
try:
reader.get_tag(feed, "has_custom_message")
except TagNotFoundError:
if reader.get_tag(feed, "custom_message") == default_custom_message:
reader.set_tag(feed.url, "has_custom_message", False) # type: ignore
else:
reader.set_tag(feed.url, "has_custom_message", True) # type: ignore
add_missing_tags(reader=reader)
scheduler: BackgroundScheduler = BackgroundScheduler()