Skip to content

Commit

Permalink
feature: support for sonarr / radarr
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroquinc committed May 23, 2024
1 parent a3c8f6f commit cca17f5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/radarr/client.py
Original file line number Diff line number Diff line change
@@ -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)}")
50 changes: 50 additions & 0 deletions src/sonarr/client.py
Original file line number Diff line number Diff line change
@@ -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)}")
15 changes: 14 additions & 1 deletion src/webhook/hook.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand All @@ -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:
Expand Down

0 comments on commit cca17f5

Please sign in to comment.