-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_series.py
executable file
·70 lines (54 loc) · 1.79 KB
/
rename_series.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
import os
import argparse
import re
import sys
def yes_no(prompt):
try:
q = raw_input(prompt + " [Y/n] ").lower()
except KeyboardInterrupt: sys.exit(0)
except EOFError: sys.exit(0)
if len(q) == 0 or q == "y":
return True
return False
def get_next_regex():
ext = '.*\.(?P<ext>avi|mkv|mp4|srt|ass)'
regexs = [
'(s|S)(?P<season>\d{2})( |-)?(E|e)(?P<episode>\d{2})',
'season (?P<season>\d+)..(?P<episode>\d+)',
]
for r in regexs:
yield r + ext
while 1:
regex = raw_input('custom regex?: ')
if yes_no('use "{0}"?'.format(regex)) is True:
yield regex + ext
def rename_series(folder, regex=None):
def _generate_filename(g):
return 'S{0:>02}E{1:>02}.{2}'.format(g.group('season'), g.group('episode'), g.group('ext'))
try:
ls = os.listdir(folder)
except Exception as e:
print e
return
for regex in get_next_regex():
err = False
for f in ls:
g = re.search(regex, f)
if g is None:
print 'failed: file: "{0}", regex "{1}"'.format(f, regex)
err = True
break
print f, ' => ', _generate_filename(g)
if err is False:
print 'ok: "{0}"'.format(regex)
if yes_no('go with this one?') is True:
for f in ls:
g = re.search(regex, f)
os.rename(f, _generate_filename(g))
return
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('folder', help='folder to work on', type=str)
parser.add_argument('-r', '--regex', help='use special regex', type=str)
args = parser.parse_args()
rename_series(folder=args.folder, regex=args.regex)