Improved multiple exceptions catching and fixed some other providers issues

* Backup files should be listed with newest ones first

Just like Sonarr and Radarr and everyone else.

* Add check_parser_binary() validation method

This is mainly to prevent the user from selecting mediainfo as the subtitles parser if it has not yet been installed on the user's system somewhere in the PATH.

* import JSONDecodeError from requests.exceptions  instead of json

Because sometimes it will return the simplejson one instead and that one won't be caught by the except clause.

* import JSONDecodeError from requests.exceptions  instead of json

Because sometimes it will return the simplejson one instead and that one won't be caught by the except clause.

Also fixed User-Agent assignment.
pull/2434/head
JayZed 8 months ago committed by GitHub
parent 1c25d125d3
commit ec85f6e172
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -7,6 +7,7 @@ import logging
import re import re
from urllib.parse import quote_plus from urllib.parse import quote_plus
from utilities.binaries import BinaryNotFound, get_binary
from literals import EXIT_VALIDATION_ERROR from literals import EXIT_VALIDATION_ERROR
from utilities.central import stop_bazarr from utilities.central import stop_bazarr
from subliminal.cache import region from subliminal.cache import region
@ -54,6 +55,14 @@ class Validator(OriginalValidator):
) )
def check_parser_binary(value):
try:
get_binary(value)
except BinaryNotFound as e:
raise ValidationError(f"Executable '{value}' not found in search path. Please install before making this selection.")
return True
validators = [ validators = [
# general section # general section
Validator('general.flask_secret_key', must_exist=True, default=hexlify(os.urandom(16)).decode(), Validator('general.flask_secret_key', must_exist=True, default=hexlify(os.urandom(16)).decode(),
@ -119,7 +128,7 @@ validators = [
Validator('general.dont_notify_manual_actions', must_exist=True, default=False, is_type_of=bool), Validator('general.dont_notify_manual_actions', must_exist=True, default=False, is_type_of=bool),
Validator('general.hi_extension', must_exist=True, default='hi', is_type_of=str, is_in=['hi', 'cc', 'sdh']), Validator('general.hi_extension', must_exist=True, default='hi', is_type_of=str, is_in=['hi', 'cc', 'sdh']),
Validator('general.embedded_subtitles_parser', must_exist=True, default='ffprobe', is_type_of=str, Validator('general.embedded_subtitles_parser', must_exist=True, default='ffprobe', is_type_of=str,
is_in=['ffprobe', 'mediainfo']), is_in=['ffprobe', 'mediainfo'], condition=check_parser_binary),
Validator('general.default_und_audio_lang', must_exist=True, default='', is_type_of=str), Validator('general.default_und_audio_lang', must_exist=True, default='', is_type_of=str),
Validator('general.default_und_embedded_subtitles_lang', must_exist=True, default='', is_type_of=str), Validator('general.default_und_embedded_subtitles_lang', must_exist=True, default='', is_type_of=str),
Validator('general.parse_embedded_audio_track', must_exist=True, default=False, is_type_of=bool), Validator('general.parse_embedded_audio_track', must_exist=True, default=False, is_type_of=bool),

@ -3,7 +3,7 @@
import logging import logging
import requests import requests
import datetime import datetime
import json from requests.exceptions import JSONDecodeError
from dogpile.cache import make_region from dogpile.cache import make_region
@ -34,13 +34,13 @@ class GetRadarrInfo:
if 'version' in radarr_json: if 'version' in radarr_json:
radarr_version = radarr_json['version'] radarr_version = radarr_json['version']
else: else:
raise json.decoder.JSONDecodeError raise JSONDecodeError
except json.decoder.JSONDecodeError: except JSONDecodeError:
try: try:
rv = f"{url_radarr()}/api/v3/system/status?apikey={settings.radarr.apikey}" rv = f"{url_radarr()}/api/v3/system/status?apikey={settings.radarr.apikey}"
radarr_version = requests.get(rv, timeout=int(settings.radarr.http_timeout), verify=False, radarr_version = requests.get(rv, timeout=int(settings.radarr.http_timeout), verify=False,
headers=headers).json()['version'] headers=headers).json()['version']
except json.decoder.JSONDecodeError: except JSONDecodeError:
logging.debug('BAZARR cannot get Radarr version') logging.debug('BAZARR cannot get Radarr version')
radarr_version = 'unknown' radarr_version = 'unknown'
except Exception: except Exception:

@ -3,7 +3,7 @@
import logging import logging
import requests import requests
import datetime import datetime
import json from requests.exceptions import JSONDecodeError
from dogpile.cache import make_region from dogpile.cache import make_region
@ -34,13 +34,13 @@ class GetSonarrInfo:
if 'version' in sonarr_json: if 'version' in sonarr_json:
sonarr_version = sonarr_json['version'] sonarr_version = sonarr_json['version']
else: else:
raise json.decoder.JSONDecodeError raise JSONDecodeError
except json.decoder.JSONDecodeError: except JSONDecodeError:
try: try:
sv = f"{url_sonarr()}/api/v3/system/status?apikey={settings.sonarr.apikey}" sv = f"{url_sonarr()}/api/v3/system/status?apikey={settings.sonarr.apikey}"
sonarr_version = requests.get(sv, timeout=int(settings.sonarr.http_timeout), verify=False, sonarr_version = requests.get(sv, timeout=int(settings.sonarr.http_timeout), verify=False,
headers=headers).json()['version'] headers=headers).json()['version']
except json.decoder.JSONDecodeError: except JSONDecodeError:
logging.debug('BAZARR cannot get Sonarr version') logging.debug('BAZARR cannot get Sonarr version')
sonarr_version = 'unknown' sonarr_version = 'unknown'
except Exception: except Exception:

@ -33,7 +33,7 @@ def get_restore_path():
def get_backup_files(fullpath=True): def get_backup_files(fullpath=True):
backup_file_pattern = os.path.join(get_backup_path(), 'bazarr_backup_v*.zip') backup_file_pattern = os.path.join(get_backup_path(), 'bazarr_backup_v*.zip')
file_list = glob(backup_file_pattern) file_list = glob(backup_file_pattern)
file_list.sort(key=os.path.getmtime) file_list.sort(key=os.path.getmtime, reverse=True)
if fullpath: if fullpath:
return file_list return file_list
else: else:

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import functools import functools
from json import JSONDecodeError from requests.exceptions import JSONDecodeError
import logging import logging
import re import re
import time import time

@ -3,6 +3,7 @@ import io
import logging import logging
import os import os
import json import json
from requests.exceptions import JSONDecodeError
from subzero.language import Language from subzero.language import Language
from guessit import guessit from guessit import guessit
@ -144,7 +145,7 @@ class KtuvitProvider(Provider):
self.session.headers["Pragma"] = "no-cache" self.session.headers["Pragma"] = "no-cache"
self.session.headers["Cache-Control"] = "no-cache" self.session.headers["Cache-Control"] = "no-cache"
self.session.headers["Content-Type"] = "application/json" self.session.headers["Content-Type"] = "application/json"
self.session.headers["User-Agent"]: os.environ.get( self.session.headers["User-Agent"] = os.environ.get(
"SZ_USER_AGENT", "Sub-Zero/2" "SZ_USER_AGENT", "Sub-Zero/2"
) )
@ -161,13 +162,13 @@ class KtuvitProvider(Provider):
is_success = self.parse_d_response( is_success = self.parse_d_response(
r, "IsSuccess", False, "Authentication to the provider" r, "IsSuccess", False, "Authentication to the provider"
) )
except json.decoder.JSONDecodeError: except JSONDecodeError:
logger.info("Failed to Login to Ktuvit") logger.info("Failed to Login to Ktuvit")
if not is_success: if not is_success:
error_message = '' error_message = ''
try: try:
error_message = self.parse_d_response(r, "ErrorMessage", "[None]") error_message = self.parse_d_response(r, "ErrorMessage", "[None]")
except json.decode.JSONDecoderError: except JSONDecodeError:
raise AuthenticationError( raise AuthenticationError(
"Error Logging in to Ktuvit Provider: " + str(r.content) "Error Logging in to Ktuvit Provider: " + str(r.content)
) )
@ -473,8 +474,8 @@ class KtuvitProvider(Provider):
try: try:
response_content = response.json() response_content = response.json()
except json.decoder.JSONDecodeError as ex: except JSONDecodeError as ex:
raise json.decoder.JSONDecodeError( raise JSONDecodeError(
"Unable to parse JSON returned while getting " + message, ex.doc, ex.pos "Unable to parse JSON returned while getting " + message, ex.doc, ex.pos
) )
else: else:
@ -486,11 +487,11 @@ class KtuvitProvider(Provider):
value = response_content.get(field, default_value) value = response_content.get(field, default_value)
if not value and value != default_value: if not value and value != default_value:
raise json.decoder.JSONDecodeError( raise JSONDecodeError(
"Missing " + message, str(response_content), 0 "Missing " + message, str(response_content), 0
) )
else: else:
raise json.decoder.JSONDecodeError( raise JSONDecodeError(
"Incomplete JSON returned while getting " + message, "Incomplete JSON returned while getting " + message,
str(response_content), str(response_content),
0 0

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import from __future__ import absolute_import
from json import JSONDecodeError from requests.exceptions import JSONDecodeError
import logging import logging
import random import random
import re import re

Loading…
Cancel
Save