-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
118 lines (115 loc) · 3.86 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
package main
import (
"AfdianToMarkdown/afdian"
"AfdianToMarkdown/afdian/album"
"AfdianToMarkdown/afdian/motion"
"AfdianToMarkdown/utils"
"context"
"github.com/fatih/color"
"github.com/urfave/cli/v3"
"log"
"os"
"time"
)
var (
afdianHost string
authorName string
albumUrl string
cookieString, authToken string
)
func main() {
successColor := color.New(color.Bold, color.FgGreen).FprintlnFunc()
failColor := color.New(color.Bold, color.FgRed).FprintlnFunc()
//记录开始时间
startTime := time.Now()
cmd := &cli.Command{
Name: "AfdianToMarkdown",
Usage: "爱发电下载器,支持按作者或按作品集爬取数据\nGithub Link: https://github.com/PhiFever/AfdianToMarkdown",
UsageText: "eg:\n\tAfdianToMarkdown.exe motions -au Alice \n" +
"eg:\n\tAfdianToMarkdown.exe albums -au Alice \n" +
"eg:\n\tAfdianToMarkdown.exe update",
Version: "0.4.0",
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringFlag{Name: "host", Destination: &afdianHost, Value: "afdian.com", Usage: "主站域名,如访问不通可自行更改"},
},
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
// 在这里可以根据需要做全局参数的预处理
afdian.SetHostUrl(afdianHost)
cookieString, authToken = afdian.GetCookies()
return ctx, nil
},
After: func(ctx context.Context, cmd *cli.Command) error {
// 在这里可以根据需要做全局参数的后处理
//记录结束时间
endTime := time.Now()
//计算执行时间,单位为秒
successColor(os.Stdout, "处理完毕,共耗时:", utils.GetExecutionTime(startTime, endTime))
return nil
},
Commands: []*cli.Command{
{
Name: "motions",
Usage: "下载指定作者的所有动态",
Flags: []cli.Flag{
&cli.StringFlag{Name: "author", Aliases: []string{"au"}, Destination: &authorName, Value: "", Usage: "待下载的作者id"},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
return motion.GetMotions(authorName, cookieString, authToken)
},
},
//{
// Name: "album",
// Usage: "下载指定的作品集",
// Flags: []cli.Flag{
// &cli.StringFlag{Name: "url", Aliases: []string{"u"}, Destination: &albumUrl, Value: "", Usage: "待下载的作品集url"},
// },
// Action: func(ctx context.Context, cmd *cli.Command) error {
// re := regexp.MustCompile("^.*/album/")
// albumId := re.ReplaceAllString(albumUrl, "")
// albumPostList := afdian.GetAlbumPostList(albumId, cookieString)
// converter := md.NewConverter("", true, nil)
// return album.GetAlbum(authorName, cookieString, authToken)
// },
//},
{
Name: "albums",
Usage: "下载指定作者的所有作品集",
Flags: []cli.Flag{
&cli.StringFlag{Name: "author", Aliases: []string{"au"}, Destination: &authorName, Value: "", Usage: "待下载的作者id"},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
return album.GetAlbums(authorName, cookieString, authToken)
},
},
{
Name: "update",
Usage: "更新所有已经下载的作者的动态和作品集",
Action: func(ctx context.Context, cmd *cli.Command) error {
authors, err := utils.CheckAndListAuthors()
if err != nil {
return err
}
for _, author := range authors {
log.Println("find exist author: ", author)
if err := motion.GetMotions(author, cookieString, authToken); err != nil {
return err
}
if err := album.GetAlbums(author, cookieString, authToken); err != nil {
return err
}
}
return nil
},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
//如果没有传入任何参数,则显示帮助信息
cli.ShowAppHelp(cmd)
return nil
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
failColor(os.Stderr, err)
}
}