From b3921381a2d6fc5089bbd2c12068e46716f619af Mon Sep 17 00:00:00 2001 From: AJ Slater Date: Fri, 21 Feb 2020 20:17:57 -0800 Subject: [PATCH] implement torznab test --- data/interfaces/default/config.html | 22 ++++++++++++------ mylar/config.py | 9 ++++---- mylar/helpers.py | 36 +++++++++++++++++++++++++++++ mylar/webserve.py | 26 +++++++++++++++++++-- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 6e467066..791028f6 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -892,9 +892,11 @@ torznab_enabled = "checked" else: torznab_enabled = "" - + if torznab[2] == '1' or torznab[2] == 1: + torznab_verify = "checked" + else: + torznab_verify = "" %> -
@@ -904,13 +906,18 @@
+
+ + +
+
- +
- +
@@ -2179,7 +2186,7 @@ $("#add_torznab").click(function() { var intId = $("#torznab_providers > div").size() + deletedTorznabs + 1; - var torformfields = $("
"); + var torformfields = $("
"); var tortestButton = $("
"); var torremoveButton = $("
"); torremoveButton.click(function() { @@ -2322,10 +2329,11 @@ $(".torznabtest").click(function () { var torznab = this.attributes["name"].value.replace('torznab_test', ''); - var imagechk = document.getElementById("tornabstatus"+torznab); + var imagechk = document.getElementById("torznabstatus"+torznab); var name = document.getElementById("torznab_name"+torznab).value; var host = document.getElementById("torznab_host"+torznab).value; - var apikey = document.getElementById("torznab_api"+torznab).value; + var ssl = document.getElementById("torznab_verify"+torznab).checked; + var apikey = document.getElementById("torznab_apikey"+torznab).value; $.get("testtorznab", { name: name, host: host, ssl: ssl, apikey: apikey }, function(data){ diff --git a/mylar/config.py b/mylar/config.py index 41accffc..b3e31ef5 100644 --- a/mylar/config.py +++ b/mylar/config.py @@ -9,6 +9,7 @@ import shutil import threading import re import ConfigParser +import errno import mylar from mylar import logger, helpers, encrypted @@ -555,7 +556,7 @@ class Config(object): if self.CONFIG_VERSION < 8: print('Checking for existing torznab configuration...') if not any([self.TORZNAB_NAME is None, self.TORZNAB_HOST is None, self.TORZNAB_APIKEY is None, self.TORZNAB_CATEGORY is None]): - torznabs =[(self.TORZNAB_NAME, self.TORZNAB_HOST, self.TORZNAB_APIKEY, self.TORZNAB_CATEGORY, str(int(self.ENABLE_TORZNAB)))] + torznabs =[(self.TORZNAB_NAME, self.TORZNAB_HOST, self.TORZNAB_VERIFY, self.TORZNAB_APIKEY, self.TORZNAB_CATEGORY, str(int(self.ENABLE_TORZNAB)))] setattr(self, 'EXTRA_TORZNABS', torznabs) config.set('Torznab', 'EXTRA_TORZNABS', str(torznabs)) print('Successfully converted existing torznab for multiple configuration allowance. Removing old references.') @@ -563,9 +564,9 @@ class Config(object): print('No existing torznab configuration found. Just removing config references at this point..') config.remove_option('Torznab', 'torznab_name') config.remove_option('Torznab', 'torznab_host') + config.remove_option('Torznab', 'torznab_verify') config.remove_option('Torznab', 'torznab_apikey') config.remove_option('Torznab', 'torznab_category') - config.remove_option('Torznab', 'torznab_verify') print('Successfully removed outdated config entries.') if self.newconfig < 9: #rejig rtorrent settings due to change. @@ -1111,7 +1112,7 @@ class Config(object): return extra_newznabs def get_extra_torznabs(self): - extra_torznabs = zip(*[iter(self.EXTRA_TORZNABS.split(', '))]*5) + extra_torznabs = zip(*[iter(self.EXTRA_TORZNABS.split(', '))]*6) return extra_torznabs def provider_sequence(self): @@ -1154,7 +1155,7 @@ class Config(object): if self.ENABLE_TORZNAB: for ets in self.EXTRA_TORZNABS: - if str(ets[4]) == '1': # if torznabs are enabled + if str(ets[5]) == '1': # if torznabs are enabled if ets[0] == "": et_name = ets[1] else: diff --git a/mylar/helpers.py b/mylar/helpers.py index 101102ea..501e180e 100755 --- a/mylar/helpers.py +++ b/mylar/helpers.py @@ -3657,6 +3657,42 @@ def newznab_test(name, host, ssl, apikey): logger.info('[ERROR:%s] - %s' % (code, description)) return False +def torznab_test(name, host, ssl, apikey): + from xml.dom.minidom import parseString, Element + params = {'t': 'search', + 'apikey': apikey, + 'o': 'xml'} + + if host[-1:] == '/': + host = host[:-1] + headers = {'User-Agent': str(mylar.USER_AGENT)} + logger.info('host: %s' % host) + try: + r = requests.get(host, params=params, headers=headers, verify=bool(ssl)) + except Exception as e: + logger.warn('Unable to connect: %s' % e) + return + else: + try: + data = parseString(r.content) + except Exception as e: + logger.warn('[WARNING] Error attempting to test: %s' % e) + + try: + error_code = data.getElementsByTagName('error')[0].attributes['code'].value + except Exception as e: + logger.info('Connected - Status code returned: %s' % r.status_code) + if r.status_code == 200: + return True + else: + logger.warn('Received response - Status code returned: %s' % r.status_code) + return False + + code = error_code + description = data.getElementsByTagName('error')[0].attributes['description'].value + logger.info('[ERROR:%s] - %s' % (code, description)) + return False + def get_free_space(folder): min_threshold = 100000000 #threshold for minimum amount of freespace available (#100mb) if platform.system() == "Windows": diff --git a/mylar/webserve.py b/mylar/webserve.py index ca749ab4..6a5660e8 100644 --- a/mylar/webserve.py +++ b/mylar/webserve.py @@ -5008,7 +5008,7 @@ class WebInterface(object): "dognzb_verify": helpers.checked(mylar.CONFIG.DOGNZB_VERIFY), "experimental": helpers.checked(mylar.CONFIG.EXPERIMENTAL), "enable_torznab": helpers.checked(mylar.CONFIG.ENABLE_TORZNAB), - "extra_torznabs": sorted(mylar.CONFIG.EXTRA_TORZNABS, key=itemgetter(4), reverse=True), + "extra_torznabs": sorted(mylar.CONFIG.EXTRA_TORZNABS, key=itemgetter(5), reverse=True), "newznab": helpers.checked(mylar.CONFIG.NEWZNAB), "extra_newznabs": sorted(mylar.CONFIG.EXTRA_NEWZNABS, key=itemgetter(5), reverse=True), "enable_ddl": helpers.checked(mylar.CONFIG.ENABLE_DDL), @@ -5426,6 +5426,10 @@ class WebInterface(object): if torznab_name == "": continue torznab_host = helpers.clean_url(kwargs['torznab_host' + torznab_number]) + try: + torznab_verify = kwargs['torznab_verify' + torznab_number] + except: + torznab_verify = 0 torznab_api = kwargs['torznab_apikey' + torznab_number] torznab_category = kwargs['torznab_category' + torznab_number] try: @@ -5435,7 +5439,7 @@ class WebInterface(object): del kwargs[kwarg] - mylar.CONFIG.EXTRA_TORZNABS.append((torznab_name, torznab_host, torznab_api, torznab_category, torznab_enabled)) + mylar.CONFIG.EXTRA_TORZNABS.append((torznab_name, torznab_host, torznab_verify, torznab_api, torznab_category, torznab_enabled)) mylar.CONFIG.process_kwargs(kwargs) @@ -6006,6 +6010,24 @@ class WebInterface(object): return 'Error - failed running test for %s' % name testnewznab.exposed = True + def testtorznab(self, name, host, ssl, apikey): + logger.fdebug('ssl/verify: %s' % ssl) + if 'ssl' == '0' or ssl == '1': + ssl = bool(int(ssl)) + else: + if ssl == 'false': + ssl = False + else: + ssl = True + result = helpers.torznab_test(name, host, ssl, apikey) + if result is True: + logger.info('Successfully tested %s [%s] - valid api response received' % (name, host)) + return 'Successfully tested %s!' % name + else: + print result + logger.warn('Testing failed to %s [HOST:%s][SSL:%s]' % (name, host, bool(ssl))) + return 'Error - failed running test for %s' % name + testtorznab.exposed = True def orderThis(self, **kwargs): return