2023-01-12 01:04:47 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of SickGear.
|
|
|
|
#
|
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings.filterwarnings('ignore', module=r'.*connectionpool.*', message='.*certificate verification.*')
|
|
|
|
warnings.filterwarnings('ignore', module=r'.*ssl_.*', message='.*SSLContext object.*')
|
|
|
|
|
2023-01-13 17:37:22 +00:00
|
|
|
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.'
|
2023-01-12 01:04:47 +00:00
|
|
|
try:
|
|
|
|
import requests
|
2023-01-13 17:37:22 +00:00
|
|
|
if PY2:
|
|
|
|
print(py_msg % 'should')
|
2023-01-12 01:04:47 +00:00
|
|
|
except ImportError:
|
2023-01-13 17:37:22 +00:00
|
|
|
if PY2:
|
|
|
|
print(py_msg % 'must')
|
|
|
|
sys.exit(1)
|
|
|
|
print('You must install python requests library')
|
2023-01-12 01:04:47 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try: # Try importing Python 3 modules
|
|
|
|
import configparser
|
|
|
|
# noinspection PyUnresolvedReferences
|
|
|
|
import urllib.request as urllib2
|
|
|
|
# noinspection PyUnresolvedReferences,PyCompatibility
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
except ImportError: # On error, import Python 2 modules
|
|
|
|
# noinspection PyPep8Naming,PyUnresolvedReferences
|
|
|
|
import ConfigParser as configparser
|
|
|
|
# noinspection PyUnresolvedReferences
|
|
|
|
import urllib2
|
|
|
|
# noinspection PyUnresolvedReferences
|
|
|
|
from urllib import urlencode
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection DuplicatedCode
|
|
|
|
def process_files(dir_to_process, org_nzb_name=None, status=None):
|
|
|
|
# Default values
|
|
|
|
host = 'localhost'
|
|
|
|
port = '8081'
|
2023-01-13 17:37:22 +00:00
|
|
|
username = password = ''
|
|
|
|
use_ssl = '' # or 's'
|
|
|
|
web_root = '' # e.g. "/path"
|
|
|
|
url = 'http%s://%s:%s%s' % (use_ssl, host, port, web_root)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Get values from config_file
|
|
|
|
config = configparser.RawConfigParser()
|
|
|
|
config_filename = os.path.join(os.path.dirname(sys.argv[0]), 'autoProcessTV.cfg')
|
|
|
|
|
|
|
|
if not os.path.isfile(config_filename):
|
|
|
|
print('ERROR: %s doesn\'t exist' % config_filename)
|
2023-01-13 17:37:22 +00:00
|
|
|
print('copy/rename %s.sample and edit\n' % config_filename)
|
|
|
|
print('Trying default url: %s\n' % url)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
print('Loading config from %s\n' % config_filename)
|
|
|
|
|
|
|
|
with open(config_filename, 'r') as fp:
|
|
|
|
""" Under py3, `config.readfp` is flagged deprecated with advice to use read_file instead.
|
|
|
|
However, py2 doesn't have `read_file`, so a little defensive coding is added here
|
|
|
|
"""
|
|
|
|
if callable(getattr(config, 'read_file', None)):
|
|
|
|
config.read_file(fp)
|
|
|
|
else:
|
|
|
|
# noinspection PyDeprecation
|
|
|
|
config.readfp(fp)
|
|
|
|
|
2023-01-13 17:37:22 +00:00
|
|
|
def cfg_get(cfg, option):
|
|
|
|
try:
|
|
|
|
return cfg.get('SickGear', option)
|
|
|
|
except configparser.NoOptionError:
|
|
|
|
return cfg.get('SickBeard', option)
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
# Replace default values with config_file values
|
2023-01-13 17:37:22 +00:00
|
|
|
host, port, username, password = [cfg_get(config, _option)
|
|
|
|
for _option in ('host', 'port', 'username', 'password')]
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
try:
|
2023-01-13 17:37:22 +00:00
|
|
|
use_ssl = int(cfg_get(config, 'ssl')) and 's' or ''
|
|
|
|
except (configparser.NoOptionError, ValueError, TypeError):
|
2023-01-12 01:04:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2023-01-13 17:37:22 +00:00
|
|
|
web_root = cfg_get(config, 'web_root')
|
|
|
|
web_root = web_root.strip('/').strip()
|
|
|
|
web_root = any(web_root) and ('/%s' % web_root) or ''
|
2023-01-12 01:04:47 +00:00
|
|
|
except configparser.NoOptionError:
|
|
|
|
pass
|
|
|
|
|
2023-01-13 17:37:22 +00:00
|
|
|
url = 'http%s://%s:%s%s' % (use_ssl, host, port, web_root)
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
except EnvironmentError:
|
|
|
|
e = sys.exc_info()[1]
|
|
|
|
print('Could not read configuration file: ' + str(e))
|
|
|
|
# There was a config_file, don't use default values but exit
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
params = {'dir_name': dir_to_process, 'quiet': 1, 'is_basedir': 0}
|
|
|
|
|
|
|
|
if None is not org_nzb_name:
|
|
|
|
params['nzb_name'] = org_nzb_name
|
|
|
|
|
|
|
|
if None is not status:
|
|
|
|
params['failed'] = status
|
|
|
|
|
2023-01-13 17:37:22 +00:00
|
|
|
login_url = '%s/login' % url
|
|
|
|
url = '%s/home/process-media/files' % url
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
print('Opening URL: ' + url)
|
|
|
|
|
|
|
|
try:
|
|
|
|
sess = requests.Session()
|
|
|
|
if username or password:
|
|
|
|
r = sess.get(login_url, verify=False)
|
|
|
|
login_params = {'username': username, 'password': password}
|
|
|
|
if 401 == r.status_code and r.cookies.get('_xsrf'):
|
|
|
|
login_params['_xsrf'] = r.cookies.get('_xsrf')
|
|
|
|
sess.post(login_url, data=login_params, stream=True, verify=False)
|
|
|
|
result = sess.get(url, params=params, stream=True, verify=False)
|
|
|
|
if 401 == result.status_code:
|
|
|
|
print('Verify and use correct username and password in autoProcessTV.cfg')
|
|
|
|
else:
|
|
|
|
for line in result.iter_lines(decode_unicode=True):
|
|
|
|
if line:
|
|
|
|
print(line.strip())
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
e = sys.exc_info()[1]
|
|
|
|
print('Unable to open URL: ' + str(e))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if '__main__' == __name__:
|
|
|
|
print('This module is supposed to be used as import in other scripts and not run standalone.')
|
2023-01-13 17:37:22 +00:00
|
|
|
print('Use sabToSickGear instead.')
|
2023-01-12 01:04:47 +00:00
|
|
|
sys.exit(1)
|