-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (274 loc) · 6.85 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"bufio"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/yokee99/neute/mp4utils"
"github.com/yokee99/neute/utils"
)
var (
/*
*参数列表
*/
count int
wg sync.WaitGroup
urllist []string
fileName string
concurrent int
mp4FrameCut int
timeout int
h bool
dontdownloadflag bool
ch chan int
blockcount int
finished int
failurl []string
failurlT []string
privateKey string // ali private Key
)
func init() {
blockcount = 0
flag.BoolVar(&dontdownloadflag, "d", false, "Don't download")
flag.IntVar(&mp4FrameCut, "f", 0, "frame cut duration , 0 = off")
flag.StringVar(&fileName, "c", "", " path of your URLLIST")
flag.IntVar(&concurrent, "k", 1, "concurrent")
flag.IntVar(&timeout, "t", 15, "timeout ")
flag.StringVar(&privateKey, "K", "", "privateKey")
flag.Usage = usage
k := utils.Exist("blocklist")
d := utils.Exist("video_tmp")
if k != true {
file, err := os.Create("blocklist")
if err != nil {
fmt.Println(err)
}
file.Close()
}
if d != true {
os.Mkdir("./video_tmp", 0777)
}
}
func main() {
start := time.Now()
flag.Parse()
args := flag.Args()
if h {
flag.Usage()
return
}
if fileName != "" {
fileName := fileName
file, err := os.Open(fileName)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
defer file.Close()
ch = make(chan int, concurrent) /*创建通道(多线程)*/
fd := bufio.NewReader(file)
count = 0
for {
line, err := fd.ReadString('\n')
if err != nil {
if err == io.EOF { //读取结束,会报EOF
fmt.Println("Read done!")
break
}
break
}
line = strings.Replace(line, "\n", "", -1)
line = strings.Replace(line, "\r", "", -1)
urllist = append(urllist, line)
count++
}
fmt.Println("Line:", count)
for i := 0; i < count; i++ {
wg.Add(1)
str := "[" + utils.Bar((i*10)/count, 10) + "] "
fmt.Printf("\r%s %.1f %% exe: %d finished: %d/%d block: %d msg:", str, float32(i)/float32(count)*100, i, finished, count, blockcount)
ch <- 1
urlc := urllist[i]
if privateKey != "" {
testurl1 := urlc
urlpath := utils.GetPathInURL(testurl1)
NowTimestamp := time.Now().Unix()
EndTimestamp := NowTimestamp + 3600
sstring := urlpath + "-" + strconv.FormatInt(EndTimestamp, 10) + "-0-0-" + privateKey
md5str := utils.Md5V(sstring)
ssurl := urlc + "?auth_key=" + strconv.FormatInt(EndTimestamp, 10) + "-0-0-" + md5str
if mp4FrameCut == 0 {
go work(ssurl)
} else {
fmt.Println("mp4FrameCut")
}
} else {
if mp4FrameCut == 0 {
go work(urlc)
} else {
go mp4work(urlc)
}
}
}
failurlT = failurl
wg.Wait()
str := "[" + utils.Bar((10), 10) + "] "
fmt.Printf("\r%s %.1f %% exe: %d finished: %d/%d block: %d ", str, float32(count)/float32(count)*100, count, finished, count, blockcount)
fmt.Printf(utils.SuccessString("\r\nDone!"))
fmt.Println()
} else { // 无 -c 参数
if len(args) < 1 {
fmt.Println(utils.ErrorString("Too few arguments"))
fmt.Println("Usage: neute [args] URLs...")
flag.PrintDefaults()
} else if len(args) == 1 {
wg.Add(1)
if privateKey != "" {
testurl1 := flag.Arg(0)
urlpath := utils.GetPathInURL(testurl1)
NowTimestamp := time.Now().Unix()
EndTimestamp := NowTimestamp + 3600
sstring := urlpath + "-" + strconv.FormatInt(EndTimestamp, 10) + "-0-0-" + privateKey
md5str := utils.Md5V(sstring)
ssurl := flag.Arg(0) + "?auth_key=" + strconv.FormatInt(EndTimestamp, 10) + "-0-0-" + md5str
fmt.Println(utils.SuccessString(ssurl))
singlework(ssurl)
} else {
singlework(flag.Arg(0))
}
}
}
end := time.Now()
during := end.Sub(start)
fmt.Println(during)
}
func singlework(urlc string) {
defer wg.Done()
num := rand.Int31n(1)
time.Sleep(time.Duration(num) * time.Second)
filename, ext, err := utils.GetNameAndExt(urlc)
if err != nil {
fmt.Fprintf(os.Stderr, "log message: %s", err)
return
}
pathPre := "./video_tmp/"
path := pathPre + filename + "." + ext + ".tmp"
downloadPro(urlc, path)
fmt.Println()
}
func mp4work(urlc string) {
defer wg.Done()
_, err := mp4utils.GenerateLength("ffprobe", urlc)
if err != nil {
fmt.Println("bad")
fmt.Println(err)
}
<-ch
}
func work(urlc string) {
defer wg.Done()
num := rand.Int31n(1)
time.Sleep(time.Duration(num) * time.Second)
filename, ext, err := utils.GetNameAndExt(urlc)
if err != nil {
fmt.Fprintf(os.Stderr, "log message: #1%s", err)
return
}
pathPre := "./video_tmp/"
path := pathPre + filename + "." + ext + ".tmp"
downloadPro(urlc, path)
<-ch
}
func downloadPro(url string, path string) {
var (
retries = 3
resp *http.Response
)
chx := make(chan string)
go func() {
out, err := os.Create(path)
if err != nil {
fmt.Fprintf(os.Stderr, "log message: create path%s", err)
return
}
defer out.Close()
for retries > 0 {
resp, err = http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "log message: connect error retring %d \n", retries)
time.Sleep(3 * time.Second)
retries--
} else {
break
}
}
if resp != nil {
defer resp.Body.Close()
resCode := resp.StatusCode
if resCode == 200 {
contentLength := resp.ContentLength
if contentLength < 512 {
blockcount++
err := utils.AppendToFile("blocklist", url+"\n")
fmt.Fprintf(os.Stderr, "log message: #Too short %s", url[0:72])
if err != nil {
// fmt.Println("ERROR CODE: #4")
fmt.Fprintf(os.Stderr, "log message: #6 %s", err)
return
}
}
scontentLength := utils.ByteCountIEC(contentLength)
fmt.Fprintf(os.Stdout, utils.InfoString("ContentLength:%s"), scontentLength)
if !dontdownloadflag {
_, err = io.Copy(out, resp.Body)
if err != nil {
if err == io.ErrUnexpectedEOF { //读取结束,会报EOF
fmt.Fprintf(os.Stderr, "log message: #5 %s", url[0:72])
return
}
fmt.Fprintf(os.Stderr, "log message: #5 %s", err)
return
}
}
} else {
blockcount++
err := utils.AppendToFile("blocklist", url+"\n")
fmt.Fprintf(os.Stderr, "log message: #%d %s", resCode, url[0:72])
if err != nil {
// fmt.Println("ERROR CODE: #4")
fmt.Fprintf(os.Stderr, "log message: #6 %s", err)
return
}
}
} else {
failurl = append(failurl, url)
}
chx <- "**"
}()
select {
case res := <-chx:
fmt.Printf("%s", res)
case <-time.After(time.Second * time.Duration(timeout)):
finished++
}
}
func usage() {
fmt.Fprintf(os.Stderr, ` version: 1.0.1.1
Usage: neute [opts][args] URLs...
Options:
`)
flag.PrintDefaults()
fmt.Println(`
Examples:
neute -c YOURFILEPATH -k 5 -t 30
(URL list is "YOURFILEPATH" concurrent is 5 , download 30s! )
neute -d -c YOURFILEPATH -k 10
(Just cheack URL without download!)`)
}