From cefd596ad291fc85b4843243086467abebbc33aa Mon Sep 17 00:00:00 2001 From: JackDandy Date: Fri, 13 Jan 2023 17:37:22 +0000 Subject: [PATCH] Change prevent included py3 requests module failure on SABnzbd systems that by default, run py2 with py2 requests. --- CHANGES.md | 1 + autoProcessTV/autoProcessTV.py | 61 +++++++++++++++++++++------------- autoProcessTV/sabToSickGear.py | 30 +++++++++++------ 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 50094f51..131a30e8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -27,6 +27,7 @@ * Update tmdbsimple 2.6.6 (679e343) to 2.9.1 (9da400a) * Update torrent_parser 0.3.0 (2a4eecb) to 0.4.0 (23b9e11) * Update unidecode module 1.1.1 (632af82) to 1.3.6 (4141992) +* Change prevent included py3 requests module failure on SABnzbd systems that by default, run py2 with py2 requests [develop changelog] diff --git a/autoProcessTV/autoProcessTV.py b/autoProcessTV/autoProcessTV.py index 15194608..06095b9f 100755 --- a/autoProcessTV/autoProcessTV.py +++ b/autoProcessTV/autoProcessTV.py @@ -23,17 +23,25 @@ import os.path import sys import warnings -sickgearPath = os.path.split(os.path.split(sys.argv[0])[0])[0] -sys.path.insert(1, os.path.join(sickgearPath, 'lib')) -sys.path.insert(1, sickgearPath) - warnings.filterwarnings('ignore', module=r'.*connectionpool.*', message='.*certificate verification.*') warnings.filterwarnings('ignore', module=r'.*ssl_.*', message='.*SSLContext object.*') +PY2 = 2 == sys.version_info[0] +if not PY2: + sg_path = os.path.split(os.path.split(sys.argv[0])[0])[0] + sys.path.insert(1, os.path.join(sg_path, 'lib')) + sys.path.insert(1, sg_path) + +py_msg = 'Python 3 %s be set to run python files instead of the current Python 2.' try: import requests + if PY2: + print(py_msg % 'should') except ImportError: - print('You need to install python requests library') + if PY2: + print(py_msg % 'must') + sys.exit(1) + print('You must install python requests library') sys.exit(1) try: # Try importing Python 3 modules @@ -56,9 +64,10 @@ def process_files(dir_to_process, org_nzb_name=None, status=None): # Default values host = 'localhost' port = '8081' - default_url = 'http://%s:%s/' % (host, port) - ssl = username = password = '' - web_root = '/' + username = password = '' + use_ssl = '' # or 's' + web_root = '' # e.g. "/path" + url = 'http%s://%s:%s%s' % (use_ssl, host, port, web_root) # Get values from config_file config = configparser.RawConfigParser() @@ -66,8 +75,8 @@ def process_files(dir_to_process, org_nzb_name=None, status=None): if not os.path.isfile(config_filename): print('ERROR: %s doesn\'t exist' % config_filename) - print('copy /rename %s.sample and edit\n' % config_filename) - print('Trying default url: %s\n' % default_url) + print('copy/rename %s.sample and edit\n' % config_filename) + print('Trying default url: %s\n' % url) else: try: @@ -83,25 +92,30 @@ def process_files(dir_to_process, org_nzb_name=None, status=None): # noinspection PyDeprecation config.readfp(fp) + def cfg_get(cfg, option): + try: + return cfg.get('SickGear', option) + except configparser.NoOptionError: + return cfg.get('SickBeard', option) + # Replace default values with config_file values - host = config.get('SickBeard', 'host') - port = config.get('SickBeard', 'port') - username = config.get('SickBeard', 'username') - password = config.get('SickBeard', 'password') + host, port, username, password = [cfg_get(config, _option) + for _option in ('host', 'port', 'username', 'password')] try: - ssl = int(config.get('SickBeard', 'ssl')) and 's' or '' - - except (configparser.NoOptionError, ValueError): + use_ssl = int(cfg_get(config, 'ssl')) and 's' or '' + except (configparser.NoOptionError, ValueError, TypeError): pass try: - web_root = config.get('SickBeard', 'web_root') - web_root = ('/%s/' % web_root.strip('/')).replace('//', '/') - + web_root = cfg_get(config, 'web_root') + web_root = web_root.strip('/').strip() + web_root = any(web_root) and ('/%s' % web_root) or '' except configparser.NoOptionError: pass + url = 'http%s://%s:%s%s' % (use_ssl, host, port, web_root) + except EnvironmentError: e = sys.exc_info()[1] print('Could not read configuration file: ' + str(e)) @@ -116,9 +130,8 @@ def process_files(dir_to_process, org_nzb_name=None, status=None): if None is not status: params['failed'] = status - url = 'http%s://%s:%s%s' % (ssl, host, port, web_root) - login_url = url + 'login' - url = url + 'home/process-media/files' + login_url = '%s/login' % url + url = '%s/home/process-media/files' % url print('Opening URL: ' + url) @@ -146,5 +159,5 @@ def process_files(dir_to_process, org_nzb_name=None, status=None): if '__main__' == __name__: print('This module is supposed to be used as import in other scripts and not run standalone.') - print('Use sabToSickBeard instead.') + print('Use sabToSickGear instead.') sys.exit(1) diff --git a/autoProcessTV/sabToSickGear.py b/autoProcessTV/sabToSickGear.py index 4c6e73a2..fcea9b7b 100755 --- a/autoProcessTV/sabToSickGear.py +++ b/autoProcessTV/sabToSickGear.py @@ -12,21 +12,31 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with SickGear. If not, see . from __future__ import print_function import sys -import autoProcessTV -if len(sys.argv) < 2: +try: + import autoProcessTV +except ImportError: + # 0 sys.argv[0] is the name of this script + print('Can\'t import autoProcessTV.py, make sure it\'s in the same folder as %s' % sys.argv[0]) + sys.exit(1) + +if 2 > len(sys.argv): print('No folder supplied - is this being called from SABnzbd?') - sys.exit() -elif len(sys.argv) >= 8: - autoProcessTV.process_files(sys.argv[1], sys.argv[2], sys.argv[7]) -elif len(sys.argv) >= 3: - autoProcessTV.process_files(sys.argv[1], sys.argv[2]) -else: - autoProcessTV.process_files(sys.argv[1]) + sys.exit(1) + +# SABnzbd user script parameters - see: https://sabnzbd.org/wiki/scripts/post-processing-scripts +autoProcessTV.process_files( + # 1 The final directory of the job (full path) + sys.argv[1], + # 2 The original name of the NZB file + None if 3 >= len(sys.argv) else sys.argv[2], + # 7 Status of processing. 0 = OK, 1 = Failed verification, 2 = Failed unpack, 3 = 1+2, -1 = Failed post processing + None if 8 >= len(sys.argv) else sys.argv[7] +)