forked from clinton-hall/nzbToMedia
/
autoProcessMovie.py
144 lines (112 loc) · 4.31 KB
/
autoProcessMovie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sys
import urllib
import os.path
import ConfigParser
import time
import json
from pprint import pprint
class AuthURLOpener(urllib.FancyURLopener):
def __init__(self, user, pw):
self.username = user
self.password = pw
self.numTries = 0
urllib.FancyURLopener.__init__(self)
def prompt_user_passwd(self, host, realm):
if self.numTries == 0:
self.numTries = 1
return (self.username, self.password)
else:
return ('', '')
def openit(self, url):
self.numTries = 0
return urllib.FancyURLopener.open(self, url)
def process(dirName, nzbName=None, status=0):
status = int(status)
config = ConfigParser.ConfigParser()
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMovie.cfg")
print "autoProcessMovie v 3.1"
print "Loading config from", configFilename
if not os.path.isfile(configFilename):
print "ERROR: You need an autoProcessMovie.cfg file - did you rename and edit the .sample?"
sys.exit(-1)
config.read(configFilename)
host = config.get("CouchPotato", "host")
port = config.get("CouchPotato", "port")
username = config.get("CouchPotato", "username")
password = config.get("CouchPotato", "password")
apikey = config.get("CouchPotato", "apikey")
delay = float(config.get("CouchPotato", "delay"))
method = config.get("CouchPotato", "method")
try:
ssl = int(config.get("CouchPotato", "ssl"))
except (ConfigParser.NoOptionError, ValueError):
ssl = 0
try:
web_root = config.get("CouchPotato", "web_root")
except ConfigParser.NoOptionError:
web_root = ""
myOpener = AuthURLOpener(username, password)
nzbName1 = str(nzbName)
if ssl:
protocol = "https://"
else:
protocol = "http://"
if status == 0:
if method == "manage":
command = "manage.update"
else:
command = "renamer.scan"
url = protocol + host + ":" + port + web_root + "/api/" + apikey + "/" + command
print "waiting for", str(delay), "seconds to allow CPS to process newly extracted files"
time.sleep(delay)
print "Opening URL:", url
try:
urlObj = myOpener.openit(url)
except IOError, e:
print "Unable to open URL: ", str(e)
sys.exit(1)
result = urlObj.readlines()
for line in result:
print line
print command, "started on CouchPotatoServer for", nzbName1
else:
print "download of", nzbName1, "has failed."
print "trying to re-cue the next highest ranked release"
a=nzbName1.find('.cp')+4
b=nzbName1.find('.nzb')-1
imdbid=nzbName1[a:b]
#print imdbid
url = protocol + host + ":" + port + web_root + "/api/" + apikey + "/movie.list"
print "Opening URL:", url
try:
urlObj = myOpener.openit(url)
except IOError, e:
print "Unable to open URL: ", str(e)
sys.exit(1)
n=0
result = json.load(urlObj)
movieid = [item["id"] for item in result["movies"]]
library = [item["library"] for item in result["movies"]]
identifier = [item["identifier"] for item in library]
for index in range(len(movieid)):
if identifier[index] == imdbid:
movid = str(movieid[index])
print "found movie id", movid, "in database for release", nzbName1
n = n + 1
break
if n == 0:
print "cound not find a mvie in the database for release", nzbName1
print "please manually ignore this release and refresh the wanted movie"
print "exiting postprocessing script"
sys.exit(1)
url = protocol + host + ":" + port + web_root + "/api/" + apikey + "/searcher.try_next/?id=" + movid
print "Opening URL:", url
try:
urlObj = myOpener.openit(url)
except IOError, e:
print "Unable to open URL: ", str(e)
sys.exit(1)
result = urlObj.readlines()
for line in result:
print line
print "movie", movid, "set to try the next best release on CouchPotatoServer"