-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
549 lines (530 loc) · 14.4 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"gitee.com/rock_rabbit/goannie/binary"
pf "gitee.com/rock_rabbit/goannie/platforms"
"github.com/fatih/color"
"github.com/garyburd/redigo/redis"
)
var goannieVersion = "v0.0.24"
var goannieUpdateTime = "2020-12-18"
var goannieTitle = `
__
__ ___ __ ___ ___ /\_\ __
/'_ ` + "`" + `\ / __` + "`" + `\ /'__` + "`" + `\ /' _ ` + "`" + `\ /' _ ` + "`" + `\/\ \ /'__` + "`" + `\
/\ \L\ \/\ \L\ \/\ \L\.\_/\ \/\ \/\ \/\ \ \ \/\ __/
\ \____ \ \____/\ \__/.\_\ \_\ \_\ \_\ \_\ \_\ \____\
\/___L\ \/___/ \/__/\/_/\/_/\/_/\/_/\/_/\/_/\/____/
/\____/
\_/__/`
// URLRegexp 平台子任务结构体
type URLRegexp struct {
Name string // 匹配名称
Info string // 简介
URLRegexps []*regexp.Regexp // URL匹配表
Run func(pf.RunType, map[string]string) error // 执行任务
}
// Platform 平台结构体
type Platform struct {
Name string // 平台名称
URLRegexps []URLRegexp // URL匹配表
CookieFile string // Cookie文件路径
DefaultCookie string // 默认Cookie
}
// isURL 匹配url
func (pf Platform) isURL(url string) (bool, URLRegexp) {
for _, item := range pf.URLRegexps {
for _, must := range item.URLRegexps {
if must.MatchString(url) {
return true, item
}
}
}
return false, URLRegexp{}
}
// printInfo 打印信息
func (pf Platform) printInfo() {
color.Set(color.FgBlue, color.Bold)
fmt.Printf("|-----------------\t ")
color.Unset()
color.Set(color.FgHiMagenta, color.Bold)
fmt.Printf(pf.Name)
color.Unset()
color.Set(color.FgBlue, color.Bold)
fmt.Printf(" \t-----------------|\n")
color.Unset()
for _, item := range pf.URLRegexps {
color.Set(color.FgBlue, color.Bold)
fmt.Printf("task: ")
color.Unset()
fmt.Printf(item.Name)
color.Set(color.FgBlue, color.Bold)
fmt.Printf("\tinfo: ")
color.Unset()
fmt.Printf(item.Info + "\n")
}
}
var platformList []Platform
func init() {
// 初始化支持平台
platformList = []Platform{
{
"优酷视频",
[]URLRegexp{
{
"one",
"单视频 https://v.youku.com/v_show/id_XNDg2MTM3MjMyMA==.html",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://v\.youku\.com/v_show/.*?\.html`),
},
pf.RunYkOne,
}, {
"userList",
"作者视频 http://i.youku.com/i/UNjMwMTY2MDUyMA==",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://i\.youku\.com/i/.*?`),
},
pf.RunYkUserlist,
},
},
"douyin.txt",
"",
}, {
"抖音视频",
[]URLRegexp{
{
"one",
"单视频 https://www.iesdouyin.com/share/video/6877354382132808971",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.iesdouyin\.com/share/video/\d+`),
},
pf.RunDyOne,
}, {
"userList",
"作者视频 https://www.iesdouyin.com/share/user/2836383897749943?sec_uid=xxxxx",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.iesdouyin\.com/share/user/\d+\?.*?sec_uid=.*?`),
},
pf.RunDyUserList,
}, {
"shortURL",
"短链接 https://v.douyin.com/JDq8uv7/",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://v\.douyin\.com/.*?`),
},
pf.RunDyShortURL,
},
},
"douyin.txt",
"",
}, {
"腾讯视频",
[]URLRegexp{
{
"one",
"单视频 https://v.qq.com/x/cover/mzc00200agq0com/r31376lllyf.html",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://v\.qq\.com/x/cover/.*?/.*?\.html.*?$`),
regexp.MustCompile(`^(http|https)://v\.qq\.com/x/cover/.*?\.html.*?$`),
regexp.MustCompile(`^(http|https)://v\.qq\.com/x/page/.*?/.*?\.html.*?$`),
regexp.MustCompile(`^(http|https)://v\.qq\.com/x/page/.*?\.html.*?$`),
},
pf.RunTxOne,
},
{
"detail",
"腾讯剧集页 https://v.qq.com/detail/5/52852.html",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://v\.qq\.com/detail/.*?/.*?\.html.*?$`),
},
pf.RunTxDetail,
}, {
"userList",
"作者视频 https://v.qq.com/s/videoplus/1790091432",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://v\.qq\.com/biu/videoplus\?vuid=\d+.*?$`),
regexp.MustCompile(`^(http|https)://v\.qq\.com/s/videoplus/\d+.*?$`),
regexp.MustCompile(`^(http|https)://v\.qq\.com/x/bu/h5_user_center\?vuid=\d+.*?$`),
},
pf.RunTxUserList,
}, {
"lookList",
"看作者作品列表 look https://v.qq.com/s/videoplus/1790091432",
[]*regexp.Regexp{
regexp.MustCompile(`^look (http|https)://v\.qq\.com/biu/videoplus\?vuid=\d+.*?$`),
regexp.MustCompile(`^look (http|https)://v\.qq\.com/s/videoplus/\d+.*?$`),
regexp.MustCompile(`^look (http|https)://v\.qq\.com/x/bu/h5_user_center\?vuid=\d+.*?$`),
},
pf.RunLookTxUserList,
},
},
"tengxun.txt",
"",
}, {
"火锅视频",
[]URLRegexp{
{
"userList",
"作者视频 https://huoguo.qq.com/m/person.html?userid=18590596",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://huoguo\.qq\.com/m/person\.html\?userid=\d+.*?$`),
},
pf.RunHgUserList,
}, {
"lookList",
"看作者作品列表 look https://huoguo.qq.com/m/person.html?userid=18590596",
[]*regexp.Regexp{
regexp.MustCompile(`^look (http|https)://huoguo\.qq\.com/m/person\.html\?userid=\d+.*?$`),
},
pf.RunLookHgUserList,
},
},
"tengxun.txt",
"",
},
{
"爱奇艺视频",
[]URLRegexp{
{
"one",
"单视频 https://www.iqiyi.com/v_1fr4mggxzpo.html",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.iqiyi\.com/v_\w+\.html.*?$`),
},
pf.RunIqyOne,
},
{
"userList",
"作者视频 https://www.iqiyi.com/u/2182689830",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.iqiyi\.com/u/.*?`),
},
pf.RunIqyUserList,
},
{
"detail",
"爱奇艺剧集页 https://www.iqiyi.com/a_19rrht2ok5.html",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.iqiyi\.com/a_\w+\.html.*?$`),
},
pf.RunIqyDetail,
},
},
"iqiyi.txt",
"",
},
{
"西瓜视频",
[]URLRegexp{
{
"one",
"单视频 https://www.ixigua.com/6832194590221533707",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.ixigua\.com/\d+.*?$`),
regexp.MustCompile(`^(http|https)://m\.ixigua\.com/\d+.*?$`),
regexp.MustCompile(`^(http|https)://m\.ixigua\.com/video/\d+.*?$`),
regexp.MustCompile(`^(http|https)://www\.ixigua\.com/.*?\?id=\d+.*?$`),
regexp.MustCompile(`^(http|https)://toutiao\.com/group/\d+/.*?$`),
},
pf.XgNewOne,
}, {
"userList",
"TA的视频 https://www.ixigua.com/home/85383446500/video/",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.ixigua\.com/home/\d+/video.*?$`),
regexp.MustCompile(`^(http|https)://www\.ixigua\.com/home/\d+($|/$|/\?.*?$|\?.*?$)`),
},
pf.RunXgUserList,
}, {
"lookList",
"看作者作品列表 look https://www.ixigua.com/home/85383446500/video/",
[]*regexp.Regexp{
regexp.MustCompile(`^look (http|https)://www\.ixigua\.com/home/\d+/video.*?$`),
regexp.MustCompile(`^look (http|https)://www\.ixigua\.com/home/\d+/($|\?.*?$)`),
},
pf.RunLookXgUserList,
},
},
"xigua.txt",
"",
},
{
"好看视频",
[]URLRegexp{
{
"one",
"单视频 https://haokan.baidu.com/v?vid=3881011031260239591",
[]*regexp.Regexp{
regexp.MustCompile(`(http|https)://haokan\.baidu\.com/v\?vid=\d+`),
},
pf.RunHkOne,
}, {
"userList",
"作者视频 https://haokan.baidu.com/author/1649278643844524",
[]*regexp.Regexp{
regexp.MustCompile(`(http|https)://haokan\.baidu\.com/author/\d+`),
},
pf.RunHkUserList,
},
},
"haokan.txt",
"",
},
{
"哔哩哔哩",
[]URLRegexp{
{
"one",
"单视频 https://www.bilibili.com/video/BV1iK4y1e7uL",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://www\.bilibili\.com/video/\w+.*?$`),
regexp.MustCompile(`^(http|https)://www\.bilibili\.com/bangumi/play/\w+.*?$`),
},
pf.RunBliOne,
},
{
"userList",
"TA的视频 https://space.bilibili.com/337312411",
[]*regexp.Regexp{
regexp.MustCompile(`^(http|https)://space\.bilibili\.com/\d+.*?$`),
},
pf.RunBliUserList,
},
},
"bilibili.txt",
"",
},
}
}
func main() {
// 设置环境变量
if err := pf.SetGoannieEnv(); err != nil {
printErrInfo(err.Error())
exitInfo()
}
// 检查二进制文件更新
if err := binary.Update(); err != nil {
printErrInfo(err.Error())
}
// 检查Data目录
if isDataPath, _ := isDir(pf.AppDataPath); !isDataPath {
if err := os.MkdirAll(pf.AppDataPath, os.ModePerm); err != nil {
printErrInfo(err.Error())
exitInfo()
}
}
// 启动 redis
cmd := exec.Command("redis-server", pf.RedisConfFile, "--dir", pf.AppDataPath)
_ = cmd.Start()
// 连接 redis
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
printErrInfo(err.Error())
exitInfo()
}
defer conn.Close()
printHello(conn)
// v0.0.09 版本兼容
pf.TOLeadRedis(conn)
GETSAVEPATH:
var savePath string
err = getInput("请输入保存路径", &savePath)
if err != nil {
printErrInfo(err.Error())
goto GETSAVEPATH
}
isSavePath, err := isDir(savePath)
if !isSavePath {
if err != nil {
printErrInfo(err.Error())
goto GETSAVEPATH
} else {
printErrInfo("文件夹不存在")
goto GETSAVEPATH
}
}
// 是否过滤重复下载?
var isDeWeight string
isDeWeightBool := true
err = getInput("是否过滤重复下载? yes or no (yes)", &isDeWeight)
if isDeWeight == "no" {
isDeWeightBool = false
}
GETURL:
var url string
err = getInput("请输入URL", &url)
if err != nil {
printErrInfo(err.Error())
goto GETURL
}
// 首先判断是否txt文件
if filepath.Ext(url) == ".txt" {
f, err := os.Open(url)
if err != nil {
printErrInfo(err.Error())
goto GETURL
}
data, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
printErrInfo(err.Error())
goto GETURL
}
matched := regexp.MustCompile("(?s)(http|https):\\/\\/([\\w\\-]+(\\.[\\w\\-]+)*\\/)*[\\w\\-]+(\\.[\\w\\-]+)*\\/?(\\?([\\w\\-\\.,@?^=%&:\\/~\\+#]*)+)?")
urlList := matched.FindAllString(string(data), -1)
pf.PrintInfo(fmt.Sprintf("读取到 %d 个URL", len(urlList)))
for _, item := range urlList {
url := item
platform, subtask, err := getURLPlatform(url)
if err != nil {
continue
}
color.Set(color.FgBlue, color.Bold)
fmt.Printf("平台:%s 子任务:%s\n", platform.Name, subtask.Name)
color.Unset()
currenpath, _ := getCurrentPath()
runType := pf.RunType{
URL: url,
SavePath: savePath,
CookieFile: fmt.Sprintf("%s%s", currenpath, platform.CookieFile),
DefaultCookie: platform.DefaultCookie,
IsDeWeight: isDeWeightBool,
RedisConn: conn,
}
err = subtask.Run(runType, map[string]string{})
if err != nil {
printErrInfo(err.Error())
continue
}
}
goto GETURL
}
platform, subtask, err := getURLPlatform(url)
if err != nil {
// 尝试直接使用 annie
err = pf.AnnieDownload(url, savePath, "", "")
if err != nil {
printErrInfo(err.Error())
}
goto GETURL
}
color.Set(color.FgBlue, color.Bold)
fmt.Printf("平台:%s 子任务:%s\n", platform.Name, subtask.Name)
color.Unset()
currenpath, _ := getCurrentPath()
runType := pf.RunType{
URL: url,
SavePath: savePath,
CookieFile: fmt.Sprintf("%s%s", currenpath, platform.CookieFile),
DefaultCookie: platform.DefaultCookie,
IsDeWeight: isDeWeightBool,
RedisConn: conn,
}
err = subtask.Run(runType, map[string]string{})
if err != nil {
printErrInfo(err.Error())
goto GETURL
}
goto GETURL
}
// getURLPlatform 获取url平台
func getURLPlatform(url string) (Platform, URLRegexp, error) {
for _, item := range platformList {
isURL, subtask := item.isURL(url)
if isURL {
return item, subtask, nil
}
}
return Platform{}, URLRegexp{}, errors.New("不支持这个链接")
}
// isDir 文件夹是否存在
func isDir(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
// getInput 获取控制台输入
func getInput(info string, outStr *string) error {
color.Set(color.FgGreen, color.Bold)
fmt.Printf("$ %s:", info)
color.Unset()
reader := bufio.NewReader(os.Stdin)
data, _, err := reader.ReadLine()
if err != nil {
return err
}
*outStr = strings.Replace(string(data), "\n", "", -1)
return nil
}
// printHello 打印欢迎语
func printHello(conn redis.Conn) {
color.Set(color.FgGreen, color.Bold)
defer color.Unset()
fmt.Println(goannieTitle)
color.Set(color.FgMagenta, color.Bold)
fmt.Printf(" version: %s updateTime: %s\n\n", goannieVersion, goannieUpdateTime)
color.Set(color.FgHiBlue, color.Bold)
fmt.Println("支持平台")
for _, item := range platformList {
item.printInfo()
color.Set(color.FgHiBlack, color.Bold)
fmt.Printf("cookie 设置:在goannie.exe同级目录中新建 %s 写入name=value;name=value....格式即可。\n", item.CookieFile)
if item.Name == "优酷视频" {
fmt.Println("ccode 和 ckey 设置:在goannie.exe同级目录中新建 ccode.txt 和 ckey.txt 写入其中即可。")
}
color.Unset()
fmt.Println("")
}
color.Set(color.FgHiBlue, color.Bold)
fmt.Println("下载统计")
color.Unset()
pf.PrintVideoIDCount(conn)
fmt.Println("")
}
// printErrInfo 打印错误信息
func printErrInfo(errInfo string) {
color.Set(color.FgRed, color.Bold)
defer color.Unset()
fmt.Println("错误信息:" + errInfo)
}
// exitInfo 结束程序
func exitInfo() {
color.Set(color.FgGreen, color.Bold)
defer color.Unset()
fmt.Printf("$ 回车退出:")
var s string
_, _ = fmt.Scanln(&s)
os.Exit(1)
}
// getCurrentPath 获取程序所在目录
func getCurrentPath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
path, err := filepath.Abs(file)
if err != nil {
return "", err
}
i := strings.LastIndex(path, "/")
if i < 0 {
i = strings.LastIndex(path, "\\")
}
if i < 0 {
return "", errors.New(`error: Can't find "/" or "\". `)
}
return string(path[0 : i+1]), nil
}