-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
142 lines (115 loc) · 3.03 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
recursiveOption := flag.Bool("r", false, "recursive option")
noInteraction := flag.Bool("y", false, "No interaction")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
log.Fatalln("Error, path (1st argument) not provided")
}
directory := args[0]
directory = strings.TrimRight(directory, string(os.PathSeparator))
var filesChan chan []FileInfo
var err error
if *recursiveOption {
filesChan, err = rDirectoryScanner(directory)
if err != nil {
log.Fatalln(err)
}
} else {
filesChan, err = directoryScanner(directory)
if err != nil {
log.Fatalln(err)
}
}
renamer := Rename{
!*noInteraction,
}
fmt.Println(renamer)
for filesCollection := range filesChan {
matchSubtitles(filesCollection, renamer)
}
}
func matchSubtitles(files []FileInfo, renamer Rename) {
fc := extractFiles(files)
movies := fc.Movies
subs := fc.Subs
extractFilesError := fc.Err
if extractFilesError != nil {
log.Fatalln(extractFilesError)
}
fmt.Println("--- Movies")
for _, file := range movies {
fmt.Println(file.Name())
}
fmt.Println("--- Subs")
for index, file := range subs {
fmt.Println(index, file.Name())
}
// remove exact matches
scanAgain := true
for scanAgain {
scanAgain = false
for movieIndex, movie := range movies {
movieName := movie.Name()[:len(movie.Name())-len(filepath.Ext(movie.Name()))]
for subIndex, sub := range subs {
subName := sub.Name()[:len(sub.Name())-len(filepath.Ext(sub.Name()))]
if subName == movieName {
subs[subIndex], subs[len(subs)-1] = subs[len(subs)-1], subs[subIndex]
subs = subs[:len(subs)-1]
movies[movieIndex], movies[len(movies)-1] = movies[len(movies)-1], movies[movieIndex]
movies = movies[:len(movies)-1]
scanAgain = true
break
}
}
if scanAgain {
break
}
}
}
// Matching
for _, movie := range movies {
bestMatchScore := 0
var bestMatchFile FileInfo
var bestMatchIndex int
for subIndex, sub := range subs {
tempMatchScore := getMatchScore(movie.Name(), sub.Name(), 3)
// @TODO check for same score!
if bestMatchScore < tempMatchScore {
bestMatchScore = tempMatchScore
bestMatchFile = sub
bestMatchIndex = subIndex
}
}
if bestMatchScore == 0 {
fmt.Println("Skipping score '0'")
continue
}
fmt.Println("score ", bestMatchScore, movie.Name(), bestMatchFile.Name())
fmt.Println("----")
movieLenWithoutExt := len(movie.Name()) - len(filepath.Ext(movie.Name()))
subsExtension := filepath.Ext(bestMatchFile.Name())
fmt.Println("Matched subs for " + movie.Name())
renamed, renameError := renamer.Rename(
bestMatchFile.dir+string(os.PathSeparator)+bestMatchFile.Name(),
movie.dir+string(os.PathSeparator)+movie.Name()[0:movieLenWithoutExt]+subsExtension,
)
if renameError != nil {
fmt.Println(renameError)
}
if renamed {
// remove subs from the list
subs[bestMatchIndex], subs[len(subs)-1] = subs[len(subs)-1], subs[bestMatchIndex]
subs = subs[:len(subs)-1]
}
}
}