-
Notifications
You must be signed in to change notification settings - Fork 4
/
nzb.py
200 lines (178 loc) · 5.85 KB
/
nzb.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
Copyright (c) 2010, 2011, 2012 Popeye
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import sys
import locale
import urllib2
import utils
WIN32 = (sys.platform == 'win32')
DARWIN = sys.platform.startswith('darwin')
class Nzbname:
def __init__(self,nzbname):
# Modified from SABnzbd /sabnzbd/nzbstuff.py
self.original = nzbname
#work_name = platform_encode(nzbname)
work_name = nzbname
# If non-future: create safe folder name stripped from ".nzb" and junk
if work_name and (work_name.lower().endswith('.nzb') or \
work_name.lower().endswith('.nzb.gz') or \
work_name.lower().endswith('.nzb.zip')):
dname, ext = os.path.splitext(work_name) # Used for folder name for final unpack
if ext.lower() == '.gz' or ext.lower() == '.zip':
work_name = dname
dname, ext = os.path.splitext(work_name) # Used for folder name for final unpack
if ext.lower() == '.nzb':
work_name = dname
work_name = sanitize_foldername(work_name)
self.final_name = work_name
# From SABnzbd /sabnzbd/encoding.py
def platform_encode(p):
""" Return the correct encoding for the platform:
Latin-1 for Windows/Posix-non-UTF and UTF-8 for OSX/Posix-UTF
"""
try:
if DARWIN:
UTF = True
else:
UTF = locale.getdefaultlocale()[1].lower().find('utf') >= 0
except:
# Incorrect locale implementation, assume the worst
UTF = False
if isinstance(p, unicode):
if UTF:
return p.encode('utf-8')
else:
return p.encode('latin-1', 'replace')
elif isinstance(p, basestring):
if UTF:
try:
p.decode('utf-8')
return p
except:
return p.decode('latin-1').encode('utf-8')
else:
try:
return p.decode('utf-8').encode('latin-1', 'replace')
except:
return p
else:
return p
# Modified from SABnzbd /sabnzbd/misc.py
if WIN32:
# the colon should be here too, but we'll handle that separately
CH_ILLEGAL = r'\/<>?*|"'
CH_LEGAL = r'++{}!@#`'
else:
CH_ILLEGAL = r'/'
CH_LEGAL = r'+'
def sanitize_filename(name):
""" Return filename with illegal chars converted to legal ones
and with the par2 extension always in lowercase
"""
if not name:
return name
illegal = CH_ILLEGAL
legal = CH_LEGAL
if ':' in name:
if WIN32:
# Compensate for the odd way par2 on Windows substitutes a colon character
name = name.replace(':', '3A')
elif DARWIN:
# Compensate for the foolish way par2 on OSX handles a colon character
name = name[name.rfind(':')+1:]
lst = []
for ch in name.strip():
if ch in illegal:
ch = legal[illegal.find(ch)]
lst.append(ch)
name = ''.join(lst)
if not name:
name = 'unknown'
name, ext = os.path.splitext(name)
lowext = ext.lower()
if lowext == '.par2' and lowext != ext:
ext = lowext
return name + ext
FL_ILLEGAL = CH_ILLEGAL + ':\x92"'
FL_LEGAL = CH_LEGAL + "-''"
uFL_ILLEGAL = FL_ILLEGAL.decode('latin-1')
uFL_LEGAL = FL_LEGAL.decode('latin-1')
def sanitize_foldername(name):
""" Return foldername with dodgy chars converted to safe ones
Remove any leading and trailing dot and space characters
"""
if not name:
return name
if isinstance(name, unicode):
illegal = uFL_ILLEGAL
legal = uFL_LEGAL
else:
illegal = FL_ILLEGAL
legal = FL_LEGAL
# repl = cfg.replace_illegal()
repl = True
lst = []
for ch in name.strip():
if ch in illegal:
if repl:
ch = legal[illegal.find(ch)]
lst.append(ch)
else:
lst.append(ch)
name = ''.join(lst)
name = name.strip('. ')
if not name:
name = 'unknown'
if WIN32:
maxlen = 128
else:
maxlen = 256
if len(name) > maxlen:
name = name[:maxlen]
return name
def save(url, nzb_path):
file, e = _load_nzb(url)
if e is None:
try:
utils.write(nzb_path, file, 'wb')
except:
e = "Pneumatic failed writing %s" % nzb_path
else:
e = "Pneumatic saved %s" % nzb_path
print e
return
def _load_nzb(url):
req = urllib2.Request(url)
try:
response = urllib2.urlopen(req)
except urllib2.URLError, ex:
if hasattr(ex, 'reason'):
print ex.reason
e = ex.reason
return None, e
elif hasattr(ex, 'code'):
e = ex.code + " " + url
return None, e
else:
doc = response.read()
response.close()
return doc, None