diff --git a/src/radarr/client.py b/src/radarr/client.py new file mode 100644 index 0000000..72b3bf5 --- /dev/null +++ b/src/radarr/client.py @@ -0,0 +1,28 @@ +import json +from utils.custom_logger import logger + +class RadarrWebhookHandler: + def __init__(self, payload, discord_bot): + self.payload = payload + self.discord_bot = discord_bot + + # Global variables for the class to use in the embed creation + self.event_type = self.payload.get('eventType', 'N/A') + self.instance_name = self.payload.get('instanceName', 'N/A') + self.old_version = self.payload.get('previousVersion', 'N/A') + self.new_version = self.payload.get('newVersion', 'N/A') + self.movie_title = self.payload.get('movie', {}).get('title', 'N/A') + self.movie_year = self.payload.get('movie', {}).get('year', 'N/A') + self.release_data = self.payload.get('release', {}) + self.quality = self.release_data.get('quality', 'N/A') + self.size = self.release_data.get('size', 'N/A') + self.indexer = self.release_data.get('indexer', 'N/A') + self.release_title = self.release_data.get('releaseTitle', 'N/A') + self.custom_format_score = self.release_data.get('customFormatScore', 'N/A') + self.custom_formats = self.release_data.get('customFormats', []) + self.tmdb_id = self.payload.get('movie', {}).get('tmdbId', 'N/A') + self.embed_title = f"{self.movie_title} ({self.movie_year})" + + async def handle_webhook(self): + logger.info(f"Processing Radarr webhook payload for event type: {self.payload.get('eventType')}") + logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}") \ No newline at end of file diff --git a/src/sonarr/client.py b/src/sonarr/client.py new file mode 100644 index 0000000..0795e2a --- /dev/null +++ b/src/sonarr/client.py @@ -0,0 +1,50 @@ +import json +from utils.custom_logger import logger + +class SonarrWebhookHandler: + def __init__(self, payload, discord_bot): + self.payload = payload + self.discord_bot = discord_bot + self.initialize_global_variables() + self.check_request_type() + + def initialize_global_variables(self): + self.event_type = self.payload.get('eventType', 'N/A') + self.instance_name = self.payload.get('instanceName', 'N/A') + self.old_version = self.payload.get('previousVersion', 'N/A') + self.new_version = self.payload.get('newVersion', 'N/A') + self.series = self.payload.get('series', {}) + self.series_title = self.series.get('title', 'N/A') + self.release_data = self.payload.get('release', {}) + self.episodes = self.payload.get('episodes', {}) + self.tvdb_id = self.series.get('tvdbId', 'N/A') + self.quality = self.release_data.get('quality', 'N/A') + self.size = self.release_data.get('size', 'N/A') + self.indexer = self.release_data.get('indexer', 'N/A') + self.release_title = self.release_data.get('releaseTitle', 'N/A') + 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) + + def check_request_type(self): + if self.episode_count > 1: + self.handle_season_request() + else: + self.handle_episode_request() + + def handle_season_request(self): + self.season_number = self.episodes[0].get('seasonNumber', 'N/A') + self.formatted_season_number = f"{self.season_number:02d}" + self.embed_title = f"{self.series_title} (Season {self.formatted_season_number})" + + def handle_episode_request(self): + self.episode_title = self.episodes[0].get('title', 'N/A') + self.episode_number = self.episodes[0].get('episodeNumber', 'N/A') + self.season_number = self.episodes[0].get('seasonNumber', 'N/A') + self.formatted_episode_number = f"{self.episode_number:02d}" + self.formatted_season_number = f"{self.season_number:02d}" + self.embed_title = f"{self.series_title} (S{self.formatted_season_number}E{self.formatted_episode_number})" + + async def handle_webhook(self): + logger.info(f"Processing Sonarr webhook payload for event type: {self.event_type}") + logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}") \ No newline at end of file diff --git a/src/webhook/hook.py b/src/webhook/hook.py index 83cc057..2e737d6 100644 --- a/src/webhook/hook.py +++ b/src/webhook/hook.py @@ -1,6 +1,7 @@ from aiohttp import web from utils.custom_logger import logger +from src.sonarr.client import SonarrWebhookHandler class HandleWebHook: # Initialize the webhook receiver @@ -9,7 +10,7 @@ def __init__(self, discord_bot, host="0.0.0.0", port=2024): self.host = host self.port = port self.app = web.Application() - #self.app.router.add_post('/sonarr_webhook', self.handle_sonarr) + self.app.router.add_post('/sonarr_webhook', self.handle_sonarr) #self.app.router.add_post('/radarr_webhook', self.handle_radarr) #self.app.router.add_post('/plex_webhook', self.handle_plex) self.uvicorn_params = { @@ -18,6 +19,18 @@ def __init__(self, discord_bot, host="0.0.0.0", port=2024): "access_log": False, } + # Handle Sonarr webhook + async def handle_sonarr(self, request): + try: + payload = await request.json() + sonarr_handler = SonarrWebhookHandler(payload, self.discord_bot) + await sonarr_handler.handle_webhook() + except Exception as e: + logger.error(f"Error handling Sonarr webhook: {e}") + return web.Response(text='Error', status=500) + + return web.Response(text='OK') + # Start the webhook receiver async def start(self): try: