From ae108adcb062dee26527b9f07cbec858a013363a Mon Sep 17 00:00:00 2001 From: morpheus65535 <5130500+morpheus65535@users.noreply.github.com> Date: Wed, 12 Sep 2018 23:17:46 -0400 Subject: [PATCH] Removed dependency on Pillow(PIL) module and added button to check Soanrr and Radarr connection settings #80 --- bazarr.py | 41 ++++++------ views/settings.tpl | 156 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 168 insertions(+), 29 deletions(-) diff --git a/bazarr.py b/bazarr.py index 0cd93ebac..3cecdbfcd 100644 --- a/bazarr.py +++ b/bazarr.py @@ -62,16 +62,12 @@ bottle.TEMPLATE_PATH.insert(0, os.path.join(os.path.dirname(__file__), 'views/') bottle.debug(True) bottle.TEMPLATES.clear() -from json import dumps import itertools import operator import requests import pretty from datetime import datetime, timedelta -from PIL import Image from io import BytesIO -from fdsend import send_file -import urllib import math import ast import hashlib @@ -159,15 +155,14 @@ def image_proxy(url): apikey = get_sonarr_settings()[4] url_image = url_sonarr_short + '/' + url + '?apikey=' + apikey try: - img_pil = Image.open(BytesIO(requests.get(url_sonarr + '/api' + url_image.split(url_sonarr)[1], timeout=15, verify=False).content)) + image_buffer = BytesIO(requests.get(url_sonarr + '/api' + url_image.split(url_sonarr)[1], timeout=15, verify=False).content) except: return None else: - img_buffer = BytesIO() - img_pil.tobytes() - img_pil.save(img_buffer, img_pil.format) - img_buffer.seek(0) - return send_file(img_buffer, ctype=img_pil.format) + image_buffer.seek(0) + bytes = image_buffer.read() + response.set_header('Content-type', 'image/jpeg') + return bytes @route(base_url + 'image_proxy_movies/', method='GET') @custom_auth_basic(check_credentials) @@ -177,16 +172,15 @@ def image_proxy_movies(url): apikey = get_radarr_settings()[4] try: url_image = (url_radarr_short + '/' + url + '?apikey=' + apikey).replace('/fanart.jpg', '/banner.jpg') - img_pil = Image.open(BytesIO(requests.get(url_radarr + '/api' + url_image.split(url_radarr)[1], timeout=15, verify=False).content)) + image_buffer = BytesIO(requests.get(url_radarr + '/api' + url_image.split(url_radarr)[1], timeout=15, verify=False).content) except: url_image = url_radarr_short + '/' + url + '?apikey=' + apikey - img_pil = Image.open(BytesIO(requests.get(url_radarr + '/api' + url_image.split(url_radarr)[1], timeout=15, verify=False).content)) - - img_buffer = BytesIO() - img_pil.tobytes() - img_pil.save(img_buffer, img_pil.format) - img_buffer.seek(0) - return send_file(img_buffer, ctype=img_pil.format) + image_buffer = BytesIO(requests.get(url_radarr + '/api' + url_image.split(url_radarr)[1], timeout=15, verify=False).content) + else: + image_buffer.seek(0) + bytes = image_buffer.read() + response.set_header('Content-type', 'image/jpeg') + return bytes @route(base_url) @@ -1587,6 +1581,17 @@ def api_history(): c.close() return dict(subtitles=data) +@route(base_url + 'test_url/', method='GET') +@custom_auth_basic(check_credentials) +def test_url(url): + try: + result = requests.get(url).json()['version'] + except: + return dict(status=False, version=result) + else: + return dict(status=True, version=result) + + logging.info('Bazarr is started and waiting for request on http://' + str(ip) + ':' + str(port) + str(base_url)) run(host=ip, port=port, server='waitress') logging.info('Bazarr has been stopped.') diff --git a/views/settings.tpl b/views/settings.tpl index 509ba7ebb..c6c19f57a 100644 --- a/views/settings.tpl +++ b/views/settings.tpl @@ -569,6 +569,23 @@
Connection settings
+
+
+ +
+
+ +
+
+
+ + +
+
+
+
@@ -576,7 +593,7 @@
- +
@@ -594,7 +611,7 @@
- +
@@ -611,7 +628,7 @@
- +
- +
@@ -640,7 +657,7 @@
- +
@@ -678,6 +695,23 @@
Connection settings
+
+
+ +
+
+ +
+
+
+ + +
+
+
+
@@ -685,7 +719,7 @@
- +
@@ -703,7 +737,7 @@
- +
@@ -720,7 +754,7 @@
- +
- +
@@ -749,7 +783,7 @@
- +
@@ -1230,7 +1264,7 @@ .tab() ; - $('a:not(.tabs), button:not(.cancel)').click(function(){ + $('a:not(.tabs), button:not(.cancel, .test)').click(function(){ $('#loader').addClass('active'); }) @@ -1333,6 +1367,9 @@ $('#settings_use_sonarr').checkbox({ onChecked: function() { $("#sonarr_tab").removeClass('disabled'); + $('#sonarr_validated').checkbox('uncheck'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); }, onUnchecked: function() { $("#sonarr_tab").addClass('disabled'); @@ -1350,6 +1387,9 @@ $('#settings_use_radarr').checkbox({ onChecked: function() { $("#radarr_tab").removeClass('disabled'); + $('#sonarr_validated').checkbox('uncheck'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); }, onUnchecked: function() { $("#radarr_tab").addClass('disabled'); @@ -1560,6 +1600,14 @@ } ] }, + sonarr_validated_checkbox : { + depends: 'settings_general_use_sonarr', + rules : [ + { + type : 'checked' + } + ] + }, settings_sonarr_ip : { depends: 'settings_general_use_sonarr', rules : [ @@ -1590,6 +1638,14 @@ } ] }, + radarr_validated_checkbox : { + depends: 'settings_general_use_radarr', + rules : [ + { + type : 'checked' + } + ] + }, settings_radarr_ip : { depends: 'settings_general_use_radarr', rules : [ @@ -1676,4 +1732,82 @@ $('.form').form('validate form'); $('#loader').removeClass('active'); }) + + $('#sonarr_validate').click(function() { + if ($('#sonarr_ssl_div').checkbox('is checked')) { + sonarr_url = 'https://'; + } else { + sonarr_url = 'http://'; + } + sonarr_url += $('#settings_sonarr_ip').val() + ":" + $('#settings_sonarr_port').val() + "/" + $('#settings_sonarr_baseurl').val().replace(/^\/|\/$/g, '') + "/api/system/status?apikey=" + $('#settings_sonarr_apikey').val(); + + $.getJSON("{{base_url}}test_url/" + encodeURIComponent(sonarr_url), function (data) { + if (data.status) { + $('#sonarr_validated').checkbox('check'); + $('#sonarr_validation_result').text('Test successful: Sonarr v' + data.version).css('color', 'green'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + } else { + $('#sonarr_validated').checkbox('uncheck'); + $('#sonarr_validation_result').text('Test failed').css('color', 'red'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + } + }); + }) + + $('.sonarr_config').keyup(function() { + $('#sonarr_validated').checkbox('uncheck'); + $('#sonarr_validation_result').text('You must test your Sonarr connection settings before saving settings.').css('color', 'red'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + }) + + $('#settings_sonarr_ssl').change(function() { + $('#sonarr_validated').checkbox('uncheck'); + $('#sonarr_validation_result').text('You must test your Sonarr connection settings before saving settings.').css('color', 'red'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + }) + + $("#sonarr_validated").checkbox('check'); + + $('#radarr_validate').click(function() { + if ($('#radarr_ssl_div').checkbox('is checked')) { + radarr_url = 'https://'; + } else { + radarr_url = 'http://'; + } + radarr_url += $('#settings_radarr_ip').val() + ":" + $('#settings_radarr_port').val() + "/" + $('#settings_radarr_baseurl').val().replace(/^\/|\/$/g, '') + "/api/system/status?apikey=" + $('#settings_radarr_apikey').val(); + + $.getJSON("{{base_url}}test_url/" + encodeURIComponent(radarr_url), function (data) { + if (data.status) { + $('#radarr_validated').checkbox('check'); + $('#radarr_validation_result').text('Test successful: Radarr v' + data.version).css('color', 'green'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + } else { + $('#radarr_validated').checkbox('uncheck'); + $('#radarr_validation_result').text('Test failed').css('color', 'red'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + } + }); + }) + + $('.radarr_config').keyup(function() { + $('#radarr_validated').checkbox('uncheck'); + $('#radarr_validation_result').text('You must test your Sonarr connection settings before saving settings.').css('color', 'red'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + }) + + $('#settings_radarr_ssl').change(function() { + $('#radarr_validated').checkbox('uncheck'); + $('#radarr_validation_result').text('You must test your Sonarr connection settings before saving settings.').css('color', 'red'); + $('.form').form('validate form'); + $('#loader').removeClass('active'); + }) + + $("#radarr_validated").checkbox('check');