-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrent-rename
executable file
·444 lines (385 loc) · 15.9 KB
/
torrent-rename
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/python2
# coding: UTF-8
# vi: set noet sw=4 ts=4 nolist :
import sys
import os
import shutil
import re
import json
import urllib2
import string
import logging
import logging.handlers
import atexit
import subprocess
try:
_, torrentid, torrentname, torrentpath = sys.argv
print torrentid, torrentname, torrentpath
except ValueError:
pass
class BufferingSMTPHandler(logging.handlers.BufferingHandler):
def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
logging.handlers.BufferingHandler.__init__(self, capacity)
self.mailhost = mailhost
self.mailport = None
self.fromaddr = fromaddr
self.toaddrs = toaddrs
self.subject = subject
self.setFormatter(logging.Formatter("%(asctime)s %(levelname)-5s %(message)s"))
def flush(self):
if len(self.buffer) > 0:
try:
import smtplib
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (self.fromaddr, string.join(self.toaddrs, ","), self.subject)
for record in self.buffer:
s = self.format(record)
msg = msg + s + "\r\n"
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
except Exception, exc:
raise
print exc
self.handleError(None) # no particular record
self.buffer = []
logger = logging.getLogger("torrent-rename")
logger.setLevel(logging.DEBUG)
# This handler emails me anything that is an error or worse.
mailhandler = BufferingSMTPHandler('localhost', 'cmiller@zippy', ['root@zippy'], 'torrent-rename abnormal events', 500)
mailhandler.setLevel(logging.INFO)
logformatter = logging.Formatter("line%(lineno)d %(message)s")
mailhandler.setFormatter(logformatter)
logger.addHandler(mailhandler)
stdouthandler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(stdouthandler)
atexit.register(logging.shutdown)
quash = ("hdtv", "x264", "avc", "dvdrip", "brrip", "mpeg-4", "[vtv]", "xvid-", "480p", "720i", "720p", "1080i", "1080p", "ws", "pdtv", "4od", "dvdscr", "albiondean", "merry xmas", "bdrip", "repack", "bluray", "proper", "yify", "dvd", "complete", "dvdrip", "linkzkraze", "tvblinkz", "tvboom", "hdtv-", "webrip", "boxxx", "remastered", "www", "rarbg", "etrg", "uncut", "hdrip", "xvid", "internal", "preair")
year = re.compile(r"\b((?:19|20)\d\d)\b")
ep = re.compile(r"\b(?:S(\d\d?)E(\d\d?)|(\d\d?)x(\d\d?))\b", re.I)
kept_extensions = set((".avi", ".mov", ".mkv", ".ogv", ".m4v", ".mp4", ".srt", ".smi", ".3gp", ".ts"))
class ConsistencyError(ValueError): pass
def smart_title(s):
"""
>>> smart_title("the of by")
'The of by'
>>> smart_title("HoUnd spaceSpace ")
'Hound Spacespace'
>>> smart_title("")
''
>>> smart_title("01EST")
'01est'
>>> smart_title("TeSt (s01e99), other")
'Test (s01e99), Other'
>>> smart_title("Elmentary (2019) (s01e99)")
'Elementary (2019) (s01e99)'
>>> smart_title("(s06e11) (s06e12)")
'(s06e11) (s06e12)'
"""
pieces = re.split(r"\s+", s)
result = list()
for i, p in enumerate(pieces):
if not p: continue
lc = p.lower()
if i != 0:
if result[-1][-1] in string.lowercase:
if (lc in ("the", "of", "a", "from", "and", "but", "for", "nor", "or", "to", "by", "at")):
result.append(lc)
continue
result.append(lc[0].upper() + lc[1:])
return " ".join(result).replace("Elmentary ", "Elementary ")
def reformat(filename, path_prefix):
"""
Year always first because it's part of the name, then episode or season info.
>>> reformat('Welcome.to.Sweden.2014.S01E09.HDTV.XviD-AFG.avi', None)
('Welcome to Sweden (2014)/Welcome to Sweden (2014) (s01e09).avi', '.avi')
>>> reformat('Doctor Who (2005) (S40E05).mkv', None)
('Doctor Who (2005)/Doctor Who (2005) (s40e05).mkv', '.mkv')
>>> reformat('Rubicon (S01E05), Connect The Dots.avi', None)
('Rubicon/Rubicon (s01e05) Connect the Dots.avi', '.avi')
>>> reformat('Top Gear (S05E05) (2004-11-21).avi', None)
('Top Gear/Top Gear (2004-11-21) (s05e05).avi', '.avi')
>>> reformat('Food Inc (2008).avi', None)
('Food Inc (2008)/Food Inc (2008).avi', '.avi')
>>> reformat('Top Gear (S05E10) 2005-01-02, The Best Of Season 5.avi', None)
('Top Gear/Top Gear (2005-01-02) (s05e10) The Best of Season 5.avi', '.avi')
>>> reformat('Rubicon.S01E01.Pilot.HDTV.XviD-FQM.avi', None)
('Rubicon/Rubicon (s01e01) Pilot.avi', '.avi')
>>> reformat('Rubicon.S01E11.A.Good.Days.Work.HDTV.XviD-FQM.[VTV].avi', None)
('Rubicon/Rubicon (s01e11) A Good Days Work.avi', '.avi')
>>> reformat("Rubicon (s01e11), A Good Day's Work.avi", None)
("Rubicon/Rubicon (s01e11) A Good Day's Work.avi", '.avi')
>>> reformat("Doctor Who (2005) A Christmas Carol 2010 Special.mkv", None)
('Doctor Who (2005)/Doctor Who (2005) A Christmas Carol 2010 Special.mkv', '.mkv')
>>> reformat("The.Direct.Article.1.avi", None)
('The Direct Article 1/The Direct Article 1.avi', '.avi')
>>> reformat("Dr. Horrible.Act.1.avi", None)
('Dr Horrible Act 1/Dr Horrible Act 1.avi', '.avi')
>>> reformat("Justified Season 1 Episode 13 Bulletville.avi", None)
('Justified/Justified (s01e13) Bulletville.avi', '.avi')
>>> reformat("Star Trek The Next Generation Season 3 Episode 21- Hollow Pursuits.avi", None)
('Star Trek the Next Generation/Star Trek the Next Generation (s03e21) Hollow Pursuits.avi', '.avi')
>>> reformat("The.Colbert.Report.2012.04.11.Michelle.Obama.HDTV.XviD-FQM.[VTV].avi", None)
('The Colbert Report/The Colbert Report (2012-04-11) Michelle Obama.avi', '.avi')
>>> reformat("Community Season 2, Episode 06 – Epidemiology - LinkzKraze.mp4", None)
('Community/Community (s02e06) Epidemiology.mp4', '.mp4')
>>> reformat("glee.404.hdtv-lol.mp4", None)
('Glee/Glee (s04e04).mp4', '.mp4')
>>> reformat("The Big Bang Theory 605 Hdtv-lol.mp4", None)
('The Big Bang Theory/The Big Bang Theory (s06e05).mp4', '.mp4')
>>> reformat("Archer - 2E11 - Jeu Monegasque.mkv", None)
('Archer/Archer (s02e11) Jeu Monegasque.mkv', '.mkv')
>>> reformat("Burn.Notice.S06E11E12.HDTV.x264-2HD.[VTV].mp4", None)
('Burn Notice/Burn Notice (s06e11) (s06e12).mp4', '.mp4')
>>> reformat("Star Trek - The Next Generation- S01 E01+2-Encounter at Fairpoint.mp4", None)
('Star Trek the Next Generation/Star Trek the Next Generation (s01e01) (s01e02) Encounter at Fairpoint.mp4', '.mp4')
>>> reformat('The.Powerpuff.Girls.S06.E07.Crazy.Mixed.Up.Puffs-Mizzen.In.Action-[boxXx].mkv', None)
('The Powerpuff Girls/The Powerpuff Girls (s06e07) Crazy Mixed Up Puffs-mizzen In Action.mkv', '.mkv')
>>> reformat('Rockford_Files-S03xE15-The_Trees_The_Bees_And_TT_Flowers_Part_2.avi', None)
('Rockford Files/Rockford Files (s03e15) The Trees the Bees and Tt Flowers Part 2.avi', '.avi')
>>> reformat('My Neighbor Totoro.mp4', None)
('My Neighbor Totoro/My Neighbor Totoro.mp4', '.mp4')
>>> reformat('My Neighbor Totoro.srt', None)
('My Neighbor Totoro/My Neighbor Totoro.srt', '.srt')
>>> reformat('Arrested Development - S1 E 02 - Top Banana.avi', None)
('Arrested Development/Arrested Development (s01e02) Top Banana.avi', '.avi')
>>> reformat('My Neighbor Totoro.srt', None)
('My Neighbor Totoro/My Neighbor Totoro.srt', '.srt')
>>> reformat('Magnum P I 3x01,3x02 Did You See the Sunrise (1)&(2).avi', None)
('Magnum P I/Magnum P I (s03e01) (s03e02) Did You See the Sunrise (1)&(2).avi', '.avi')
>>> reformat('penn.and.teller.fool.us.s02e01.phone.y.business.hdtv.x264.mp4', None)
('Penn and Teller Fool Us/Penn and Teller Fool Us (s02e01) Phone Y Business.mp4', '.mp4')
>>> reformat('Sicario.2015.1080p.Bluray.DTS.x264-ETRG.mkv', None)
('Sicario (2015)/Sicario (2015).mkv', '.mkv')
>>> reformat('Italian.srt', 'The Jungle Book 2016 1080p BluRay x264 DTS-JYK/Subs')
('The Jungle Book (2016)/Italian.srt', '.srt')
"""
already_dated = False
assert os.sep not in filename
fragments = list()
times = list()
basename, extension = os.path.splitext(filename)
is_language_file = (extension == ".srt" and path_prefix)
if is_language_file:
orig_filename, basename = filename, path_prefix
want_container = True
container_pieces = list()
container_finished = False
b = basename
# S01 E01+2
basename = re.sub(r"(?i)(?<=\w)-\s+", r" ", basename)
basename = re.sub(r"(?i)\b(\S\d+)\s+(E\d+)\s*\+?\s*(\d+)\b", r"\1\2E\3 ", basename)
basename = re.sub(r"(?i)\b(\S\d+)\s+(E\d+)-?\b", r"(\1\2)", basename)
basename = re.sub(r"(?i)\bseason\s?(\d+),?\s?episode\s?(\d+)-*", r"(s\1e\2)", basename)
basename = re.sub(r"(?i)\bseason\s?(\d+),?\s?show\s?(\d+)", r"(s\1e\2)", basename)
basename = re.sub(r"\b(2\d\d\d)\.(\d\d?)\.(\d\d?)\b", r"\1-\2-\3", basename)
basename = re.sub(r"(?i)(?<=\))-(?=\w)", r" ", basename)
basename = re.sub(r"(?i)(?<=\s)-(?=\w)", r"", basename)
basename = re.sub(r"(?i)\b(\d+)E(\d\d)\b", r"s\1e\2", basename)
basename = re.sub(r"(?i)\bs\s*(\d+)\s*E\s*(\d\d)\b", r"s\1e\2", basename)
basename = re.sub(r"(?i)\bs(\d+)\.E(\d\d)\b", r"s\1e\2 ", basename)
basename = re.sub(r"(?i)-s(\d+)x?e(\d+)-", r" s\1e\2 ", basename)
basename = re.sub(r"(?i)\b(\d+)x?(\d\d+),", r" s\1e\2 ", basename)
if b != basename:
want_container = True
basename = re.sub(r"\.US\.", r".", basename)
basename = re.sub(r"([a-zA-Z])-(\d+)\b", r"\1\2", basename)
for piece in re.split(r"\.| +|_|\[|\]", basename):
was_time = False
if piece.endswith(","):
piece = piece[:-1]
if piece == "-": continue
if piece == "–": continue
if not piece.strip(): continue
if piece.lower().startswith(quash):
break
if piece.lower() == "dvdscr":
fragments.append("(screener)")
break
m = re.match(r"(?i)^\(S(\d+)E(\d+)\)$", piece)
if m:
want_container = True
container_finished = True
times.append("s%02de%02d" % (int(m.group(1)), int(m.group(2))))
was_time = True
m = re.match(r"(?i)^S(\d+)E(\d+)-?E(\d+)$", piece)
if m:
want_container = True
container_finished = True
times.append("s%02de%02d" % (int(m.group(1)), int(m.group(3))))
times.append("s%02de%02d" % (int(m.group(1)), int(m.group(2))))
was_time = True
m = re.match(r"(?i)^S(\d+)E(\d+)$", piece)
if m:
want_container = True
container_finished = True
times.append("s%02de%02d" % (int(m.group(1)), int(m.group(2))))
was_time = True
m = re.match(r"(?i)^S(\d+)$", piece)
if m:
want_container = True
container_finished = True
times.append("s%02d" % (int(m.group(1)),))
was_time = True
m = re.match(r"(?i)^E(\d+)$", piece)
if m:
want_container = True
container_finished = True
times.append("e%02d" % (int(m.group(1)),))
was_time = True
m = re.match(r"^(\d)(\d\d)$", piece)
if m:
if fragments and m.group(1)+m.group(2) != "720":
want_container = True
container_finished = True
times.append("s%02de%02d" % (int(m.group(1)), int(m.group(2))))
was_time = True
else:
pass # let fragments catch
m = re.match(r"^(\d+)x(\d+)$", piece)
if m:
want_container = True
container_finished = True
times.append("s%02de%02d" % (int(m.group(1)), int(m.group(2))))
was_time = True
m = re.match(r"^\(((?:201|200|19\d)\d)\)$", piece)
if m and not already_dated:
want_container = True
container_finished = True
container_pieces.append("(" + m.group(1) + ")")
times.append("%04d" % (int(m.group(1)),))
was_time = True
already_dated = True
m = re.match(r"^((?:201|200|19\d)\d)$", piece)
if m and not already_dated:
want_container = True
container_finished = True
container_pieces.append("(" + m.group(1) + ")")
times.append("%04d" % (int(m.group(1)),))
was_time = True
already_dated = True
m = re.match("^\(((?:201|200|19\d)\d).([01]\d).([0123]\d)\)$", piece)
if m and not already_dated:
want_container = True
container_finished = True
times.append("%s-%s-%s" % (m.group(1), m.group(2), m.group(3)))
#container_pieces.append("(" + m.group(1) + ")")
was_time = True
already_dated = True
m = re.match("^((?:201|200|19\d)\d).([01]\d).([0123]\d)$", piece)
if m and not already_dated:
want_container = True
container_finished = True
times.append("%s-%s-%s" % (m.group(1), m.group(2), m.group(3)))
#container_pieces.append("(" + m.group(1) + ")")
was_time = True
already_dated = True
if times:
if not was_time:
fragments.append("(" + ") (".join(sorted(times)) + ")")
times = list()
fragments.append(piece)
continue
fragments.append(piece)
if not container_finished:
container_pieces.append(piece)
basename = smart_title(" ".join(fragments)).strip("-")
dirs = os.listdir("/data/%%media/sorted/torrents")
candidates = sorted((d for d in dirs if basename in d), key=lambda s: (-len(s), s))
if candidates and candidates[0] != basename:
basename = candidates[0]
y = basename.split()[-1]
if y not in container_pieces:
container_pieces.append(basename.split()[-1])
if times:
for time in sorted(times):
if time not in basename:
basename += " (" + time + ")"
if is_language_file:
return smart_title(" ".join(container_pieces)) + "/" + orig_filename, extension.lower()
if want_container:
return smart_title(" ".join(container_pieces)) + "/" + basename + extension.lower(), extension.lower()
else:
return basename + extension.lower(), extension.lower()
def rename(dummy, container, filenames):
assert container.startswith("/data/%%media/unsorted")
unrooted_container = container[len("/data/%%media/unsorted")+1:]
imdbttid = None
nfo_file_contents = None
if unrooted_container.startswith("imdb-tt"):
first_slash_position = unrooted_container.find("/")
if first_slash_position == -1:
imdbttid = int(unrooted_container[8:])
else:
imdbttid = int(unrooted_container[8:first_slash_position])
nfo_file_contents = """<tvshow>\n<id moviedb="imdb">tt{}</id>\n</tvshow>""".format(imdbttid)
renamed = list()
for filename in filenames:
try:
if os.path.isfile(os.path.join(container, filename)):
if "trailer" in filename.lower():
logger.debug("should remove %r", filename)
continue
if "sample" in filename.lower():
logger.debug("should remove %r", filename)
continue
new_filename, extension = reformat(filename, container)
consistency = reformat(os.path.basename(new_filename), container)[0]
if new_filename != consistency:
raise ConsistencyError(new_filename + " != " + consistency +" // " + filename)
src = os.path.join(container, filename)
# FIXME set dst with show name
dst = os.path.join("/data/%%media/sorted/torrents", new_filename)
if extension not in kept_extensions:
print("not extension", filename)
continue
if src == dst:
print("srcdst", src)
continue
if os.sep not in new_filename:
if os.path.exists(dst):
logger.debug("Weird file %s (AND REMOVING!)", filename)
os.remove(dst)
print("weird", filename)
continue
else:
logger.debug("Just a file %s", new_filename)
if not os.path.exists(dst):
try:
os.mkdir(os.path.dirname(dst))
except OSError:
pass
try:
if nfo_file_contents and re.search(r"\s\(s\d\de\d\d\)", dst):
nfo_file = os.path.join(os.path.dirname(dst), "tvshow.nfo")
if not os.path.exists(nfo_file):
with open(nfo_file, "w") as f:
f.write(nfo_file_contents)
logger.debug("Wrote new tvshow nfo file near " + dst)
renamed.append(dst)
os.link(src, dst)
print((os.path.join(container, filename)))
except OSError, exc:
print(src, dst)
logger.warn("# couldn't link %s", (dst,))
raise
pass
else:
print("not file", filename)
except ConsistencyError, ex:
pass
#logger.warn("ignoring inconsistent name, %s", ex)
return renamed
import doctest
if doctest.testmod().failed > 0:
raise ValueError("Test errors")
try:
os.path.walk("/data/%%media/unsorted", rename, None)
except Exception, ex:
logger.exception("failed to complete")
raise
print(rename(None, "/data/%%media/unsorted/The.Karate.Kid.2010.1080p.BluRay.x264.AC3-ETRG", ["The.Karate.Kid.2010.1080p.BluRay.x264.AC3-ETRG.mp4"]))
subprocess.call(["find", "/data/%%media/sorted/torrents", "-type", "f", "-ctime", "+180", "-print", "-delete"])
subprocess.call(["find", "/data/%%media/unsorted", "-type", "f", "-links", "1", "-ctime", "+60", "-print", "-delete"])