forked from AvivRubys/gosub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosub.go
84 lines (70 loc) · 1.82 KB
/
gosub.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
package main
import (
"flag"
"fmt"
"io"
"log"
"mime"
"os"
"path/filepath"
"strings"
"github.com/kardianos/osext"
"github.com/AvivRubys/gosub/providers"
)
const logFileName = "gosub.log"
var (
language = flag.String("language", "en", "")
help = flag.Bool("h", false, "Help")
)
func main() {
flag.Parse()
if *help {
usage := `Usage: gosub (flags) [FILES/DIRECTORIES]
-language="eng": Language to search with
-h: Display this help message`
fmt.Println(usage)
os.Exit(0)
}
// Set up proper logging
dir, _ := osext.ExecutableFolder()
logfile, err := os.OpenFile(dir+logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("Error opening logfile %s (%v)", dir+logFileName, err)
}
defer logfile.Close()
log.SetOutput(io.MultiWriter(logfile, os.Stderr))
// Make sure we have all needed mime types
mime.AddExtensionType(".mkv", "video/x-matroska")
mime.AddExtensionType(".mp4", "video/mp4")
mime.AddExtensionType(".avi", "video/avi")
// Arguments we have been given may be files or directories,
// filter them for video files
var files []string
if args := flag.Args(); len(args) >= 1 {
for _, arg := range args {
filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Error in walking directory %s\n", err)
return nil
}
fileInfo, err := os.Stat(path)
if err != nil {
log.Printf("Error in opening path %s\n", path)
return nil
}
if fileInfo.IsDir() {
return nil
}
mimeType := mime.TypeByExtension(filepath.Ext(path))
if strings.Contains(mimeType, "video") {
files = append(files, path)
} else {
log.Printf("Ignoring %s (not a video file)\n", filepath.Base(path))
}
return nil
})
}
}
db := providers.GetSubtitleDB()
db.GetAll(files, *language)
}