|
1 |
| -# Author: Nic Wolfe <nic@wolfeden.ca> |
2 |
| -# URL: http://code.google.com/p/sickbeard/ |
3 |
| -# |
4 |
| -# This file is part of Sick Beard. |
5 |
| -# |
6 |
| -# Sick Beard is free software: you can redistribute it and/or modify |
7 |
| -# it under the terms of the GNU General Public License as published by |
8 |
| -# the Free Software Foundation, either version 3 of the License, or |
9 |
| -# (at your option) any later version. |
10 |
| -# |
11 |
| -# Sick Beard is distributed in the hope that it will be useful, |
12 |
| -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
| -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
| -# GNU General Public License for more details. |
15 |
| -# |
16 |
| -# You should have received a copy of the GNU General Public License |
17 |
| -# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>. |
18 |
| - |
19 |
| - |
20 |
| -import sys |
21 |
| -import urllib |
22 |
| -import os.path |
23 |
| -import ConfigParser |
24 |
| - |
25 |
| -class AuthURLOpener(urllib.FancyURLopener): |
26 |
| - def __init__(self, user, pw): |
27 |
| - self.username = user |
28 |
| - self.password = pw |
29 |
| - self.numTries = 0 |
30 |
| - urllib.FancyURLopener.__init__(self) |
31 |
| - |
32 |
| - def prompt_user_passwd(self, host, realm): |
33 |
| - if self.numTries == 0: |
34 |
| - self.numTries = 1 |
35 |
| - return (self.username, self.password) |
36 |
| - else: |
37 |
| - return ('', '') |
38 |
| - |
39 |
| - def openit(self, url): |
40 |
| - self.numTries = 0 |
41 |
| - return urllib.FancyURLopener.open(self, url) |
42 |
| - |
43 |
| - |
44 |
| -def processEpisode(dirName, nzbName=None): |
45 |
| - |
46 |
| - config = ConfigParser.ConfigParser() |
47 |
| - configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessTV.cfg") |
48 |
| - print "Loading config from", configFilename |
49 |
| - |
50 |
| - if not os.path.isfile(configFilename): |
51 |
| - print "ERROR: You need an autoProcessTV.cfg file - did you rename and edit the .sample?" |
52 |
| - sys.exit(-1) |
53 |
| - |
54 |
| - try: |
55 |
| - fp = open(configFilename, "r") |
56 |
| - config.readfp(fp) |
57 |
| - fp.close() |
58 |
| - except IOError, e: |
59 |
| - print "Could not read configuration file: ", str(e) |
60 |
| - sys.exit(1) |
61 |
| - |
62 |
| - host = config.get("SickBeard", "host") |
63 |
| - port = config.get("SickBeard", "port") |
64 |
| - username = config.get("SickBeard", "username") |
65 |
| - password = config.get("SickBeard", "password") |
66 |
| - try: |
67 |
| - ssl = int(config.get("SickBeard", "ssl")) |
68 |
| - except (ConfigParser.NoOptionError, ValueError): |
69 |
| - ssl = 0 |
70 |
| - |
71 |
| - try: |
72 |
| - web_root = config.get("SickBeard", "web_root") |
73 |
| - except ConfigParser.NoOptionError: |
74 |
| - web_root = "" |
75 |
| - |
76 |
| - params = {} |
77 |
| - |
78 |
| - params['quiet'] = 1 |
79 |
| - |
80 |
| - params['dir'] = dirName |
81 |
| - if nzbName != None: |
82 |
| - params['nzbName'] = nzbName |
83 |
| - |
84 |
| - myOpener = AuthURLOpener(username, password) |
85 |
| - |
86 |
| - if ssl: |
87 |
| - protocol = "https://" |
88 |
| - else: |
89 |
| - protocol = "http://" |
90 |
| - |
91 |
| - url = protocol + host + ":" + port + web_root + "/home/postprocess/processEpisode?" + urllib.urlencode(params) |
92 |
| - |
93 |
| - print "Opening URL:", url |
94 |
| - |
95 |
| - try: |
96 |
| - urlObj = myOpener.openit(url) |
97 |
| - except IOError, e: |
98 |
| - print "Unable to open URL: ", str(e) |
99 |
| - sys.exit(1) |
100 |
| - |
101 |
| - result = urlObj.readlines() |
102 |
| - for line in result: |
103 |
| - print line |
104 |
| - |
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Author: Nic Wolfe <nic@wolfeden.ca> |
| 4 | +# URL: http://code.google.com/p/sickbeard/ |
| 5 | +# |
| 6 | +# This file is part of Sick Beard. |
| 7 | +# |
| 8 | +# Sick Beard is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# Sick Beard is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +from __future__ import with_statement |
| 22 | + |
| 23 | +import os.path |
| 24 | +import sys |
| 25 | + |
| 26 | +# Try importing Python 2 modules using new names |
| 27 | +try: |
| 28 | + import ConfigParser as configparser |
| 29 | + import urllib2 |
| 30 | + from urllib import urlencode |
| 31 | + |
| 32 | +# On error import Python 3 modules |
| 33 | +except ImportError: |
| 34 | + import configparser |
| 35 | + import urllib.request as urllib2 |
| 36 | + from urllib.parse import urlencode |
| 37 | + |
| 38 | +# workaround for broken urllib2 in python 2.6.5: wrong credentials lead to an infinite recursion |
| 39 | +if sys.version_info >= (2, 6, 5) and sys.version_info < (2, 6, 6): |
| 40 | + class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler): |
| 41 | + def retry_http_basic_auth(self, host, req, realm): |
| 42 | + # don't retry if auth failed |
| 43 | + if req.get_header(self.auth_header, None) is not None: |
| 44 | + return None |
| 45 | + |
| 46 | + return urllib2.HTTPBasicAuthHandler.retry_http_basic_auth(self, host, req, realm) |
| 47 | + |
| 48 | +else: |
| 49 | + HTTPBasicAuthHandler = urllib2.HTTPBasicAuthHandler |
| 50 | + |
| 51 | + |
| 52 | +def processEpisode(dir_to_process, org_NZB_name=None): |
| 53 | + |
| 54 | + # Default values |
| 55 | + host = "localhost" |
| 56 | + port = "8081" |
| 57 | + username = "" |
| 58 | + password = "" |
| 59 | + ssl = 0 |
| 60 | + web_root = "/" |
| 61 | + |
| 62 | + default_url = host + ":" + port + web_root |
| 63 | + if ssl: |
| 64 | + default_url = "https://" + default_url |
| 65 | + else: |
| 66 | + default_url = "http://" + default_url |
| 67 | + |
| 68 | + # Get values from config_file |
| 69 | + config = configparser.RawConfigParser() |
| 70 | + config_filename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessTV.cfg") |
| 71 | + |
| 72 | + if not os.path.isfile(config_filename): |
| 73 | + print ("ERROR: " + config_filename + " doesn\'t exist") |
| 74 | + print ("copy /rename " + config_filename + ".sample and edit\n") |
| 75 | + print ("Trying default url: " + default_url + "\n") |
| 76 | + |
| 77 | + else: |
| 78 | + try: |
| 79 | + print ("Loading config from " + config_filename + "\n") |
| 80 | + |
| 81 | + with open(config_filename, "r") as fp: |
| 82 | + config.readfp(fp) |
| 83 | + |
| 84 | + # Replace default values with config_file values |
| 85 | + host = config.get("SickBeard", "host") |
| 86 | + port = config.get("SickBeard", "port") |
| 87 | + username = config.get("SickBeard", "username") |
| 88 | + password = config.get("SickBeard", "password") |
| 89 | + |
| 90 | + try: |
| 91 | + ssl = int(config.get("SickBeard", "ssl")) |
| 92 | + |
| 93 | + except (configparser.NoOptionError, ValueError): |
| 94 | + pass |
| 95 | + |
| 96 | + try: |
| 97 | + web_root = config.get("SickBeard", "web_root") |
| 98 | + if not web_root.startswith("/"): |
| 99 | + web_root = "/" + web_root |
| 100 | + |
| 101 | + if not web_root.endswith("/"): |
| 102 | + web_root = web_root + "/" |
| 103 | + |
| 104 | + except configparser.NoOptionError: |
| 105 | + pass |
| 106 | + |
| 107 | + except EnvironmentError: |
| 108 | + e = sys.exc_info()[1] |
| 109 | + print ("Could not read configuration file: " + str(e)) |
| 110 | + # There was a config_file, don't use default values but exit |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + params = {} |
| 114 | + |
| 115 | + params['quiet'] = 1 |
| 116 | + |
| 117 | + params['dir'] = dir_to_process |
| 118 | + if org_NZB_name != None: |
| 119 | + params['nzbName'] = org_NZB_name |
| 120 | + |
| 121 | + if ssl: |
| 122 | + protocol = "https://" |
| 123 | + else: |
| 124 | + protocol = "http://" |
| 125 | + |
| 126 | + url = protocol + host + ":" + port + web_root + "home/postprocess/processEpisode?" + urlencode(params) |
| 127 | + |
| 128 | + print ("Opening URL: " + url) |
| 129 | + |
| 130 | + try: |
| 131 | + password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 132 | + password_mgr.add_password(None, url, username, password) |
| 133 | + handler = HTTPBasicAuthHandler(password_mgr) |
| 134 | + opener = urllib2.build_opener(handler) |
| 135 | + urllib2.install_opener(opener) |
| 136 | + |
| 137 | + result = opener.open(url).readlines() |
| 138 | + |
| 139 | + for line in result: |
| 140 | + if line: |
| 141 | + print (line.strip()) |
| 142 | + |
| 143 | + except IOError: |
| 144 | + e = sys.exc_info()[1] |
| 145 | + print ("Unable to open URL: " + str(e)) |
| 146 | + sys.exit(1) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + print ("This module is supposed to be used as import in other scripts and not run standalone.") |
| 151 | + print ("Use sabToSickBeard instead.") |
| 152 | + sys.exit(1) |
0 commit comments