-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
main.go
484 lines (435 loc) · 12.8 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
package main
import (
"fmt"
"github.com/gobs/args"
"github.com/iikira/BaiduPCS-Go/command"
"github.com/iikira/BaiduPCS-Go/config"
"github.com/kardianos/osext"
"github.com/peterh/liner"
"github.com/urfave/cli"
"io"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
)
var (
historyFile = "pcs_command_history.txt"
reloadFn = func(c *cli.Context) error {
baidupcscmd.ReloadIfInConsole()
return nil
}
)
func init() {
// change work directory
folderPath, err := osext.ExecutableFolder()
if err != nil {
folderPath, err = filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
folderPath = filepath.Dir(os.Args[0])
}
}
os.Chdir(folderPath)
pcsconfig.Init()
}
func main() {
app := cli.NewApp()
app.Name = "BaiduPCS-Go"
app.Version = "beta-v2"
app.Author = "iikira/BaiduPCS-Go: https://github.com/iikira/BaiduPCS-Go"
app.Usage = "百度网盘工具箱 for " + runtime.GOOS + "/" + runtime.GOARCH
app.Description = `BaiduPCS-Go 使用 Go语言编写, 为操作百度网盘, 提供实用功能.
具体功能, 参见 COMMANDS 列表
特色:
网盘内列出文件和目录, 支持通配符匹配路径;
下载网盘内文件, 支持网盘内目录 (文件夹) 下载, 支持多个文件或目录下载, 支持断点续传和高并发高速下载.
程序目前处于测试版, 后续会添加更多的实用功能.`
app.Action = func(c *cli.Context) {
if c.NArg() == 0 {
cli.ShowAppHelp(c)
line := newLiner()
defer closeLiner(line)
for {
if commandLine, err := line.Prompt("BaiduPCS-Go > "); err == nil {
line.AppendHistory(commandLine)
cmdArgs := args.GetArgs(commandLine)
if len(cmdArgs) == 0 {
continue
}
s := []string{os.Args[0]}
s = append(s, cmdArgs...)
closeLiner(line)
c.App.Run(s)
line = newLiner()
} else if err == liner.ErrPromptAborted || err == io.EOF {
break
} else {
log.Print("Error reading line: ", err)
continue
}
}
} else {
fmt.Printf("未找到命令: %s\n运行命令 %s help 获取帮助\n", c.Args().Get(0), app.Name)
}
}
app.Commands = []cli.Command{
{
Name: "login",
Usage: "使用百度BDUSS登录百度账号",
Description: fmt.Sprintf("\n 示例: \n\n %s\n\n %s\n\n %s\n\n %s\n\n %s\n",
filepath.Base(os.Args[0])+" login --bduss=123456789",
filepath.Base(os.Args[0])+" login",
"百度BDUSS获取方法: ",
"参考这篇 Wiki: https://github.com/iikira/BaiduPCS-Go/wiki/关于-获取百度-BDUSS",
"或者百度搜索: 获取百度BDUSS",
),
Category: "百度帐号操作",
Before: reloadFn,
After: reloadFn,
Action: func(c *cli.Context) error {
bduss := ""
if c.IsSet("bduss") {
bduss = c.String("bduss")
} else if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
line := liner.NewLiner()
line.SetCtrlCAborts(true)
defer line.Close()
bduss, _ = line.Prompt("请输入百度BDUSS值, 回车键提交 > ")
} else {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
username, err := pcsconfig.Config.SetBDUSS(bduss)
if err != nil {
fmt.Println(err)
return nil
}
fmt.Println("百度帐号登录成功:", username)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "bduss",
Usage: "百度BDUSS",
},
},
},
{
Name: "chuser",
Usage: "切换已登录的百度帐号",
Description: fmt.Sprintf("%s\n 示例:\n\n %s\n %s\n",
"如果运行该条命令没有提供参数, 程序将会列出所有的百度帐号, 供选择切换",
filepath.Base(os.Args[0])+" chuser --uid=123456789",
filepath.Base(os.Args[0])+" chuser",
),
Category: "百度帐号操作",
Before: reloadFn,
After: reloadFn,
Action: func(c *cli.Context) error {
if len(pcsconfig.Config.BaiduUserList) == 0 {
fmt.Println("未设置任何百度帐号, 不能切换")
return nil
}
var uid uint64
if c.IsSet("uid") {
if pcsconfig.Config.CheckUIDExist(c.Uint64("uid")) {
uid = c.Uint64("uid")
} else {
fmt.Println("切换用户失败, uid 不存在")
}
} else if c.NArg() == 0 {
cli.HandleAction(app.Command("loglist").Action, c)
line := liner.NewLiner()
line.SetCtrlCAborts(true)
defer line.Close()
nLine, _ := line.Prompt("请输入要切换帐号的 index 值 > ")
if n, err := strconv.Atoi(nLine); err == nil && n >= 0 && n < len(pcsconfig.Config.BaiduUserList) {
uid = pcsconfig.Config.BaiduUserList[n].UID
} else {
fmt.Println("切换用户失败, 请检查 index 值是否正确")
}
} else {
cli.ShowCommandHelp(c, c.Command.Name)
}
if uid == 0 {
return nil
}
pcsconfig.Config.BaiduActiveUID = uid
if err := pcsconfig.Config.Save(); err != nil {
fmt.Println(err)
return nil
}
fmt.Printf("切换用户成功, %v\n", pcsconfig.ActiveBaiduUser.Name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "uid",
Usage: "百度帐号 uid 值",
},
},
},
{
Name: "logout",
Usage: "退出已登录的百度帐号",
Description: fmt.Sprintf("%s\n 示例:\n\n %s\n %s\n",
"如果运行该条命令没有提供参数, 程序将会列出所有的百度帐号, 供选择退出",
filepath.Base(os.Args[0])+" logout --uid=123456789",
filepath.Base(os.Args[0])+" logout",
),
Category: "百度帐号操作",
Before: reloadFn,
After: reloadFn,
Action: func(c *cli.Context) error {
if len(pcsconfig.Config.BaiduUserList) == 0 {
fmt.Println("未设置任何百度帐号, 不能退出")
return nil
}
var uid uint64
if c.IsSet("uid") {
if pcsconfig.Config.CheckUIDExist(c.Uint64("uid")) {
uid = c.Uint64("uid")
} else {
fmt.Println("退出用户失败, uid 不存在")
}
} else if c.NArg() == 0 {
cli.HandleAction(app.Command("loglist").Action, c)
line := liner.NewLiner()
line.SetCtrlCAborts(true)
defer line.Close()
nLine, _ := line.Prompt("请输入要退出帐号的 index 值 > ")
if n, err := strconv.Atoi(nLine); err == nil && n >= 0 && n < len(pcsconfig.Config.BaiduUserList) {
uid = pcsconfig.Config.BaiduUserList[n].UID
} else {
fmt.Println("退出用户失败, 请检查 index 值是否正确")
}
} else {
cli.ShowCommandHelp(c, c.Command.Name)
}
if uid == 0 {
return nil
}
// 删除之前先获取被删除的数据, 用于下文输出日志
baidu, err := pcsconfig.Config.GetBaiduUserByUID(uid)
if err != nil {
fmt.Println(err)
return nil
}
if !pcsconfig.Config.DeleteBaiduUserByUID(uid) {
fmt.Printf("退出用户失败, %s\n", baidu.Name)
}
fmt.Printf("退出用户成功, %v\n", baidu.Name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "uid",
Usage: "百度帐号 uid 值",
},
},
},
{
Name: "loglist",
Usage: "获取当前帐号, 和所有已登录的百度帐号",
Category: "百度帐号操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
fmt.Printf("\n当前帐号 uid: %d, 用户名: %s\n", pcsconfig.ActiveBaiduUser.UID, pcsconfig.ActiveBaiduUser.Name)
fmt.Println(pcsconfig.Config.GetAllBaiduUser())
return nil
},
},
{
Name: "quota",
Usage: "获取配额, 即获取网盘总空间, 和已使用空间",
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
baidupcscmd.RunGetQuota()
return nil
},
},
{
Name: "cd",
Usage: "切换工作目录",
UsageText: fmt.Sprintf("%s cd <目录 绝对路径或相对路径>", filepath.Base(os.Args[0])),
Category: "网盘操作",
Before: reloadFn,
After: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
baidupcscmd.RunChangeDirectory(c.Args().Get(0))
return nil
},
},
{
Name: "ls",
Aliases: []string{"l", "ll"},
Usage: "列出当前工作目录内的文件和目录 或 指定目录内的文件和目录",
UsageText: fmt.Sprintf("%s ls <目录 绝对路径或相对路径>", filepath.Base(os.Args[0])),
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
baidupcscmd.RunLs(c.Args().Get(0))
return nil
},
},
{
Name: "pwd",
Usage: "输出当前所在目录 (工作目录)",
UsageText: fmt.Sprintf("%s pwd", filepath.Base(os.Args[0])),
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
fmt.Println(pcsconfig.ActiveBaiduUser.Workdir)
return nil
},
},
{
Name: "meta",
Usage: "获取单个文件/目录的元信息 (详细信息)",
UsageText: fmt.Sprintf("%s meta <文件/目录 绝对路径或相对路径>", filepath.Base(os.Args[0])),
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
baidupcscmd.RunGetMeta(c.Args().Get(0))
return nil
},
},
{
Name: "rm",
Usage: "删除 单个/多个 文件/目录",
UsageText: fmt.Sprintf("%s rm <网盘文件或目录的路径1> <文件或目录2> <文件或目录3> ...", filepath.Base(os.Args[0])),
Description: fmt.Sprintf("\n %s\n",
"注意: 删除多个文件和目录时, 请确保每一个文件和目录都存在, 否则删除操作会失败.",
),
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
var dargs []string
for i := 0; c.Args().Get(i) != ""; i++ {
dargs = append(dargs, c.Args().Get(i))
}
baidupcscmd.RunRemove(dargs...)
return nil
},
},
{
Name: "mkdir",
Usage: "创建目录",
UsageText: fmt.Sprintf("%s mkdir <目录 绝对路径或相对路径> ...", filepath.Base(os.Args[0])),
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
baidupcscmd.RunMkdir(c.Args().Get(0))
return nil
},
},
{
Name: "download",
Aliases: []string{"d"},
Usage: "下载文件或目录",
UsageText: fmt.Sprintf("%s download <网盘文件或目录的路径1> <文件或目录2> <文件或目录3> ...", filepath.Base(os.Args[0])),
Description: "下载的文件将会保存到, 程序所在目录的 download/ 目录 (文件夹).\n 已支持目录下载.\n 已支持多个文件或目录下载.\n 自动跳过下载重名的文件! \n",
Category: "网盘操作",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
var dargs []string
for i := 0; c.Args().Get(i) != ""; i++ {
dargs = append(dargs, c.Args().Get(i))
}
baidupcscmd.RunDownload(dargs...)
return nil
},
},
{
Name: "set",
Usage: "设置配置",
UsageText: fmt.Sprintf("%s set OptionName Value", filepath.Base(os.Args[0])),
Description: `
可设置的值:
OptionName Value
------------------
max_parallel 下载最大线程 (并发量) - 建议值 ( 100 ~ 500 )
例子:
set max_parallel 250
`,
Category: "配置",
Before: reloadFn,
After: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
switch c.Args().Get(0) {
case "max_parallel":
parallel, err := strconv.Atoi(c.Args().Get(1))
if err != nil {
return cli.NewExitError(fmt.Errorf("max_parallel 设置值不合法, 错误: %s", err), 1)
}
pcsconfig.Config.MaxParallel = parallel
err = pcsconfig.Config.Save()
if err != nil {
fmt.Println("设置失败, 错误:", err)
return nil
}
fmt.Printf("设置成功, %s -> %v\n", c.Args().Get(0), c.Args().Get(1))
default:
fmt.Printf("未知设定值: %s\n\n", c.Args().Get(0))
cli.ShowCommandHelp(c, "set")
}
return nil
},
},
{
Name: "quit",
Aliases: []string{"exit"},
Usage: "退出程序",
Action: func(c *cli.Context) error {
return cli.NewExitError("", 0)
},
Hidden: true,
HideHelp: true,
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}
func newLiner() *liner.State {
line := liner.NewLiner()
line.SetCtrlCAborts(true)
if f, err := os.Open(historyFile); err == nil {
line.ReadHistory(f)
f.Close()
}
return line
}
func closeLiner(line *liner.State) {
if f, err := os.Create(historyFile); err != nil {
log.Print("Error writing history file: ", err)
} else {
line.WriteHistory(f)
f.Close()
}
line.Close()
}