Prepared the post command for release
This commit is contained in:
+59
-22
@@ -8,6 +8,11 @@ import logging
|
|||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from praw.models import Submission, Subreddit
|
from praw.models import Submission, Subreddit
|
||||||
from utils.portal import Portal
|
from utils.portal import Portal
|
||||||
|
import aiohttp
|
||||||
|
from PIL import Image
|
||||||
|
from io import BytesIO
|
||||||
|
from datetime import datetime
|
||||||
|
from utils.datetime_tools import get_elapsed_time_milliseconds
|
||||||
|
|
||||||
class Post_Command(Base_Cog):
|
class Post_Command(Base_Cog):
|
||||||
def __init__(self, bot:commands.Bot):
|
def __init__(self, bot:commands.Bot):
|
||||||
@@ -16,12 +21,14 @@ class Post_Command(Base_Cog):
|
|||||||
|
|
||||||
@app_commands.command(name = "post", description = "Post an embed in the Current Channel with a link to the content")
|
@app_commands.command(name = "post", description = "Post an embed in the Current Channel with a link to the content")
|
||||||
@app_commands.check(Maintenance_Command.handle_check)
|
@app_commands.check(Maintenance_Command.handle_check)
|
||||||
async def post(self, ctx:discord.Interaction, url:str):
|
async def post(self, ctx:discord.Interaction, url:str, custom_note:str = None):
|
||||||
domain_info = urlparse(url)
|
domain_info = urlparse(url)
|
||||||
portal = Portal.instance()
|
portal = Portal.instance()
|
||||||
toplevel_domain = '.'.join(domain_info.netloc.split('.')[-2:])
|
toplevel_domain = '.'.join(domain_info.netloc.split('.')[-2:])
|
||||||
match toplevel_domain:
|
match toplevel_domain:
|
||||||
case "reddit.com":
|
case "reddit.com":
|
||||||
|
await ctx.response.defer()
|
||||||
|
|
||||||
subm:Submission = portal.reddit_adapter.fetch(url)
|
subm:Submission = portal.reddit_adapter.fetch(url)
|
||||||
embeds = []
|
embeds = []
|
||||||
|
|
||||||
@@ -35,30 +42,60 @@ class Post_Command(Base_Cog):
|
|||||||
image_urls.append(subm.url)
|
image_urls.append(subm.url)
|
||||||
self._logger.debug(f"Found {len(image_urls)} image urls for the post")
|
self._logger.debug(f"Found {len(image_urls)} image urls for the post")
|
||||||
|
|
||||||
embed = discord.Embed(url = "https://discord.com/humans.txt", color = int(portal.platforms_config["REDDIT"]["embed_color"], 16))
|
# Download and convert each image
|
||||||
embed.set_author(name = subm.title)
|
image_files:list[discord.File] = []
|
||||||
embeds.append(embed)
|
begin_conversion = datetime.now().timestamp()
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
index = 0
|
||||||
|
for image_url in image_urls:
|
||||||
|
async with session.get(image_url) as response:
|
||||||
|
response.raise_for_status() # Fehlerbehandlung für HTTP-Status
|
||||||
|
image_data = await response.read() # Bilddaten im RAM laden
|
||||||
|
|
||||||
embed.add_field(
|
original_image = Image.open(BytesIO(image_data)) # Originalbild laden
|
||||||
name = "Author",
|
webp_buffer = BytesIO() # Puffer für WebP-Bild
|
||||||
value = f"[u/{subm.author}](https://www.reddit.com/user/{subm.author})",
|
original_image.save(webp_buffer, format="WEBP", lossless=True) # WebP speichern
|
||||||
inline = True)
|
webp_buffer.seek(0)
|
||||||
embed.add_field(
|
image_file = discord.File(webp_buffer, filename = f"image_{index}.webp")
|
||||||
name = "Subreddit",
|
image_files.append(image_file)
|
||||||
value = f"[r/{subm.subreddit.display_name}]({url})",
|
|
||||||
inline = True)
|
index += 1
|
||||||
if subm.selftext:
|
|
||||||
embed.add_field(
|
self._logger.debug(f"Downloaded and converted {len(image_files)} images in {get_elapsed_time_milliseconds(datetime.now().timestamp() - begin_conversion)}")
|
||||||
name = "Discription",
|
|
||||||
value = subm.selftext,
|
|
||||||
inline = False)
|
|
||||||
|
|
||||||
for image_url in image_urls:
|
author = subm.author.name if subm.author else "Author not found"
|
||||||
embed = discord.Embed(url = "https://discord.com/humans.txt")
|
content = f":copyright: [{author}]({url})"
|
||||||
embed.set_image(url = image_url)
|
if custom_note:
|
||||||
embeds.append(embed)
|
content += f"\n> {custom_note}"
|
||||||
|
|
||||||
|
await ctx.followup.send(
|
||||||
|
content = content,
|
||||||
|
suppress_embeds = True,
|
||||||
|
files = image_files
|
||||||
|
)
|
||||||
|
|
||||||
await ctx.response.send_message(embeds = embeds)
|
# embed = discord.Embed(url = "https://discord.com/humans.txt", color = int(portal.platforms_config["REDDIT"]["embed_color"], 16))
|
||||||
|
# embed.set_author(name = subm.title)
|
||||||
|
# embeds.append(embed)
|
||||||
|
|
||||||
|
# embed.add_field(
|
||||||
|
# name = "Author",
|
||||||
|
# value = f"[u/{subm.author}](https://www.reddit.com/user/{subm.author})",
|
||||||
|
# inline = True)
|
||||||
|
# embed.add_field(
|
||||||
|
# name = "Subreddit",
|
||||||
|
# value = f"[r/{subm.subreddit.display_name}]({url})",
|
||||||
|
# inline = True)
|
||||||
|
# if subm.selftext:
|
||||||
|
# embed.add_field(
|
||||||
|
# name = "Discription",
|
||||||
|
# value = subm.selftext,
|
||||||
|
# inline = False)
|
||||||
|
|
||||||
|
# for image_url in image_urls:
|
||||||
|
# embed = discord.Embed(url = "https://discord.com/humans.txt")
|
||||||
|
# embed.set_image(url = image_url)
|
||||||
|
# embeds.append(embed)
|
||||||
|
|
||||||
# No domain for seperation found
|
# No domain for seperation found
|
||||||
case _:
|
case _:
|
||||||
|
|||||||
Reference in New Issue
Block a user