2021-03-27 18:40:42 +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 18:40:42 +01:00
|
|
|
from zerver.lib.response import json_success
|
2022-06-27 11:15:41 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
2021-03-27 18:40:42 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
RADARR_TOPIC_TEMPLATE = "{movie_title}".strip()
|
|
|
|
RADARR_TOPIC_TEMPLATE_TEST = "Radarr - Test".strip()
|
2023-05-11 19:36:14 +02:00
|
|
|
RADARR_TOPIC_TEMPLATE_APPLICATION_UPDATE = "Radarr - Application update"
|
2021-03-27 18:40:42 +01:00
|
|
|
RADARR_TOPIC_TEMPLATE_HEALTH_CHECK = "Health {level}".strip()
|
|
|
|
|
|
|
|
RADARR_MESSAGE_TEMPLATE_HEALTH_CHECK = "{message}.".strip()
|
2023-05-11 19:36:14 +02:00
|
|
|
RADARR_MESSAGE_TEMPLATE_APPLICATION_UPDATE = (
|
|
|
|
"Radarr was updated from {previous_version} to {new_version}."
|
|
|
|
)
|
2021-03-27 18:40:42 +01:00
|
|
|
RADARR_MESSAGE_TEMPLATE_MOVIE_RENAMED = "The movie {movie_title} has been renamed.".strip()
|
|
|
|
RADARR_MESSAGE_TEMPLATE_MOVIE_IMPORTED = "The movie {movie_title} has been imported.".strip()
|
|
|
|
RADARR_MESSAGE_TEMPLATE_MOVIE_IMPORTED_UPGRADE = (
|
|
|
|
"The movie {movie_title} has been upgraded from {old_quality} to {new_quality}.".strip()
|
|
|
|
)
|
|
|
|
RADARR_MESSAGE_TEMPLATE_MOVIE_GRABBED = "The movie {movie_title} has been grabbed.".strip()
|
|
|
|
|
2023-05-11 19:36:14 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"ApplicationUpdate",
|
|
|
|
"Test",
|
|
|
|
"Rename",
|
|
|
|
"Download",
|
|
|
|
"Health",
|
|
|
|
"Grab",
|
|
|
|
]
|
2021-03-27 18:40:42 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Radarr", all_event_types=ALL_EVENT_TYPES)
|
2021-03-27 18:40:42 +01:00
|
|
|
@has_request_variables
|
|
|
|
def api_radarr_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-06-27 11:15:41 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-03-27 18:40:42 +01:00
|
|
|
) -> HttpResponse:
|
|
|
|
body = get_body_for_http_request(payload)
|
|
|
|
subject = get_subject_for_http_request(payload)
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
check_send_webhook_message(
|
|
|
|
request, user_profile, subject, body, payload["eventType"].tame(check_string)
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-03-27 18:40:42 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
def get_subject_for_http_request(payload: WildValue) -> str:
|
|
|
|
event_type = payload["eventType"].tame(check_string)
|
2023-05-11 19:36:14 +02:00
|
|
|
if event_type == "Test":
|
|
|
|
return RADARR_TOPIC_TEMPLATE_TEST
|
|
|
|
elif event_type == "ApplicationUpdate":
|
|
|
|
return RADARR_TOPIC_TEMPLATE_APPLICATION_UPDATE
|
|
|
|
elif event_type == "Health":
|
|
|
|
return RADARR_TOPIC_TEMPLATE_HEALTH_CHECK.format(level=payload["level"].tame(check_string))
|
|
|
|
else:
|
|
|
|
return RADARR_TOPIC_TEMPLATE.format(
|
2022-06-27 11:15:41 +02:00
|
|
|
movie_title=payload["movie"]["title"].tame(check_string)
|
|
|
|
)
|
2021-03-27 18:40:42 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
def get_body_for_health_check_event(payload: WildValue) -> str:
|
|
|
|
return RADARR_MESSAGE_TEMPLATE_HEALTH_CHECK.format(
|
|
|
|
message=payload["message"].tame(check_string)
|
|
|
|
)
|
2021-03-27 18:40:42 +01:00
|
|
|
|
|
|
|
|
2023-05-11 19:36:14 +02:00
|
|
|
def get_body_for_application_update_event(payload: WildValue) -> str:
|
|
|
|
return RADARR_MESSAGE_TEMPLATE_APPLICATION_UPDATE.format(
|
|
|
|
previous_version=payload["previousVersion"].tame(check_string),
|
|
|
|
new_version=payload["newVersion"].tame(check_string),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
def get_body_for_movie_renamed_event(payload: WildValue) -> str:
|
|
|
|
return RADARR_MESSAGE_TEMPLATE_MOVIE_RENAMED.format(
|
|
|
|
movie_title=payload["movie"]["title"].tame(check_string)
|
|
|
|
)
|
2021-03-27 18:40:42 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
def get_body_for_movie_imported_upgrade_event(payload: WildValue) -> str:
|
2021-03-27 18:40:42 +01:00
|
|
|
data = {
|
2022-06-27 11:15:41 +02:00
|
|
|
"movie_title": payload["movie"]["title"].tame(check_string),
|
|
|
|
"new_quality": payload["movieFile"]["quality"].tame(check_string),
|
|
|
|
"old_quality": payload["deletedFiles"][0]["quality"].tame(check_string),
|
2021-03-27 18:40:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return RADARR_MESSAGE_TEMPLATE_MOVIE_IMPORTED_UPGRADE.format(**data)
|
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
def get_body_for_movie_imported_event(payload: WildValue) -> str:
|
|
|
|
return RADARR_MESSAGE_TEMPLATE_MOVIE_IMPORTED.format(
|
|
|
|
movie_title=payload["movie"]["title"].tame(check_string)
|
|
|
|
)
|
2021-03-27 18:40:42 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +02:00
|
|
|
def get_body_for_movie_grabbed_event(payload: WildValue) -> str:
|
|
|
|
return RADARR_MESSAGE_TEMPLATE_MOVIE_GRABBED.format(
|
|
|
|
movie_title=payload["movie"]["title"].tame(check_string)
|
|
|
|
)
|
2021-03-27 18:40:42 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:15:41 +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 18:40:42 +01:00
|
|
|
return get_setup_webhook_message("Radarr")
|
2023-05-11 19:36:14 +02:00
|
|
|
elif event_type == "ApplicationUpdate":
|
|
|
|
return get_body_for_application_update_event(payload)
|
2022-06-27 11:15:41 +02:00
|
|
|
elif event_type == "Health":
|
2021-03-27 18:40:42 +01:00
|
|
|
return get_body_for_health_check_event(payload)
|
2022-06-27 11:15:41 +02:00
|
|
|
elif event_type == "Rename":
|
2021-03-27 18:40:42 +01:00
|
|
|
return get_body_for_movie_renamed_event(payload)
|
2022-06-27 11:15:41 +02:00
|
|
|
elif event_type == "Download" and "isUpgrade" in payload:
|
2021-03-27 18:40:42 +01:00
|
|
|
if payload["isUpgrade"]:
|
|
|
|
return get_body_for_movie_imported_upgrade_event(payload)
|
|
|
|
else:
|
|
|
|
return get_body_for_movie_imported_event(payload)
|
2022-06-27 11:15:41 +02:00
|
|
|
elif event_type == "Grab":
|
2021-03-27 18:40:42 +01:00
|
|
|
return get_body_for_movie_grabbed_event(payload)
|
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event_type)
|