-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmd_rename.go
515 lines (468 loc) · 14.3 KB
/
cmd_rename.go
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package main
// This command seems to work well, but the code below is a mess. It was
// written stream-of-consciousness, but I think there are more opportunities
// for abstraction. In particular, I think it should be easier to customize
// the behavior of the "smart" modes of this command.
//
// TODO: Rewrite (or major refactor) the 'rename' command when we get a better
// idea of what its behavior should be.
import (
"bytes"
"flag"
"os"
path "path/filepath"
"regexp"
"strconv"
"strings"
"github.com/BurntSushi/ty/fun"
"github.com/BurntSushi/goim/imdb"
"github.com/BurntSushi/goim/imdb/search"
"github.com/BurntSushi/goim/tpl"
)
var (
flagRenameTvshow = ""
flagRenameRegexEpisode = `\b[Ss]([0-9]+)[Ee]([0-9]+)\b`
flagRenameRegexYear = `\b([0-9]{4})\b`
flagRenameTvshowName = false
flagVotes = 10000
)
var cmdRename = &command{
name: "rename",
positionalUsage: "[ query ] file [ file ... ]",
shortHelp: "renames files to match search results",
help: `
NOTICE: Note that this command is in BETA. It should work OK, but I'm not
sold completely on its behavior. This means that its interface could change.
This command will ALWAYS prompt you before renaming your files.
The rename command renames files to names found in IMDb's database. The naming
scheme is specified in templates with the "rename_" prefix found in your
command.tpl file.
The most general operation is to provide an arbitrary search query as the first
argument (so it must be in quotes, unlike with 'goim search') and a set of
files. This command will assume that the search results are in correspondence
with your files and attempt to rename them as such. This is the most cumbersome
interface but it is also the most precise. In this mode, Goim does not try to
guess anything---the results of your search are taken for gospel.
This command can also be smart by looking for key pieces of information that
are frequently in similar formats (like years or season/episode numbers). Note
that this "smart" mode assumes that fuzzy searching is available.
If the first argument is a file name (i.e., the query is omitted), then Goim
will try to be smart and guess what the file corresponds to based on any name
or year information. This only works with movies or episodes. For movies, a
big piece of distinguishing information is the year, which is extracted with
the regular expression in the 'match-year' flag.
If you're renaming multiple episodes, use the '-tv' flag to specify the TV show
and omit the query. This will also be significantly faster, since only one
search will be performed instead of a search for each file. Goim will try to
extract episode numbers from the file names. This extraction can be controlled
with the 'match-episode' flag.
`,
flags: flag.NewFlagSet("rename", flag.ExitOnError),
run: cmd_rename,
addFlags: func(c *command) {
c.flags.StringVar(&flagRenameTvshow, "tv", flagRenameTvshow,
"Set this to a search query, and the set of files will be\n"+
"interpreted as a list of episodes for the matching TV show.\n"+
"When this is set, episode names are guessed. No additional\n"+
"search query should be provided.")
c.flags.BoolVar(&flagRenameTvshowName, "tvname",
flagRenameTvshowName,
"When set, the name of the TV show is included as a prefix\n"+
"when renaming an episode.")
c.flags.StringVar(&flagRenameRegexEpisode, "match-episode",
flagRenameRegexEpisode,
"An RE2 regular expression for matching the season and episode\n"+
"numbers in an episode file name. The regex MUST contain two\n"+
"capturing groups, where the first is the season number and\n"+
"the second is the episode number.")
c.flags.StringVar(&flagRenameRegexYear, "match-year",
flagRenameRegexYear,
"An RE2 regular expression for matching the year in a file name.\n"+
"The regex MUST contain one capturing group for the year\n"+
"as an integer.")
c.flags.IntVar(&flagVotes, "votes", flagVotes,
"The minimum number of votes required for results. For episodes,\n"+
"this applies to the TV show. This is not used when the\n"+
"query is fully specified.")
},
}
func cmd_rename(c *command) bool {
if len(flagRenameTvshow) > 0 {
return cmd_rename_tvshow(c, flagRenameTvshow)
}
firstArg := c.flags.Arg(0)
if _, err := os.Stat(firstArg); err == nil {
return cmd_rename_smart(c)
}
c.assertLeastNArg(2)
db := openDb(c.dbinfo())
defer closeDb(db)
query := c.flags.Arg(0)
files := fun.Map(path.Clean, c.flags.Args()[1:]).([]string)
searcher, err := search.Query(db, query)
if err != nil {
pef("%s", err)
return false
}
searcher.Chooser(c.chooser)
searcher.Limit(len(files))
results, err := searcher.Results()
if err != nil {
pef("%s", err)
return false
}
if len(results) == 0 {
pef("No search results.")
return false
}
if len(results) < len(files) {
pef("Omitting last %d file(s) since there are only %d search results.",
len(files)-len(results), len(results))
files = files[0:len(results)]
}
entities := make([]imdb.Entity, len(results))
for i, r := range results {
e, err := r.GetEntity(db)
if err != nil {
pef("Could not get entity for '%s': %s", r, err)
return false
}
entities[i] = e
}
return doRename(c, db, files, entities)
}
func cmd_rename_smart(c *command) bool {
c.assertLeastNArg(1)
db := openDb(c.dbinfo())
defer closeDb(db)
files := fun.Map(path.Clean, c.flags.Args()).([]string)
var oldNames []string
var newNames []imdb.Entity
for _, file := range files {
e, err := guessEntity(c, db, file)
if err != nil {
pef("Could not guess entity for '%s': %s", file, err)
continue
}
oldNames = append(oldNames, file)
newNames = append(newNames, e)
}
return doRename(c, db, oldNames, newNames)
}
func cmd_rename_tvshow(c *command, tvQuery string) bool {
c.assertLeastNArg(1)
db := openDb(c.dbinfo())
defer closeDb(db)
files := fun.Map(path.Clean, c.flags.Args()).([]string)
tv, err := searchTvshow(c, db, tvQuery)
if err != nil {
pef("%s", err)
return false
}
episodes, err := tvEpisodes(db, tv)
if err != nil {
pef("%s", err)
return false
}
var oldNames []string
var newNames []imdb.Entity
for _, file := range files {
baseFile := path.Base(file)
s, e, _, _, err := episodeNumbers(baseFile, flagRenameRegexEpisode)
if err != nil {
pef("Could not find episode numbers in '%s': %s", file, err)
continue
}
if s == 0 || e == 0 {
pef("Found numbers, but they look wrong: (s: %d, e: %d)", s, e)
continue
}
ep, ok := episodes[episodeKey{s, e}]
if !ok {
pef("Could not find episode (%s, S%02dE%02d) for '%s'.",
tv, s, e, file)
continue
}
oldNames = append(oldNames, file)
newNames = append(newNames, ep)
}
return doRename(c, db, oldNames, newNames)
}
func doRename(
c *command,
db *imdb.DB,
files []string,
entities []imdb.Entity,
) bool {
names := renames(c, db, files, entities)
if len(names) == 0 {
return false
}
if len(names) != len(files) {
pef("BUG: Have %d names but have %d files.", len(names), len(files))
return false
}
for i := range names {
oldName, newName := files[i], names[i]
pf("Rename '%s' to '%s'\n", oldName, newName)
}
if !areYouSure("Are you sure you want to rename these files?") {
return true
}
for i := range names {
if err := os.Rename(files[i], names[i]); err != nil {
pef("Error renaming '%s' to '%s': %s", files[i], names[i], err)
return false
}
}
return true
}
func renames(
c *command,
db *imdb.DB,
files []string,
entities []imdb.Entity,
) []string {
var names []string
tpl.SetDB(db)
for i := range files {
file, ent := files[i], entities[i]
t := c.tpl(sf("rename_%s", ent.Type()))
// If this file is a directory, don't both with extensions.
stat, err := os.Stat(file)
if err != nil {
pef("%s", err)
return nil
}
ext := path.Ext(file)
if stat.IsDir() {
ext = ""
}
buf := new(bytes.Buffer)
attrs := tpl.Attrs{"Ext": ext, "ShowTv": flagRenameTvshowName}
if err := t.Execute(buf, tpl.Args{E: ent, A: attrs}); err != nil {
pef("%s", err)
return nil
}
name := strings.TrimSpace(buf.String())
name = strings.Replace(name, "/", "_", -1)
name = path.Join(path.Dir(file), name)
names = append(names, name)
}
return names
}
func searchTvshow(c *command, db *imdb.DB, query string) (*imdb.Tvshow, error) {
tvsearch, err := search.Query(db, query)
if err != nil {
return nil, err
}
tvsearch.Chooser(c.chooser)
tvsearch.Entity(imdb.EntityTvshow)
tvsearch.Votes(flagVotes, -1)
results, err := tvsearch.Results()
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, ef("Could not find TV show.")
}
tv, err := tvsearch.Pick(results)
if err != nil {
return nil, err
}
if tv == nil {
return nil, ef("No TV show results to pick from.")
}
if tv.Entity != imdb.EntityTvshow {
return nil, ef("Expected TV show but got %s", tv.Entity)
}
ent, err := tv.GetEntity(db)
if err != nil {
return nil, err
}
return ent.(*imdb.Tvshow), nil
}
type episodeKey struct {
s, e int
}
type episodeMap map[episodeKey]*imdb.Episode
func tvEpisodes(db *imdb.DB, tv *imdb.Tvshow) (episodeMap, error) {
episodes := make(episodeMap, 30)
epsearch := search.New(db)
epsearch.Entity(imdb.EntityEpisode)
epsearch.Tvshow(search.New(db).Atom(tv.Id))
epsearch.Seasons(1, -1).Episodes(1, -1)
epsearch.Limit(-1)
results, err := epsearch.Results()
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, ef("Could not find any episodes for %s", tv)
}
for _, r := range results {
ent, err := r.GetEntity(db)
if err != nil {
return nil, err
}
ep := ent.(*imdb.Episode)
episodes[episodeKey{ep.Season, ep.EpisodeNum}] = ep
}
return episodes, nil
}
func guessEntity(c *command, db *imdb.DB, fname string) (imdb.Entity, error) {
// Look for episode numbers. If we can find them, then this is an
// episode.
fname = path.Base(fname)
_, _, _, _, err := episodeNumbers(fname, flagRenameRegexEpisode)
if err == nil {
return guessEpisode(c, db, fname)
} else {
return guessMovie(c, db, fname)
}
}
func guessEpisode(
c *command,
db *imdb.DB,
fname string,
) (*imdb.Episode, error) {
fname = path.Base(fname)
s, e, start, _, err := episodeNumbers(fname, flagRenameRegexEpisode)
if err != nil {
return nil, ef("Could not find episode numbers: %s", err)
}
// A guess at where the TV show name is in the file name.
title := strings.TrimSpace(fname[0:start])
tvsub, err := search.Query(db, title)
if err != nil {
return nil, err
}
tvsub.Entity(imdb.EntityTvshow)
tvsub.Votes(flagVotes, -1)
esearch := search.New(db)
esearch.Tvshow(tvsub)
esearch.Entity(imdb.EntityEpisode)
esearch.Seasons(s, s).Episodes(e, e)
esearch.Chooser(c.chooser)
results, err := esearch.Results()
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, ef("Could not find episode.")
}
m, err := esearch.Pick(results)
if err != nil {
return nil, err
}
if m == nil {
return nil, ef("No episode results to pick from.")
}
if m.Entity != imdb.EntityEpisode {
return nil, ef("Expected episode but got %s", m.Entity)
}
ent, err := m.GetEntity(db)
if err != nil {
return nil, err
}
return ent.(*imdb.Episode), nil
}
func guessMovie(c *command, db *imdb.DB, fname string) (*imdb.Movie, error) {
fname = path.Base(fname)
year, ystart, _, err := fileNameYear(fname, flagRenameRegexYear)
if err != nil {
return nil, ef("Could not find year for movie: %s", err)
}
// A guess at where the title is in the file name.
title := strings.TrimSpace(fname[0:ystart])
msearch, err := search.Query(db, title)
if err != nil {
return nil, err
}
msearch.Entity(imdb.EntityMovie)
msearch.Years(year-1, year+1)
msearch.Chooser(c.chooser)
msearch.Votes(flagVotes, -1)
results, err := msearch.Results()
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, ef("Could not find movie.")
}
m, err := msearch.Pick(results)
if err != nil {
return nil, err
}
if m == nil {
return nil, ef("No movie results to pick from.")
}
if m.Entity != imdb.EntityMovie {
return nil, ef("Expected movie but got %s", m.Entity)
}
ent, err := m.GetEntity(db)
if err != nil {
return nil, err
}
return ent.(*imdb.Movie), nil
}
// fileNameYear attempts to extract the year of release for an entity from a
// file name using the RE2 regular expression provided. If the match is
// unsuccessful, 0, -1, -1 is returned. The regex must have exactly 1 capturing
// group, where the group corresponds to the year which must be an integer.
// The triple of integers returned correspond to the year and the start and end
// indices of the year in 'fname'.
//
// fname should be a base name of a file path.
func fileNameYear(fname, regex string) (year, start, end int, err error) {
reg, err := regexp.Compile(regex)
if err != nil {
return 0, -1, -1, ef("Could not compile regex '%s': %s", regex, err)
}
groups := reg.FindStringSubmatchIndex(fname)
if len(groups) != 4 || groups[2] == -1 || groups[3] == -1 {
return 0, -1, -1, ef("Unsuccessful match.")
}
start, end = groups[2], groups[3]
year64, err := strconv.Atoi(fname[groups[2]:groups[3]])
if err != nil {
return 0, -1, -1, ef("Could not parse '%s' as int: %s", groups[1], err)
}
year = int(year64)
return
}
// episodeNumbers attempts to extract the season and episode numbers from
// a file name. If the match is unsuccessfull, zeroes are returned. The regex
// must have exactly two capturing groups, where the first is the season and
// the second is the episode. Both of these capturing groups must correspond
// to integers.
//
// fname should be a base name of a file path.
func episodeNumbers(
fname,
regex string,
) (season, episode, start, end int, err error) {
fname = path.Base(fname)
reg, err := regexp.Compile(regex)
if err != nil {
return 0, 0, -1, -1, ef("Could not compile regex '%s': %s", regex, err)
}
groups := reg.FindStringSubmatchIndex(fname)
if len(groups) != 6 || groups[2] == -1 || groups[5] == -1 {
return 0, 0, -1, -1, ef("Unsuccessful match.")
}
sstart, send := groups[2], groups[3]
estart, eend := groups[4], groups[5]
start, end = sstart, eend
nseason, err := strconv.Atoi(fname[sstart:send])
if err != nil {
return 0, 0, -1, -1,
ef("Could not parse '%s' as an int: %s", groups[1], err)
}
nepisode, err := strconv.Atoi(fname[estart:eend])
if err != nil {
return 0, 0, -1, -1,
ef("Could not parse '%s' as an int: %s", groups[2], err)
}
return int(nseason), int(nepisode), start, end, nil
}