2021-03-27 03:56:36 +01:00
from django . http import HttpRequest , HttpResponse
2021-07-16 22:11:10 +02:00
from zerver . decorator import webhook_view
2022-11-17 09:30:48 +01:00
from zerver . lib . exceptions import UnsupportedWebhookEventTypeError
2021-07-16 22:11:10 +02:00
from zerver . lib . request import REQ , has_request_variables
2021-03-27 03:56:36 +01:00
from zerver . lib . response import json_success
2022-06-23 16:21:12 +02:00
from zerver . lib . validator import WildValue , check_bool , check_int , check_string , to_wild_value
2021-03-27 03:56:36 +01:00
from zerver . lib . webhooks . common import check_send_webhook_message , get_setup_webhook_message
from zerver . models import UserProfile
SONARR_TOPIC_TEMPLATE = " {series_title} " . strip ( )
SONARR_TOPIC_TEMPLATE_TEST = " Sonarr - Test " . strip ( )
SONARR_TOPIC_TEMPLATE_HEALTH_CHECK = " Health {level} " . strip ( )
SONARR_MESSAGE_TEMPLATE_SERIES_DELETED = " {series_title} has been deleted. " . strip ( )
SONARR_MESSAGE_TEMPLATE_HEALTH_CHECK = " {message} . " . strip ( )
SONARR_MESSAGE_TEMPLATE_EPISODES_RENAMED = " {series_title} episodes have been renamed. " . strip ( )
SONARR_MESSAGE_TEMPLATE_EPISODE_IMPORTED = (
" {series_title} - {series_number} x {episode_number} - {episode_name} has been imported. " . strip ( )
)
SONARR_MESSAGE_TEMPLATE_EPISODE_IMPORTED_UPGRADE = " {series_title} - {series_number} x {episode_number} - {episode_name} has been upgraded from {old_quality} to {new_quality} . " . strip ( )
SONARR_MESSAGE_TEMPLATE_EPISODE_GRABBED = (
" {series_title} - {series_number} x {episode_number} - {episode_name} has been grabbed. " . strip ( )
)
SONARR_MESSAGE_TEMPLATE_EPISODE_DELETED = (
" {series_title} - {series_number} x {episode_number} - {episode_name} has been deleted. " . strip ( )
)
SONARR_MESSAGE_TEMPLATE_EPISODE_DELETED_UPGRADE = " {series_title} - {series_number} x {episode_number} - {episode_name} has been deleted due to quality upgrade. " . strip ( )
2021-07-16 11:40:46 +02:00
ALL_EVENT_TYPES = [
" Grab " ,
" EpisodeFileDelete " ,
" Test " ,
" Download " ,
" SeriesDelete " ,
" Health " ,
" Rename " ,
]
@webhook_view ( " Sonarr " , all_event_types = ALL_EVENT_TYPES )
2021-03-27 03:56:36 +01:00
@has_request_variables
def api_sonarr_webhook (
request : HttpRequest ,
user_profile : UserProfile ,
2022-06-23 16:21:12 +02:00
payload : WildValue = REQ ( argument_type = " body " , converter = to_wild_value ) ,
2021-03-27 03:56:36 +01:00
) - > HttpResponse :
body = get_body_for_http_request ( payload )
2023-07-12 13:37:08 +02:00
topic = get_topic_for_http_request ( payload )
2021-03-27 03:56:36 +01:00
2022-06-23 16:21:12 +02:00
check_send_webhook_message (
2023-07-12 13:37:08 +02:00
request , user_profile , topic , body , payload [ " eventType " ] . tame ( check_string )
2022-06-23 16:21:12 +02:00
)
2022-01-31 13:44:02 +01:00
return json_success ( request )
2021-03-27 03:56:36 +01:00
2023-07-12 13:37:08 +02:00
def get_topic_for_http_request ( payload : WildValue ) - > str :
2022-06-23 16:21:12 +02:00
event_type = payload [ " eventType " ] . tame ( check_string )
2023-07-22 01:15:10 +02:00
if event_type == " Test " :
2021-03-27 03:56:36 +01:00
topic = SONARR_TOPIC_TEMPLATE_TEST
2022-06-23 16:21:12 +02:00
elif event_type == " Health " :
topic = SONARR_TOPIC_TEMPLATE_HEALTH_CHECK . format ( level = payload [ " level " ] . tame ( check_string ) )
2023-07-22 01:15:10 +02:00
else :
topic = SONARR_TOPIC_TEMPLATE . format (
series_title = payload [ " series " ] [ " title " ] . tame ( check_string )
)
2021-03-27 03:56:36 +01:00
return topic
2022-06-23 16:21:12 +02:00
def get_body_for_health_check_event ( payload : WildValue ) - > str :
return SONARR_MESSAGE_TEMPLATE_HEALTH_CHECK . format (
message = payload [ " message " ] . tame ( check_string )
)
2021-03-27 03:56:36 +01:00
2022-06-23 16:21:12 +02:00
def get_body_for_episodes_renamed_event ( payload : WildValue ) - > str :
return SONARR_MESSAGE_TEMPLATE_EPISODES_RENAMED . format (
series_title = payload [ " series " ] [ " title " ] . tame ( check_string )
)
2021-03-27 03:56:36 +01:00
2022-06-23 16:21:12 +02:00
def get_body_for_series_deleted_event ( payload : WildValue ) - > str :
return SONARR_MESSAGE_TEMPLATE_SERIES_DELETED . format (
series_title = payload [ " series " ] [ " title " ] . tame ( check_string )
)
2021-03-27 03:56:36 +01:00
2022-06-23 16:21:12 +02:00
def get_body_for_episode_imported_upgrade_event ( payload : WildValue ) - > str :
2021-03-27 03:56:36 +01:00
data = {
2022-06-23 16:21:12 +02:00
" series_title " : payload [ " series " ] [ " title " ] . tame ( check_string ) ,
" series_number " : payload [ " episodes " ] [ 0 ] [ " seasonNumber " ] . tame ( check_int ) ,
" episode_number " : payload [ " episodes " ] [ 0 ] [ " episodeNumber " ] . tame ( check_int ) ,
" episode_name " : payload [ " episodes " ] [ 0 ] [ " title " ] . tame ( check_string ) ,
" new_quality " : payload [ " episodeFile " ] [ " quality " ] . tame ( check_string ) ,
" old_quality " : payload [ " deletedFiles " ] [ 0 ] [ " quality " ] . tame ( check_string ) ,
2021-03-27 03:56:36 +01:00
}
return SONARR_MESSAGE_TEMPLATE_EPISODE_IMPORTED_UPGRADE . format ( * * data )
2022-06-23 16:21:12 +02:00
def get_body_for_episode_imported_event ( payload : WildValue ) - > str :
2021-03-27 03:56:36 +01:00
data = {
2022-06-23 16:21:12 +02:00
" series_title " : payload [ " series " ] [ " title " ] . tame ( check_string ) ,
" series_number " : payload [ " episodes " ] [ 0 ] [ " seasonNumber " ] . tame ( check_int ) ,
" episode_number " : payload [ " episodes " ] [ 0 ] [ " episodeNumber " ] . tame ( check_int ) ,
" episode_name " : payload [ " episodes " ] [ 0 ] [ " title " ] . tame ( check_string ) ,
2021-03-27 03:56:36 +01:00
}
return SONARR_MESSAGE_TEMPLATE_EPISODE_IMPORTED . format ( * * data )
2022-06-23 16:21:12 +02:00
def get_body_for_episode_grabbed_event ( payload : WildValue ) - > str :
2021-03-27 03:56:36 +01:00
data = {
2022-06-23 16:21:12 +02:00
" series_title " : payload [ " series " ] [ " title " ] . tame ( check_string ) ,
" series_number " : payload [ " episodes " ] [ 0 ] [ " seasonNumber " ] . tame ( check_int ) ,
" episode_number " : payload [ " episodes " ] [ 0 ] [ " episodeNumber " ] . tame ( check_int ) ,
" episode_name " : payload [ " episodes " ] [ 0 ] [ " title " ] . tame ( check_string ) ,
2021-03-27 03:56:36 +01:00
}
return SONARR_MESSAGE_TEMPLATE_EPISODE_GRABBED . format ( * * data )
2022-06-23 16:21:12 +02:00
def get_body_for_episode_deleted_upgrade_event ( payload : WildValue ) - > str :
2021-03-27 03:56:36 +01:00
data = {
2022-06-23 16:21:12 +02:00
" series_title " : payload [ " series " ] [ " title " ] . tame ( check_string ) ,
" series_number " : payload [ " episodes " ] [ 0 ] [ " seasonNumber " ] . tame ( check_int ) ,
" episode_number " : payload [ " episodes " ] [ 0 ] [ " episodeNumber " ] . tame ( check_int ) ,
" episode_name " : payload [ " episodes " ] [ 0 ] [ " title " ] . tame ( check_string ) ,
2021-03-27 03:56:36 +01:00
}
return SONARR_MESSAGE_TEMPLATE_EPISODE_DELETED_UPGRADE . format ( * * data )
2022-06-23 16:21:12 +02:00
def get_body_for_episode_deleted_event ( payload : WildValue ) - > str :
2021-03-27 03:56:36 +01:00
data = {
2022-06-23 16:21:12 +02:00
" series_title " : payload [ " series " ] [ " title " ] . tame ( check_string ) ,
" series_number " : payload [ " episodes " ] [ 0 ] [ " seasonNumber " ] . tame ( check_int ) ,
" episode_number " : payload [ " episodes " ] [ 0 ] [ " episodeNumber " ] . tame ( check_int ) ,
" episode_name " : payload [ " episodes " ] [ 0 ] [ " title " ] . tame ( check_string ) ,
2021-03-27 03:56:36 +01:00
}
return SONARR_MESSAGE_TEMPLATE_EPISODE_DELETED . format ( * * data )
2022-06-23 16:21:12 +02:00
def get_body_for_http_request ( payload : WildValue ) - > str :
event_type = payload [ " eventType " ] . tame ( check_string )
if event_type == " Test " :
2021-03-27 03:56:36 +01:00
return get_setup_webhook_message ( " Sonarr " )
2022-06-23 16:21:12 +02:00
elif event_type == " Health " :
2021-03-27 03:56:36 +01:00
return get_body_for_health_check_event ( payload )
2022-06-23 16:21:12 +02:00
elif event_type == " Rename " :
2021-03-27 03:56:36 +01:00
return get_body_for_episodes_renamed_event ( payload )
2022-06-23 16:21:12 +02:00
elif event_type == " SeriesDelete " :
2021-03-27 03:56:36 +01:00
return get_body_for_series_deleted_event ( payload )
2022-06-23 16:21:12 +02:00
elif event_type == " Download " and " isUpgrade " in payload :
if payload [ " isUpgrade " ] . tame ( check_bool ) :
2021-03-27 03:56:36 +01:00
return get_body_for_episode_imported_upgrade_event ( payload )
else :
return get_body_for_episode_imported_event ( payload )
2022-06-23 16:21:12 +02:00
elif event_type == " Grab " :
2021-03-27 03:56:36 +01:00
return get_body_for_episode_grabbed_event ( payload )
2022-06-23 16:21:12 +02:00
elif event_type == " EpisodeFileDelete " and " deleteReason " in payload :
if payload [ " deleteReason " ] . tame ( check_string ) == " upgrade " :
2021-03-27 03:56:36 +01:00
return get_body_for_episode_deleted_upgrade_event ( payload )
else :
return get_body_for_episode_deleted_event ( payload )
else :
2022-11-17 09:30:48 +01:00
raise UnsupportedWebhookEventTypeError ( event_type )