SickGear/autoProcessTV/autoProcessTV.py

151 lines
4.5 KiB
Python
Raw Normal View History

2014-04-02 06:29:46 +00:00
#!/usr/bin/env python
# Author: Nic Wolfe <nic@wolfeden.ca>
# URL: http://code.google.com/p/sickbeard/
#
# 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.
2014-04-02 06:29:46 +00:00
#
# 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
2014-04-02 06:29:46 +00:00
from __future__ import with_statement
import os.path
2014-04-02 06:29:46 +00:00
import sys
sickbeardPath = os.path.split(os.path.split(sys.argv[0])[0])[0]
sys.path.insert(1, os.path.join(sickbeardPath, 'lib'))
sys.path.insert(1, sickbeardPath)
try:
import requests
except ImportError:
print ('You need to install python requests library')
sys.exit(1)
2014-04-02 06:29:46 +00:00
# Try importing Python 2 modules using new names
try:
import ConfigParser as configparser
import urllib2
from urllib import urlencode
# On error import Python 3 modules
except ImportError:
import configparser
import urllib.request as urllib2
from urllib.parse import urlencode
2014-04-22 03:58:03 +00:00
def processEpisode(dir_to_process, org_NZB_name=None, status=None):
2014-04-02 06:29:46 +00:00
# Default values
host = 'localhost'
port = '8081'
username = ''
password = ''
2014-04-02 06:29:46 +00:00
ssl = 0
web_root = '/'
2014-04-02 06:29:46 +00:00
default_url = host + ':' + port + web_root
2014-04-02 06:29:46 +00:00
if ssl:
default_url = 'https://' + default_url
2014-04-02 06:29:46 +00:00
else:
default_url = 'http://' + default_url
2014-04-02 06:29:46 +00:00
# Get values from config_file
config = configparser.RawConfigParser()
config_filename = os.path.join(os.path.dirname(sys.argv[0]), 'autoProcessTV.cfg')
2014-04-02 06:29:46 +00:00
if not os.path.isfile(config_filename):
print ('ERROR: ' + config_filename + " doesn't exist")
print ('copy /rename ' + config_filename + '.sample and edit\n')
print ('Trying default url: ' + default_url + '\n')
2014-04-02 06:29:46 +00:00
else:
try:
print ('Loading config from ' + config_filename + '\n')
2014-04-02 06:29:46 +00:00
with open(config_filename, 'r') as fp:
2014-04-02 06:29:46 +00:00
config.readfp(fp)
# 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')
2014-04-02 06:29:46 +00:00
try:
ssl = int(config.get('SickBeard', 'ssl'))
2014-04-02 06:29:46 +00:00
except (configparser.NoOptionError, ValueError):
pass
try:
web_root = config.get('SickBeard', 'web_root')
if not web_root.startswith('/'):
web_root = '/' + web_root
2014-04-02 06:29:46 +00:00
if not web_root.endswith('/'):
web_root = web_root + '/'
2014-04-02 06:29:46 +00:00
except configparser.NoOptionError:
pass
except EnvironmentError:
e = sys.exc_info()[1]
print ('Could not read configuration file: ' + str(e))
2014-04-02 06:29:46 +00:00
# There was a config_file, don't use default values but exit
sys.exit(1)
params = {}
2014-04-02 06:29:46 +00:00
params['quiet'] = 1
2014-04-02 06:29:46 +00:00
params['dir'] = dir_to_process
if org_NZB_name != None:
params['nzbName'] = org_NZB_name
2014-04-22 03:58:03 +00:00
if status != None:
params['failed'] = status
if ssl:
protocol = 'https://'
else:
protocol = 'http://'
url = protocol + host + ':' + port + web_root + 'home/postprocess/processEpisode'
login_url = protocol + host + ':' + port + web_root + 'login'
2014-04-02 06:29:46 +00:00
print ('Opening URL: ' + url)
2014-04-02 06:29:46 +00:00
try:
sess = requests.Session()
sess.post(login_url, data={'username': username, 'password': password}, stream=True, verify=False)
result = sess.get(url, params=params, stream=True, verify=False)
if result.status_code == 401:
print('Verify and use correct username and password in autoProcessTV.cfg')
else:
for line in result.iter_lines():
if line:
print (line.strip())
2014-04-02 06:29:46 +00:00
except IOError:
e = sys.exc_info()[1]
print ('Unable to open URL: ' + str(e))
sys.exit(1)
2014-04-02 06:29:46 +00:00
if __name__ == '__main__':
print ('This module is supposed to be used as import in other scripts and not run standalone.')
print ('Use sabToSickBeard instead.')
2014-04-02 06:29:46 +00:00
sys.exit(1)