2014-04-02 02:29:46 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# 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.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# 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 02:29:46 -04:00
|
|
|
#
|
2014-03-10 01:18:05 -04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-02 02:29:46 -04:00
|
|
|
from __future__ import with_statement
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import os.path
|
2014-04-02 02:29:46 -04:00
|
|
|
import sys
|
2014-12-13 08:27:31 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
except ImportError:
|
|
|
|
print ("You need to install python requests library")
|
|
|
|
sys.exit(1)
|
2014-04-02 02:29:46 -04: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-21 23:58:03 -04:00
|
|
|
def processEpisode(dir_to_process, org_NZB_name=None, status=None):
|
2014-04-02 02:29:46 -04:00
|
|
|
# Default values
|
|
|
|
host = "localhost"
|
|
|
|
port = "8081"
|
|
|
|
username = ""
|
|
|
|
password = ""
|
|
|
|
ssl = 0
|
|
|
|
web_root = "/"
|
|
|
|
|
|
|
|
default_url = host + ":" + port + web_root
|
|
|
|
if ssl:
|
|
|
|
default_url = "https://" + default_url
|
|
|
|
else:
|
|
|
|
default_url = "http://" + default_url
|
|
|
|
|
|
|
|
# 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: " + config_filename + " doesn\'t exist")
|
|
|
|
print ("copy /rename " + config_filename + ".sample and edit\n")
|
|
|
|
print ("Trying default url: " + default_url + "\n")
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
print ("Loading config from " + config_filename + "\n")
|
|
|
|
|
|
|
|
with open(config_filename, "r") as fp:
|
|
|
|
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")
|
|
|
|
|
|
|
|
try:
|
|
|
|
ssl = int(config.get("SickBeard", "ssl"))
|
|
|
|
|
|
|
|
except (configparser.NoOptionError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
web_root = config.get("SickBeard", "web_root")
|
|
|
|
if not web_root.startswith("/"):
|
|
|
|
web_root = "/" + web_root
|
|
|
|
|
|
|
|
if not web_root.endswith("/"):
|
|
|
|
web_root = web_root + "/"
|
|
|
|
|
|
|
|
except configparser.NoOptionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
params = {}
|
2014-04-02 02:29:46 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
params['quiet'] = 1
|
|
|
|
|
2014-04-02 02:29:46 -04:00
|
|
|
params['dir'] = dir_to_process
|
|
|
|
if org_NZB_name != None:
|
|
|
|
params['nzbName'] = org_NZB_name
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-21 23:58:03 -04:00
|
|
|
if status != None:
|
|
|
|
params['failed'] = status
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if ssl:
|
|
|
|
protocol = "https://"
|
|
|
|
else:
|
|
|
|
protocol = "http://"
|
|
|
|
|
2014-12-13 00:02:57 -05:00
|
|
|
url = protocol + host + ":" + port + web_root + "home/postprocess/processEpisode"
|
|
|
|
login_url = protocol + host + ":" + port + web_root + "login"
|
2014-04-02 02:29:46 -04:00
|
|
|
|
|
|
|
print ("Opening URL: " + url)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
2014-12-13 00:02:57 -05:00
|
|
|
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)
|
2014-04-02 02:29:46 -04:00
|
|
|
|
2014-12-13 00:02:57 -05:00
|
|
|
for line in result.iter_lines():
|
2014-04-02 02:29:46 -04:00
|
|
|
if line:
|
|
|
|
print (line.strip())
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
e = sys.exc_info()[1]
|
|
|
|
print ("Unable to open URL: " + str(e))
|
2014-03-10 01:18:05 -04:00
|
|
|
sys.exit(1)
|
2014-04-02 02:29:46 -04: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.")
|
|
|
|
sys.exit(1)
|