From b1a37943624b76e4e76ede54273144385dc4363a Mon Sep 17 00:00:00 2001 From: Zeroquinc Date: Thu, 30 May 2024 02:55:34 +0200 Subject: [PATCH] feature: new embeds for radarr, sonarr and plex --- api/plex/client.py | 128 +++++++++++----------------------------- api/radarr/client.py | 4 +- api/sonarr/client.py | 3 +- src/discord/bot.py | 2 +- src/radarr/functions.py | 13 +--- src/sonarr/functions.py | 17 +----- 6 files changed, 43 insertions(+), 124 deletions(-) diff --git a/api/plex/client.py b/api/plex/client.py index dc1a3d4..92afed0 100644 --- a/api/plex/client.py +++ b/api/plex/client.py @@ -1,9 +1,8 @@ import json import discord -from discord.utils import utcnow - -from config.globals import PLEX_ICON, DISCORD_THUMBNAIL, PLEX_PLAYING, PLEX_CONTENT +from config.globals import PLEX_ICON, PLEX_PLAYING, PLEX_CONTENT +from src.discord.embed import EmbedBuilder from utils.custom_logger import logger class PlexWebhookHandler: @@ -26,20 +25,23 @@ def __init__(self, payload, discord_bot): self.poster_url = self.payload.get('source_metadata_details', {}).get('poster_url', 'N/A') self.imdb_url = self.payload.get('source_metadata_details', {}).get('imdb_url', 'N/A') self.tvdb_url = self.payload.get('source_metadata_details', {}).get('thetvdb_url', 'N/A') + self.trakt_url = self.payload.get('source_metadata_details', {}).get('trakt_url', 'N/A') + self.critic_rating = self.payload.get('source_metadata_details', {}).get('critic_rating', 'N/A') + self.audience_rating = self.payload.get('source_metadata_details', {}).get('audience_rating', 'N/A') self.rating = self.payload.get('source_metadata_details', {}).get('rating', 'N/A') self.username = self.payload.get('stream_details', {}).get('username', 'N/A') + self.player = self.payload.get('stream_details', {}).get('player', 'N/A') self.product = self.payload.get('stream_details', {}).get('product', 'N/A') self.video_decision = self.payload.get('stream_details', {}).get('video_decision', 'N/A').capitalize() self.remaining_time = self.payload.get('stream_details', {}).get('remaining_time', 'N/A') self.duration_time = self.payload.get('stream_details', {}).get('duration_time', 'N/A') self.server_name = self.payload.get('server_info', {}).get('server_name', 'N/A') self.webhook_type = self.payload.get('server_info', {}).get('webhook_type', 'N/A') - + async def handle_webhook(self): logger.info(f"Processing Plex webhook payload for event type: {self.webhook_type}") logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}") - channel_id = self.determine_channel_id() - await self.dispatch_embed(channel_id) + await self.dispatch_embed() def determine_channel_id(self): channel_ids = { @@ -64,132 +66,70 @@ def generate_embed(self): } # Get the embed creation method for the event type - generate_embed = embed_creators.get(self.webhook_type, self.embed_for_default) + generate_embed = embed_creators.get(self.webhook_type) # Call the embed creation method and return the embed return generate_embed() - def init_embed(self, title, color, author): - embed = discord.Embed(title=title, color=color) - embed.set_author(name=author, icon_url=PLEX_ICON) - embed.url = self.imdb_url if self.imdb_url != 'N/A' else self.tvdb_url - timestamp = utcnow() - embed.timestamp = timestamp - embed.set_image(url=DISCORD_THUMBNAIL) - - return embed - def embed_for_nowplaying(self): - embed_title = f"{self.title} ({self.year})" if self.media_type == "Movie" else f"{self.title} (S{self.season_number}E{self.episode_number})" - embed = self.init_embed(embed_title, 0x1C4673, f"Plex - Streaming {self.media_type}") - + description = f"{self.title} ({self.year})" if self.media_type == "Movie" else f"{self.title} (S{self.season_number}E{self.episode_number})" + embed = EmbedBuilder(title="Playing on Plex", description=f"{description}", color=0xe5a00d) poster_path = self.poster_url if poster_path: embed.set_thumbnail(url=poster_path) - - embed.add_field(name=":arrow_forward: Now Streaming", value=f"{self.remaining_time} remaining", inline=True) - embed.set_footer(text=f"{self.server_name} | {self.username} | {self.product} | {self.video_decision}") - + embed.set_footer(text=f"{self.username} • {self.video_decision} • {self.player}", icon_url=PLEX_ICON) return embed def embed_for_nowresuming(self): - embed_title = f"{self.title} ({self.year})" if self.media_type == "Movie" else f"{self.title} (S{self.season_number}E{self.episode_number})" - embed = self.init_embed(embed_title, 0x3587DE, f"Plex - Streaming {self.media_type}") - + description = f"{self.title} ({self.year})" if self.media_type == "Movie" else f"{self.title} (S{self.season_number}E{self.episode_number})" + embed = EmbedBuilder(title="Resuming on Plex", description=f"{description}", color=0xe5a00d) poster_path = self.poster_url if poster_path: embed.set_thumbnail(url=poster_path) - - embed.add_field(name=":play_pause: Now Resuming", value=f"{self.remaining_time} remaining", inline=True) - embed.set_footer(text=f"{self.server_name} | {self.username} | {self.product} | {self.video_decision}") - + embed.set_footer(text=f"{self.username} • {self.video_decision} • {self.player}", icon_url=PLEX_ICON) return embed def embed_for_finished(self): - embed_title = f"{self.title} ({self.year})" if self.media_type == "Movie" else f"{self.title} (S{self.season_number}E{self.episode_number})" - embed = self.init_embed(embed_title, 0x891836, f"Plex - Streaming {self.media_type}") - + description = f"{self.title} ({self.year})" if self.media_type == "Movie" else f"{self.title} (S{self.season_number}E{self.episode_number})" + embed = EmbedBuilder(title="Finished on Plex", description=f"{description}", color=0xe5a00d) poster_path = self.poster_url if poster_path: embed.set_thumbnail(url=poster_path) - - embed.add_field(name=":white_check_mark: Finished", value=f"{self.duration_time} total", inline=True) - embed.set_footer(text=f"{self.server_name} | {self.username} | {self.product} | {self.video_decision}") - + embed.set_footer(text=f"{self.username} • {self.video_decision} • {self.player}", icon_url=PLEX_ICON) return embed def embed_for_newcontent_episode(self): - embed_title = f"{self.title} (S{self.season_number}E{self.episode_number})" - embed = self.init_embed(embed_title, 0xE91655, "Plex - New Episode") - embed.description = self.summary - + description = f"{self.title} (S{self.season_number}E{self.episode_number})\nTMDb: {self.rating}" + embed = EmbedBuilder(title="New Episode added to Plex", description=description, color=0xe5a00d) poster_path = self.poster_url if poster_path: embed.set_thumbnail(url=poster_path) - - fields = { - "Quality": (self.quality, True), - "Runtime": (self.remaining_time, True), - "Air date": (self.air_date, True), - } - - for name, (value, inline) in fields.items(): - embed.add_field(name=name, value=value, inline=inline) - - embed.set_footer(text=self.server_name) - + embed.set_footer(text=f"{self.server_name}", icon_url=PLEX_ICON) return embed def embed_for_newcontent_season(self): - embed_title = self.title - embed = self.init_embed(embed_title, 0x169CE9, "Plex - New Season") - embed.description = self.summary - + description = f"{self.title}\n\nSeason {self.season_number} ({self.episode_count} episodes)" + embed = EmbedBuilder(title="New Season added to Plex", description=description, color=0xe5a00d) poster_path = self.poster_url if poster_path: embed.set_thumbnail(url=poster_path) - - fields = { - "Season": (self.season_number, True), - "Episodes": (self.episode_count, True), - "Details": (f"[IMDb]({self.imdb_url})", True), - } - - for name, (value, inline) in fields.items(): - embed.add_field(name=name, value=value, inline=inline) - - embed.set_footer(text=self.server_name) - + embed.set_footer(text=f"{self.server_name}", icon_url=PLEX_ICON) return embed def embed_for_newcontent_movie(self): - embed_title = f"{self.title} ({self.year})" - embed = self.init_embed(embed_title, 0x7CE916, "Plex - New Movie") - embed.description = self.summary - + description = f"[{self.title} ({self.year})]({self.imdb_url})\n\nRotten Tomatoes: {self.rating} :popcorn:\nIMDb: {self.audience_rating} :popcorn:" + embed = EmbedBuilder(title="New Movie added to Plex", description=description, color=0xe5a00d) poster_path = self.poster_url if poster_path: embed.set_thumbnail(url=poster_path) - - fields = { - "Quality": (self.quality, True), - "Genres": (self.genres, True), - "Release date": (self.release_date, True), - "Rotten Tomatoes": (f":popcorn: {self.rating}", True), - "Runtime": (self.remaining_time, True), - } - - for name, (value, inline) in fields.items(): - embed.add_field(name=name, value=value, inline=inline) - - embed.set_footer(text=self.server_name) - - return embed - - def embed_for_default(self): - embed = discord.Embed (title=" ") + embed.set_footer(text=f"{self.genres}", icon_url=PLEX_ICON) return embed - async def dispatch_embed(self, channel_id): + async def dispatch_embed(self): embed = self.generate_embed() - await self.discord_bot.dispatch_embed(channel_id, embed) \ No newline at end of file + channel_id = self.determine_channel_id() + channel = self.discord_bot.bot.get_channel(channel_id) # Get the Channel object using the ID + if channel: + await embed.send_embed(channel) + else: + logger.error(f"Channel with ID {channel_id} not found.") \ No newline at end of file diff --git a/api/radarr/client.py b/api/radarr/client.py index 63fa0fa..86bcbb3 100644 --- a/api/radarr/client.py +++ b/api/radarr/client.py @@ -27,8 +27,8 @@ def initialize_global_variables(self): self.custom_format_score = self.release_data.get('customFormatScore', 'N/A') self.custom_formats = self.release_data.get('customFormats', []) self.tmdb_id = self.movie.get('tmdbId', 'N/A') - self.embed_title = f"{self.movie_title} ({self.movie_year})" - self.poster = TMDb.movie_poster_path(self.tmdb_id) + if self.event_type != 'Test': + self.poster = TMDb.movie_poster_path(self.tmdb_id) async def handle_webhook(self): logger.info(f"Processing Radarr webhook payload for event type: {self.event_type}") diff --git a/api/sonarr/client.py b/api/sonarr/client.py index 274acc0..76003f9 100644 --- a/api/sonarr/client.py +++ b/api/sonarr/client.py @@ -29,7 +29,8 @@ def initialize_global_variables(self): self.custom_format_score = self.release_data.get('customFormatScore', 'N/A') self.custom_formats = self.release_data.get('customFormats', []) self.episode_count = len(self.episodes) - self.poster = TMDb.show_poster_path(self.tvdb_id) + if self.event_type != 'Test': + self.poster = TMDb.show_poster_path(self.tvdb_id) def check_request_type(self): if self.episode_count > 1: diff --git a/src/discord/bot.py b/src/discord/bot.py index e3d057b..d1441ec 100644 --- a/src/discord/bot.py +++ b/src/discord/bot.py @@ -48,6 +48,6 @@ async def dispatch_embed(self, channel_id, embed): return try: await channel.send(embed=embed) - logger.info(f"Embed dispatched with title: {embed.title} and author: {embed.author.name}") + logger.info(f"Embed dispatched with title: {embed.title}") except Exception as e: logger.error(f"Error dispatching embed: {e}") \ No newline at end of file diff --git a/src/radarr/functions.py b/src/radarr/functions.py index 0009e38..67afb08 100644 --- a/src/radarr/functions.py +++ b/src/radarr/functions.py @@ -6,18 +6,9 @@ async def process_webhook(handler, channel): def build_grab_embed(): - embed = EmbedBuilder(title=handler.embed_title, color=0x9e7a18) - embed.set_author(name=f"{handler.instance_name} - {handler.event_type}", icon_url=RADARR_ICON) + embed = EmbedBuilder(title="A new grab by Radarr", description=f"```{handler.release_title}```\n{Formatter.format_custom_formats(handler.custom_format_score, handler.custom_formats)}", color=0x9e7a18) embed.set_thumbnail(url=handler.poster) - fields = { - "Quality": (handler.quality, True), - "Size": (Converter.bytes_to_human_readable(handler.size), True), - "Indexer": (Formatter.indexer_value(handler.indexer), True), - "Release": (handler.release_title, False), - "Custom Formats": (Formatter.format_custom_formats(handler.custom_format_score, handler.custom_formats), False), - } - for name, (value, inline) in fields.items(): - embed.add_field(name=name, value=value, inline=inline) + embed.set_footer(text=f"{handler.quality} • {Converter.bytes_to_human_readable(handler.size)} • {handler.indexer}", icon_url=RADARR_ICON) return embed def build_download_embed(): diff --git a/src/sonarr/functions.py b/src/sonarr/functions.py index 043c724..1856ed1 100644 --- a/src/sonarr/functions.py +++ b/src/sonarr/functions.py @@ -6,22 +6,9 @@ async def process_webhook(handler, channel): def build_grab_embed(): - embed = EmbedBuilder(title=handler.embed_title, color=0xadd9c9) - embed.set_author(name=f"{handler.instance_name} - {handler.event_type}", icon_url=SONARR_ICON) + embed = EmbedBuilder(title="A new grab by Sonarr", description=f"```{handler.release_title}```\n{Formatter.format_custom_formats(handler.custom_format_score, handler.custom_formats)}", color=0xadd9c9) embed.set_thumbnail(url=handler.poster) - if len(handler.episodes) > 1: - embed.add_field(name="Episodes", value=handler.episode_count, inline=False) - else: - embed.add_field(name="Episode", value=handler.episode_title, inline=False) - fields = { - "Quality": (handler.quality, True), - "Size": (Converter.bytes_to_human_readable(handler.size), True), - "Indexer": (Formatter.indexer_value(handler.indexer), True), - "Release": (handler.release_title, False), - "Custom Formats": (Formatter.format_custom_formats(handler.custom_format_score, handler.custom_formats), False), - } - for name, (value, inline) in fields.items(): - embed.add_field(name=name, value=value, inline=inline) + embed.set_footer(text=f"{handler.quality} • {Converter.bytes_to_human_readable(handler.size)} • {handler.indexer}", icon_url=SONARR_ICON) return embed def build_download_embed():