-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (95 loc) · 2.41 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
package main
import (
"fmt"
"log"
"os"
"strconv"
)
var WGET_ERR *os.File
var APP_LOG *os.File
var FAILED *os.File
var start int
var end int
func main() {
var err error
args := os.Args[1:]
if len(args) < 1 {
fmt.Println("No name provided!")
return
}
name := args[0]
if len(args) > 1 {
start, err = strconv.Atoi(args[1])
if err != nil {
log.Fatalf("Start must Be a number!\n")
}
start -= 1
}
if len(args) > 2 {
end, err = strconv.Atoi(args[2])
if err != nil {
log.Fatalf("End must Be a number!\n")
}
}
if end != 0 && start > end {
log.Fatalf("Start must be less than end!\n")
}
WGET_ERR, err = os.OpenFile(name+"_wget.err", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening %s_wget.err\n%v\n\n", name, err)
}
defer WGET_ERR.Close()
APP_LOG, err = os.OpenFile(name+"_app.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening %s_app.err\n%v\n\n", name, err)
}
defer APP_LOG.Close()
FAILED, err = os.OpenFile(name+"_failures.err", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening %s_failures.err\n%v\n\n", name, err)
}
defer FAILED.Close()
fmt.Println("Getting playlist XML...")
x := GetXML(name)
fmt.Println("Parsing playlist XML")
episodes := ParseXML(x)
if end == 0 {
end = len(episodes)
}
var errs []string
fmt.Println("Starting episodes download...")
episodes = episodes[start:end]
fmt.Println(len(episodes))
for i, episode := range episodes {
if episode.HD.FileLocation != "" {
fmt.Printf("Downloading HD video %d of %d...\n", i+1, len(episodes))
err := Download(episode.HD.FileLocation, name)
if err == nil {
continue
}
Logger(APP_LOG, err.Error())
}
Logger(APP_LOG, "Episode %s has no HD link", episode.Title)
if episode.SD.FileLocation != "" {
fmt.Printf("Downloading SD video %d of %d...\n", i+1, len(episodes))
err := Download(episode.SD.FileLocation, name)
if err == nil {
continue
}
Logger(APP_LOG, err.Error())
}
Logger(APP_LOG, err.Error())
Logger(FAILED, "Episode #%d: %s", i+1, episode.Title)
errs = append(errs, fmt.Sprintf("Episode #%d: %s", i+1, episode.Title))
}
if len(errs) > 0 {
fmt.Printf("Unable to download the following...\n\n")
for _, s := range errs {
fmt.Println(s)
}
}
}
func Logger(f *os.File, s string, v ...interface{}) {
log.SetOutput(f)
log.Printf("\n"+s+"\n\n", v...)
}