-
Notifications
You must be signed in to change notification settings - Fork 0
/
plexize.go
393 lines (339 loc) · 10.2 KB
/
plexize.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
)
var uid int = -1
var gid int
var toRemove = [...]string{"unknown_release_type", "filmpokvip", "Film_pok"}
func init() {
u, _ := user.Lookup("plex")
if u == nil {
return
}
var err error
uid, err = strconv.Atoi(u.Uid)
if err != nil {
log.Panicf("cannot parse the number: %v\n", err)
}
gid, err = strconv.Atoi(u.Gid)
if err != nil {
log.Panicf("cannot parse the number: %v\n", err)
}
}
type patterns [11]*regexp.Regexp
func (p *patterns) match(s string) bool {
for _, r := range p {
if r.MatchString(s) {
return true
}
}
return false
}
// Inspired by PTN project (https://github.com/divijbindlish/parse-torrent-name/blob/master/PTN/patterns.py).
var commonPatterns = patterns{
regexp.MustCompile(`(?:PPV\.)?[HP]DTV|(?:HD)?CAM|hd-?ts|(?:PPV )?WEB-?DL(?: DVDRip)?|(?:D[vV][dD]|H[dD]|Cam|W[EB]B|B[DR])(?:(?i)rip)|(?:(?i)blu[-]?ray)|(?:(?i)telesync)|DvDScr|hdtv|PPV|Dvd`),
regexp.MustCompile(`MP3|DD5\.?1|Dual[\- ]Audio|LiNE|DTS|AAC[.-]LC|AAC(?:\.?2\.0|2)?|AC3(?:\.5\.1)?|Dual|Audio`),
regexp.MustCompile(`xvid|[hx]\.?26[45](?:(?i)-fov|-w4f)?`),
regexp.MustCompile(`(?i)hindi|(?:rus|ita)(?:\.eng|$)|eng$`),
regexp.MustCompile(`[1-9]\d+(?:\.\d+)?(?:(?i)gb|mb)`),
regexp.MustCompile(`(?i)EXTENDED(:?.CUT)?`),
regexp.MustCompile(`[1-9]\d{2,3}[pP]`),
regexp.MustCompile(`(?:Half-)?SBS`),
regexp.MustCompile(`MKV|AVI|MP4`),
regexp.MustCompile(`10bit`),
regexp.MustCompile(`UpScaled|iNTERNAL|CONVERT|READNFO|PROPER|REPACK|UNRATED|FRENCH|(?:(?i)hardsub)|(?:(?i)ensub)|(?:(?i)rarbg)|(?:(?i)hevc)|AMZN|PDTV|YIFY|1CD|WEB|NBY|R[0-9]|TS|HC|WS|3D`),
}
type movie struct {
sep string
name string
year string
season string
episode string
epiName string
}
type plexFile struct {
dir string
name string
ext string
mov movie
}
func (p *plexFile) parse() {
var (
// First movie ever was in 1888, so lets check movie years since 1800.
yearRe = regexp.MustCompile(`((?:1[8-9]|[2-9]\d)\d{2})`)
seasonRe = regexp.MustCompile(`[sS]?(\d{1,2})[eExX](\d{1,2})`)
domainRe = regexp.MustCompile(`^[wW]{2,3}\.[^.]*\.[^.]{3,4}(.*)$`)
bracePrefixRe = regexp.MustCompile(`^[\[\(🃏].*[\]\)🃏](.*)$`)
prefixRe = regexp.MustCompile(`^[^0-9a-zA-Z]*(.*)$`)
)
n := p.name
for _, s := range toRemove {
n = strings.ReplaceAll(n, s, "")
}
n = bracePrefixRe.ReplaceAllString(n, "$1")
n = domainRe.ReplaceAllString(n, "$1")
n = prefixRe.ReplaceAllString(n, "$1")
n = strings.ReplaceAll(n, " - ", " ")
max := 0
for _, s := range [...]string{" ", ".", "-", "_"} {
c := strings.Count(n, s)
if c > max {
max = c
p.mov.sep = s
continue
}
}
if p.mov.sep == "" {
p.mov.name = strings.Title(n)
return
}
ts := strings.Split(n, p.mov.sep)
done := false
seasoned := false
for _, t := range ts {
t = strings.Trim(t, " -[]()")
if !done {
if y := yearRe.FindString(t); y != "" && p.mov.name != "" {
done = true
p.mov.year = y
if i := strings.Index(t, y); i > 0 {
p.mov.name += t[:i-1] + " "
}
continue
}
if s := seasonRe.FindStringSubmatch(t); len(s) != 0 {
done = true
seasoned = true
p.mov.season = fmt.Sprintf("%02v", s[1])
p.mov.episode = fmt.Sprintf("%02v", s[2])
continue
}
if commonPatterns.match(t) {
done = true
continue
}
p.mov.name += t + " "
continue
}
if y := yearRe.FindString(t); y != "" {
if p.mov.year == "" {
p.mov.year = y
} else {
p.mov.name += p.mov.year + " "
p.mov.year = y
}
continue
}
if s := seasonRe.FindStringSubmatch(t); len(s) != 0 {
seasoned = true
p.mov.season = fmt.Sprintf("%02v", s[1])
p.mov.episode = fmt.Sprintf("%02v", s[2])
continue
}
if commonPatterns.match(t) {
break
}
if seasoned {
p.mov.epiName += t + " "
} else {
p.mov.name += t + " "
}
}
p.mov.name = strings.TrimSpace(p.mov.name)
p.mov.name = strings.Title(p.mov.name)
if seasoned {
p.mov.epiName = strings.TrimSpace(p.mov.epiName)
p.mov.epiName = strings.Title(p.mov.epiName)
}
}
func (p *plexFile) plexName() string {
if p.mov.name == "" {
return ""
}
if p.mov.season == "" {
if p.mov.year == "" {
return p.mov.name
}
return fmt.Sprintf("%s (%s)", p.mov.name, p.mov.year)
}
if p.mov.epiName == "" {
if p.mov.year == "" {
return fmt.Sprintf("%s - s%se%s", p.mov.name, p.mov.season, p.mov.episode)
}
return fmt.Sprintf("%s (%s) - s%se%s", p.mov.name, p.mov.year, p.mov.season, p.mov.episode)
}
if p.mov.year == "" {
return fmt.Sprintf("%s - s%se%s - %s", p.mov.name, p.mov.season, p.mov.episode, p.mov.epiName)
}
return fmt.Sprintf("%s (%s) - s%se%s - %s", p.mov.name, p.mov.year, p.mov.season, p.mov.episode, p.mov.epiName)
}
func (p *plexFile) plexDir() string {
if p.mov.name == "" {
return ""
}
if p.mov.year == "" {
return p.mov.name
}
return fmt.Sprintf("%s (%s)", p.mov.name, p.mov.year)
}
func (p *plexFile) seasonDir() string {
if p.mov.season == "" {
return ""
}
return fmt.Sprintf("Season %s", p.mov.season)
}
func usage() {
fmt.Fprintln(flag.CommandLine.Output(), `Movie and TV show files, Plex friendly maker.
Usage:
plexize [-]
plexize [OPTION]... FILE...
Options:
-d, --dry-run Show result without running
-m, --change-mode Change file mode to 660
-o, --change-owner Change file owner to plex:plex (sudo might be needed)
-p, --path PATH Output path (move file to the path and then refactor)
-s, --separate Separate movie files in their own folders (not required for TV series)
Example:
$ plexize # start in interactive mode to convert file(s) name
$ cat movie_list.txt | plexize # convert file(s) name with piping
$ plexize trainwreck.mkv war.dogs.2016.mkv # convert multiple files
$ plexize the*.mkv # convert multiple files with wildcard
$ plexize -d The.Platform.2019.720p.mkv # dry run
$ plexize -p ~/plex The.Platform.2019.720p.mkv # move the file to ~/plex and convert
$ plexize -m -o -s The.Platform.2019.720p.mkv # change mode/owner and move the movie file to its own folder
$ plexize -m -o The.Flash.2014.S01E01.HDTV.mkv # change mode/owner a TV show file (would be separated in its own folder)`)
}
func main() {
log.SetFlags(0)
var (
dryRun, chmod, chown, separate bool
outDir string
)
flag.Usage = usage
flag.BoolVar(&dryRun, "d", false, "Show result without running")
flag.BoolVar(&dryRun, "dry-run", false, "Show result without running")
flag.BoolVar(&chmod, "m", false, "Change file mode to 660")
flag.BoolVar(&chmod, "change-mode", false, "Change file mode to 660")
flag.BoolVar(&chown, "o", false, "Change file owner (default is plex:plex)")
flag.BoolVar(&chown, "change-owner", false, "Change file owner (default is plex:plex)")
flag.StringVar(&outDir, "p", "", "Output path (move file to the path and then refactor)")
flag.StringVar(&outDir, "path", "", "Output path (move file to the path and then refactor)")
flag.BoolVar(&separate, "s", false, "Separate movie files in their own folders (not required for TV series)")
flag.BoolVar(&separate, "separate", false, "Separate movie files in their own folders (not required for TV series)")
flag.Parse()
if flag.Arg(0) == "" || flag.Arg(0) == "-" {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
l := scanner.Text()
log.Printf("%s\n", convert(l, true, false, false, ""))
}
if err := scanner.Err(); err != nil {
log.Fatalf("cannot read from stdin: %v\n", err)
}
os.Exit(0)
}
if dryRun {
log.Println("Dry run...")
}
if chown {
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
chown = false
log.Println("the OS does not support changing the file owner")
} else if uid == -1 {
chown = false
log.Println("user plex does not exist, cannot change the file owner")
}
}
for i := 0; i < flag.NArg(); i++ {
var paths []string
var err error
if strings.Contains(flag.Arg(i), "*") {
paths, err = filepath.Glob(flag.Arg(i))
if err != nil {
log.Fatalf("invalid path format: %v\n", err)
}
} else {
paths = append(paths, flag.Arg(i))
}
for _, path := range paths {
np := convert(path, dryRun, separate, chown, outDir)
log.Printf("%s -> %s\n", path, np)
if !dryRun {
err := os.Rename(path, np)
if err != nil {
if os.IsPermission(err) {
log.Printf("you don't have permission to move/rename the file (you can retry with sudo): %v\n", err)
} else {
log.Printf("cannot move/rename the file: %v\n", err)
}
}
if chmod {
err := os.Chmod(np, 0660)
if err != nil {
log.Printf("cannot change the file mode: %v\n", err)
}
}
if chown {
err = os.Chown(np, uid, gid)
if os.IsPermission(err) {
log.Printf("you don't have permission to change owner of the file (you can retry with sudo): %v\n", err)
}
}
// TODO: support copy to server (delete local).
// TODO: support fixing title in metadata.
}
}
}
}
func convert(path string, dryRun, separate, chown bool, outDir string) (newPath string) {
dir, file := filepath.Split(path)
ext := filepath.Ext(file)
pf := &plexFile{
dir: dir,
name: strings.TrimSuffix(file, ext),
ext: strings.ToLower(ext),
mov: movie{},
}
pf.parse()
ps := make([]string, 0, 4)
ps = append(ps, dir)
if outDir != "" {
ps[0] = outDir
}
if separate || pf.mov.season != "" {
ps = append(ps, pf.plexDir())
if !dryRun {
makeDir(chown, "cannot make separate movie or TV serie folder: %v\n", ps...)
}
}
if pf.mov.season != "" {
ps = append(ps, pf.seasonDir())
if !dryRun {
makeDir(chown, "cannot make TV serie season folder: %v\n", ps...)
}
}
ps = append(ps, pf.plexName())
return fmt.Sprintf("%s%s", filepath.Join(ps...), pf.ext)
}
func makeDir(chown bool, errMsg string, ps ...string) {
err := os.MkdirAll(filepath.Join(ps...), os.ModePerm)
if err != nil && !os.IsExist(err) {
log.Printf(errMsg, err)
}
if chown {
err = os.Chown(filepath.Join(ps...), uid, gid)
if os.IsPermission(err) {
log.Printf("you don't have permission to change owner of the file (you can retry with sudo): %v\n", err)
}
}
}