-
Notifications
You must be signed in to change notification settings - Fork 1
/
dvdorder
executable file
·171 lines (154 loc) · 6.24 KB
/
dvdorder
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
#!/usr/bin/env python3
# -*- python -*-
#
# Copyright 2016 by Chris Osborn <[email protected]>
#
# This file is part of viddin.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
import sys
import argparse
import os
import re
import sys
import math
import csv
import fnmatch
import viddin
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument("series", help="Name of series")
parser.add_argument("--rename", action="store_true", help="rename files to match dvd order")
parser.add_argument("--dvdmissing", action="store_true",
help="use episode data when dvd info is missing")
parser.add_argument("--dvdignore", action="store_true", help="ignore dvd episode data")
parser.add_argument("--enumerate", action="store_true",
help="Episodes are in order but may have gaps")
parser.add_argument("--first", default=1, help="Start enumeration at this number")
parser.add_argument("--minsize", help="Ignore videos smaller than this")
parser.add_argument("--dvdorder", action="store_true", help="keep episodes in DVD order")
parser.add_argument("--absolute", action="store_true", help="put episodes in absolute order")
parser.add_argument("--airedorder", action="store_true",
help="episodes are currently in aired order")
parser.add_argument("--csv", action="store_true", help="output data as csv")
parser.add_argument("--quiet", action="store_true",
help="don't print errors about missing DVD info")
parser.add_argument("--regex", help="pattern for finding videos",
default="[0-9]+x[0-9][0-9]+\\.(mkv|mp4)")
parser.add_argument("--glob", help="glob pattern for finding videos")
parser.add_argument("--dryrun", action="store_true", help="Don't actually rename files")
parser.add_argument("--interactive", action="store_true",
help="Choose from list of matching titles")
parser.add_argument("--split", action="store_true",
help="DVD episodes have been split into individual cartoons")
parser.add_argument("--tmdb", action="store_true", help="Use TheMovieDB to get episode data")
return parser
# FIXME - make finding the file as smart as tvrenamer.pl
def main():
args = build_argparser().parse_args()
if os.path.isfile(args.series):
series = viddin.loadEpisodeInfoFromCSV(args.series)
elif args.tmdb:
series = viddin.loadEpisodeInfoFromTMDB(args.series, dvdIgnore=args.dvdignore,
dvdMissing=args.dvdmissing, quietFlag=args.quiet,
interactiveFlag=args.interactive)
else:
series = viddin.loadEpisodeInfoFromTVDB(args.series, dvdIgnore=args.dvdignore,
dvdMissing=args.dvdmissing, quietFlag=args.quiet,
interactiveFlag=args.interactive)
dvdorder = viddin.EpisodeList(series, "dvdID")
airedorder = viddin.EpisodeList(series, "airedID")
files = os.listdir(".")
pattern = args.regex
if args.glob:
pattern = fnmatch.translate(args.glob)
videos = [f for f in files if re.match(pattern, f) and f[0] != '.']
videos.sort()
minsize = 0
if args.minsize:
val = re.sub("^( *[0-9]+).*", "\\1", args.minsize)
minsize = int(val)
suffix = args.minsize[-1]
try:
magnitude = ["B", "K", "M", "G"].index(suffix.upper())
except ValueError:
magnitude = -1
if magnitude >= 0:
minsize *= math.pow(1024, magnitude)
if args.csv:
csvdata = []
for row in dvdorder.episodes:
info = [airedorder.formatEpisodeID(row), \
"%ix%04.1f" % (row.dvdID.season, row.dvdID.episode), row.airDate, row.title,
row.absoluteNum]
csvdata.append(info)
a = csv.writer(sys.stdout)
a.writerows(csvdata)
elif args.rename:
epnum = 0
if args.airedorder:
eids = airedorder.episodeNumbers()
else:
eids = dvdorder.episodeNumbers(args.split)
if viddin.isint(args.first):
first = int(args.first)
else:
if args.airedorder:
episode = airedorder.findEpisode(args.first)
first = airedorder.indexOf(episode) + 1
else:
episode = dvdorder.findEpisode(args.first)
first = dvdorder.indexOf(episode) + 1
for idx, e in enumerate(eids):
if e == args.first:
first = idx + 1
break
# FIXME - use magic from renumber to order renaming in best
# possible way to avoid conflicts with other files that will be
# renamed.
for filename in videos:
if minsize:
size = os.path.getsize(filename)
if size < minsize:
continue
video, ext = os.path.splitext(filename)
epid = video
if args.enumerate:
oid = epid
e_idx = epnum + first - 1
if e_idx >= len(eids):
break
epid = eids[e_idx]
order = viddin.EpisodeOrder.AIRED
if args.dvdorder:
order = viddin.EpisodeOrder.DVD
if args.absolute:
order = viddin.EpisodeOrder.ABSOLUTE
if args.airedorder:
episode = airedorder.findEpisode(epid, fractional=args.split)
if episode:
filename = airedorder.renameVid(episode, filename, order, args.dryrun)
elif args.dvdorder:
episode = dvdorder.findEpisode(epid, order, fractional=args.split)
if episode:
filename = dvdorder.renameVid(episode, filename, order, args.dryrun)
else:
episode = dvdorder.findEpisode(epid, fractional=args.split)
if episode:
filename = airedorder.renameVid(episode, filename, order, args.dryrun)
epnum += 1
else:
for row in dvdorder.episodes:
print(dvdorder.episodes.index(row) + 1, row.description())
return
if __name__ == '__main__':
exit(main() or 0)