@@ -16,7 +16,9 @@ import (
16
16
"github.com/qianlnk/pgbar"
17
17
)
18
18
19
- // Crawl posts from CSDN
19
+ // Crawl posts from csdn
20
+ // build posts to hexo style
21
+
20
22
const (
21
23
ListPostURL = "https://blog.csdn.net/%s/article/list/%d?"
22
24
PostDetailURL = "https://mp.csdn.net/mdeditor/getArticle?id=%s"
@@ -47,6 +49,7 @@ type PostDetail struct {
47
49
var (
48
50
username string
49
51
page int
52
+ cookie string
50
53
currentPage = 1
51
54
count int
52
55
wg sync.WaitGroup
55
58
56
59
func init () {
57
60
flag .StringVar (& username , "username" , "junmoxi" , "your csdn username" )
61
+ flag .StringVar (& cookie , "cookie" , "UserName=junmoxi; UserToken=c3c29cca48be43c4884fe36d052d5852;" , "your csdn cookie" )
58
62
flag .IntVar (& page , "page" , - 1 , "download pages" )
59
63
flag .Parse ()
60
64
}
@@ -64,6 +68,7 @@ func main() {
64
68
if err != nil {
65
69
panic (err )
66
70
}
71
+
67
72
bar = pgbar .NewBar (0 , "下载进度" , len (urls ))
68
73
for _ , url := range urls {
69
74
wg .Add (1 )
@@ -78,7 +83,6 @@ func crawlPosts(username string) ([]string, error) {
78
83
client := http.Client {}
79
84
var (
80
85
urls []string
81
- err error
82
86
)
83
87
84
88
for {
@@ -108,53 +112,55 @@ func crawlPosts(username string) ([]string, error) {
108
112
}
109
113
currentPage ++
110
114
}
111
-
112
- return urls , err
113
115
}
114
116
115
- func crawlPostMarkdown (url string ) ( * PostDetail , error ) {
117
+ func crawlPostMarkdown (url string ) {
116
118
index := strings .LastIndex (url , "/" )
117
119
id := url [index + 1 :]
118
120
119
121
client := http.Client {}
120
122
121
123
req , _ := http .NewRequest ("GET" , fmt .Sprintf (PostDetailURL , id ), nil )
122
- req .Header .Set ("cookie" , "UserName=junmoxi; UserToken=de709e85392f4b8a8d19d69eb2273c56;" )
124
+ req .Header .Set ("cookie" , cookie )
123
125
124
126
resp , err := client .Do (req )
125
127
if err != nil {
126
- return nil , err
128
+ return
127
129
}
128
130
129
131
data , err := ioutil .ReadAll (resp .Body )
130
132
if err != nil {
131
- return nil , err
133
+ return
132
134
}
133
135
134
136
post := new (DetailData )
135
137
err = json .Unmarshal (data , post )
136
138
if err != nil {
137
- return nil , err
139
+ return
138
140
}
139
141
140
- go buildPost ( post .Data )
141
-
142
- return nil , nil
142
+ if post .Data . Markdowncontent != "" {
143
+ go buildPost ( post . Data )
144
+ }
143
145
}
144
146
145
147
func buildPost (post PostDetail ) {
146
148
147
149
date := postTime .Format ("2006-01-02 15:03:04" )
148
150
header := fmt .Sprintf (HexoHeader , post .Title , date , post .Tags , post .Categories )
149
151
150
- ioutil .WriteFile (
152
+ err := ioutil .WriteFile (
151
153
fmt .Sprintf ("%s.md" , post .Title ),
152
154
[]byte (fmt .Sprintf ("%s\n %s" , header , post .Markdowncontent )),
153
155
os .ModePerm )
154
156
157
+ if err != nil {
158
+ return
159
+ }
160
+
155
161
rand .Seed (time .Now ().UnixNano ())
156
162
d := rand .Intn (3 ) + 1
157
- postTime = postTime .AddDate (0 , 0 , - d )
163
+ postTime = postTime .AddDate (0 , 0 , - d ). Add ( time . Hour )
158
164
159
165
count ++
160
166
0 commit comments