-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
157 lines (135 loc) · 3.42 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"dmm-scraper/pkg/config"
"dmm-scraper/pkg/img"
"dmm-scraper/pkg/logger"
"dmm-scraper/pkg/metadata"
"dmm-scraper/pkg/scraper"
)
var (
posterWidth = 378
)
func isValidVideo(ext string) bool {
switch strings.ToLower(ext) {
case
".wmv",
".mp4",
".avi",
".mkv":
return true
}
return false
}
func MyProgress(l logger.Logger, sType, filename string) func(current, total int64) {
return func(current, total int64) {
l.Infof(fmt.Sprintf("%s downloading %s ... %f%%", sType, filename, float32(current)/float32(total)*100))
}
}
func main() {
var err error
log := logger.New()
conf, err := config.NewLoader().LoadFile("config")
if err != nil {
log.Errorf("Error reading config file, %s", err)
log.Warnf("Loading default config")
conf = config.Default()
}
scraper.Setup(conf)
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
if f.IsDir() {
continue
}
ext := filepath.Ext(f.Name())
if !isValidVideo(ext) {
continue
}
log.Infof("Check file: %s", f.Name())
name := strings.TrimSuffix(f.Name(), ext)
// 用正则处理文件名
if query, scrapers := scraper.GetQuery(name); query != "" {
for _, s := range scrapers {
log.Infof("%s capturing query: %s", s.GetType(), query)
// fetch
err = s.FetchDoc(query)
if err != nil {
log.Error(err)
continue
}
if s.GetNumber() == "" {
log.Errorf("%s get num empty", s.GetType())
continue
}
num := s.GetFormatNumber()
log.Infof("%s get num %s format: %s", s.GetType(), s.GetNumber(), num)
// mkdir
outputPath := scraper.GetOutputPath(s, conf.Output.Path)
log.Infof("%s making output path: %s", s.GetType(), outputPath)
err = os.MkdirAll(outputPath, 0700)
if err != nil && !os.IsExist(err) {
log.Error(err)
break
}
// build nfo
movieNfo := metadata.NewMovieNfo(s)
poster := fmt.Sprintf("%s.jpg", num)
// movieNfo.SetPoster(poster)
movieNfo.SetTitle(num)
posterPath := path.Join(outputPath, poster)
err = scraper.Download(s.GetCover(), posterPath, MyProgress(log, s.GetType(), poster))
if err != nil {
log.Error(err)
break
}
if s.NeedCut() {
log.Infof("%s cropping poster: %s", s.GetType(), posterPath)
imgOperation := img.NewOperation()
err = imgOperation.CropAndSave(posterPath, posterPath, posterWidth, 0)
if err != nil {
log.Error(err)
}
}
nfo := path.Join(outputPath, fmt.Sprintf("%s.nfo", num))
log.Infof("%s writing nfo file: %s", s.GetType(), nfo)
err = movieNfo.Save(nfo)
if err != nil {
log.Error(err)
break
}
log.Infof("%s moving video file to: %s", s.GetType(), outputPath)
// if file exist no overwrite
err = MoveFile(f.Name(), outputPath, num, 1)
if err != nil {
log.Error(err)
}
break
}
}
}
}
func MoveFile(oldPath, outputPath, num string, index int) error {
var filename string
if _, err := os.Stat(oldPath); os.IsNotExist(err) {
return err
}
if index != 1 {
filename = fmt.Sprintf("%s-cd%d%s", num, index, filepath.Ext(oldPath))
} else {
filename = fmt.Sprintf("%s%s", num, filepath.Ext(oldPath))
}
newPath := path.Join(outputPath, filename)
if _, err := os.Stat(newPath); err == nil {
index += 1
return MoveFile(oldPath, outputPath, num, index)
}
return os.Rename(oldPath, newPath)
}