-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
97 lines (85 loc) · 2.16 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
package main
import (
"fmt"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
)
var (
// version and build info
buildStamp string
gitHash string
goVersion string
version string
client *http.Client
token *Token
logger *zap.SugaredLogger
)
// SetupCloseHandler creates a 'listener' on a new goroutine which will notify the
// program if it receives an interrupt from the OS. We then handle this by calling
// our clean-up procedure and exiting the program.
func setupCloseHandler() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0)
}()
}
func main() {
setupCloseHandler()
var options = InitOptions()
if options.Version {
logger.Infof("Current version: %s", version)
logger.Infof("Git Commit Hash: %s", gitHash)
logger.Infof("UTC Build Time : %s", buildStamp)
logger.Infof("Golang Version : %s", goVersion)
os.Exit(0)
} else {
client = newClient(options.Proxy)
err := os.MkdirAll(options.Output, os.ModePerm)
if err != nil {
logger.Fatalf("failed to create output directory: %v", err)
}
token, err = NewToken(
options.Username, options.Password,
filepath.Join(options.Output, fmt.Sprintf("%s.json", options.Username)))
if err != nil {
logger.Fatal(err)
}
var wg sync.WaitGroup
files := decodeTCIA(options.Input)
wg.Add(options.Concurrent)
inputChan := make(chan *FileInfo, 5)
for i := 0; i < options.Concurrent; i++ {
go func(input chan *FileInfo) {
defer wg.Done()
for i := range input {
if _, err := os.Stat(i.MetaFile(options.Output)); os.IsNotExist(err) {
if !options.Meta {
if err := i.Download(options.Output); err != nil {
logger.Warnf("Download %s failed - %s", i.SeriesUID, err)
} else {
if err := i.GetMeta(options.Output); err != nil {
logger.Warnf("save meta info %s failed - %s", i.SeriesUID, err)
}
}
}
} else {
logger.Infof("Skip %s", i.SeriesUID)
}
}
}(inputChan)
}
for _, f := range files {
inputChan <- f
}
close(inputChan)
wg.Wait()
}
}