-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (122 loc) · 2.81 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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/drew-harris/asl-pro/database"
"github.com/fatih/color"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print(color.MagentaString("Enter tag to use: "))
scanner.Scan()
tag := scanner.Text()
err := database.SaveTag(tag)
if err != nil {
panic(err)
}
color.Magenta("Saved tag: " + tag)
for {
fmt.Print(color.CyanString("\n1. Download From Input.txt\n2. Download individual words\n3. Exit\nWhich would you like to do: "))
scanner.Scan()
choice := scanner.Text()
fmt.Print("\n")
if choice == "1" {
downloadFromFile(tag)
} else if choice == "2" {
typeToDownload(scanner, tag)
} else {
break
}
}
}
func typeToDownload(scanner *bufio.Scanner, tag string) {
for {
fmt.Print("Enter word: ")
scanner.Scan()
word := scanner.Text()
if word == "exit" {
break
}
err := downloadWord(word)
if err != nil {
color.Red(word + ": " + err.Error())
} else {
color.HiGreen("Downloaded word: " + word)
database.SaveWord(word, tag)
color.HiGreen("Saved word to DB: " + word)
}
}
}
func downloadFromFile(tag string) {
color.HiCyan("Getting words from input.txt")
words, err := getWords("./input.txt")
if err != nil {
panic(err)
}
var missed []string
for _, word := range words {
err = downloadWord(word)
if err != nil {
color.Red(word + ": " + err.Error())
missed = append(missed, word)
} else {
color.HiGreen("Downloaded word: " + word)
database.SaveWord(word, tag)
color.HiGreen("Saved word to DB: " + word)
}
}
fmt.Println("\n\nManually Required Words: ")
for _, word := range missed {
fmt.Println(word)
}
fmt.Print("\n\n")
}
func getWords(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
log.Panic(err)
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example
var words []string
for scanner.Scan() {
words = append(words, strings.ReplaceAll(strings.ToLower(scanner.Text()), " ", "_"))
}
if err := scanner.Err(); err != nil {
log.Panic(err)
return nil, err
}
return words, nil
}
func downloadWord(word string) error {
client := &http.Client{}
url := fmt.Sprintf("http://www.aslpro.cc/main/%s/%s.mp4", word[0:1], word)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Referer", url)
res, error := client.Do(req)
if error != nil {
return errors.New("could not generate http request")
}
defer res.Body.Close()
if res.ContentLength < 5000 {
return errors.New("word not found")
}
out, err := os.Create("./output/" + word + ".mp4")
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, res.Body)
if err != nil {
return errors.New("could not copy data to path")
}
return nil
}