2022-03-22 02:14:44 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
from flask_restx import Resource, Namespace, reqparse
|
2022-03-22 02:14:44 +00:00
|
|
|
|
2023-07-26 23:34:49 +00:00
|
|
|
from app.database import TableEpisodes, TableShows, database, select
|
2022-05-01 12:00:20 +00:00
|
|
|
from subtitles.mass_download import episode_download_subtitles
|
|
|
|
from subtitles.indexer.series import store_subtitles
|
|
|
|
from utilities.path_mappings import path_mappings
|
|
|
|
|
2022-03-22 02:14:44 +00:00
|
|
|
from ..utils import authenticate
|
|
|
|
|
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
api_ns_webhooks_sonarr = Namespace('Webhooks Sonarr', description='Webhooks to trigger subtitles search based on '
|
|
|
|
'Sonarr episode file ID')
|
|
|
|
|
|
|
|
|
|
|
|
@api_ns_webhooks_sonarr.route('webhooks/sonarr')
|
2022-03-22 02:14:44 +00:00
|
|
|
class WebHooksSonarr(Resource):
|
2022-09-22 03:51:34 +00:00
|
|
|
post_request_parser = reqparse.RequestParser()
|
|
|
|
post_request_parser.add_argument('sonarr_episodefile_id', type=int, required=True, help='Episode file ID')
|
|
|
|
|
2022-03-22 02:14:44 +00:00
|
|
|
@authenticate
|
2022-09-22 03:51:34 +00:00
|
|
|
@api_ns_webhooks_sonarr.doc(parser=post_request_parser)
|
|
|
|
@api_ns_webhooks_sonarr.response(200, 'Success')
|
|
|
|
@api_ns_webhooks_sonarr.response(401, 'Not Authenticated')
|
2022-03-22 02:14:44 +00:00
|
|
|
def post(self):
|
2022-09-22 03:51:34 +00:00
|
|
|
"""Search for missing subtitles for a specific episode file id"""
|
|
|
|
args = self.post_request_parser.parse_args()
|
|
|
|
episode_file_id = args.get('sonarr_episodefile_id')
|
2022-03-22 02:14:44 +00:00
|
|
|
|
2023-07-26 23:34:49 +00:00
|
|
|
sonarrEpisodeId = database.execute(
|
|
|
|
select(TableEpisodes.sonarrEpisodeId, TableEpisodes.path)
|
|
|
|
.select_from(TableEpisodes)
|
|
|
|
.join(TableShows)
|
|
|
|
.where(TableEpisodes.episode_file_id == episode_file_id)) \
|
|
|
|
.first()
|
2022-03-22 02:14:44 +00:00
|
|
|
|
|
|
|
if sonarrEpisodeId:
|
2023-07-26 23:34:49 +00:00
|
|
|
store_subtitles(sonarrEpisodeId.path, path_mappings.path_replace(sonarrEpisodeId.path))
|
|
|
|
episode_download_subtitles(no=sonarrEpisodeId.sonarrEpisodeId, send_progress=True)
|
2022-03-22 02:14:44 +00:00
|
|
|
|
|
|
|
return '', 200
|