-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
113 lines (100 loc) · 2.31 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
package main
import (
"flag"
"geekpdf/geek"
log "github.com/sirupsen/logrus"
"os"
)
var (
cellphone string
password string
path string
cid int
)
func main() {
err := Init()
if err != nil {
log.WithError(err).Error("init failed")
return
}
g := geek.NewGeekTime(cellphone, password)
loginResp, err := g.Login()
if err != nil {
log.WithError(err).Error("Login failed")
return
}
log.WithFields(log.Fields{
"uid": loginResp.UID,
"cookies": g.Cookies(),
}).Info("Login success")
articleList, err := g.ArticleList(cid)
if err != nil {
log.WithError(err).Error("Loading article list failed")
return
}
log.WithFields(log.Fields{
"productId": cid,
"count": len(articleList),
}).Info("Loading article list success")
for _, article := range articleList {
article, err := g.Article(article.ID)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"articleId": article.ID,
"title": article.ArticleTitle,
}).Error("Loading article failed")
continue
}
log.WithFields(log.Fields{
"articleId": article.ID,
"title": article.ArticleTitle,
}).Info("Loading article success")
pdfPath := path + article.ArticleTitle + ".pdf"
err = geek.SaveArticleAsPdf(article.ArticleContent, pdfPath)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"title": article.ArticleTitle,
"filePath": pdfPath,
}).Error("Save pdf failed")
continue
}
log.WithFields(log.Fields{
"articleId": article.ID,
"title": article.ArticleTitle,
"filePath": pdfPath,
}).Info("Save pdf success")
}
}
func Init() (err error) {
initLog()
initCmd()
initApp()
return nil
}
func initLog() {
//log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}
func initCmd() {
flag.StringVar(&cellphone, "c", "", "Cellphone")
flag.StringVar(&password, "w", "", "Password")
flag.StringVar(&path, "p", "pdf/", "Path to store pdf")
flag.IntVar(&cid, "i", 0, "Product ID")
flag.Parse()
if cellphone == "" || password == "" {
log.Error("Invalid cellphone or password")
os.Exit(1)
}
}
func initApp() {
if _, err := os.Stat(path); err == nil {
return
}
err := os.Mkdir(path, 0755)
if err != nil {
log.WithError(err).Error("Can not init dir for saving pdf")
os.Exit(1)
}
}