@@ -16,7 +16,9 @@ import (
1616 "github.com/qianlnk/pgbar"
1717)
1818
19- // Crawl posts from CSDN
19+ // Crawl posts from csdn
20+ // build posts to hexo style
21+
2022const (
2123 ListPostURL = "https://blog.csdn.net/%s/article/list/%d?"
2224 PostDetailURL = "https://mp.csdn.net/mdeditor/getArticle?id=%s"
@@ -47,6 +49,7 @@ type PostDetail struct {
4749var (
4850 username string
4951 page int
52+ cookie string
5053 currentPage = 1
5154 count int
5255 wg sync.WaitGroup
5558
5659func init () {
5760 flag .StringVar (& username , "username" , "junmoxi" , "your csdn username" )
61+ flag .StringVar (& cookie , "cookie" , "UserName=junmoxi; UserToken=c3c29cca48be43c4884fe36d052d5852;" , "your csdn cookie" )
5862 flag .IntVar (& page , "page" , - 1 , "download pages" )
5963 flag .Parse ()
6064}
@@ -64,6 +68,7 @@ func main() {
6468 if err != nil {
6569 panic (err )
6670 }
71+
6772 bar = pgbar .NewBar (0 , "下载进度" , len (urls ))
6873 for _ , url := range urls {
6974 wg .Add (1 )
@@ -78,7 +83,6 @@ func crawlPosts(username string) ([]string, error) {
7883 client := http.Client {}
7984 var (
8085 urls []string
81- err error
8286 )
8387
8488 for {
@@ -108,53 +112,55 @@ func crawlPosts(username string) ([]string, error) {
108112 }
109113 currentPage ++
110114 }
111-
112- return urls , err
113115}
114116
115- func crawlPostMarkdown (url string ) ( * PostDetail , error ) {
117+ func crawlPostMarkdown (url string ) {
116118 index := strings .LastIndex (url , "/" )
117119 id := url [index + 1 :]
118120
119121 client := http.Client {}
120122
121123 req , _ := http .NewRequest ("GET" , fmt .Sprintf (PostDetailURL , id ), nil )
122- req .Header .Set ("cookie" , "UserName=junmoxi; UserToken=de709e85392f4b8a8d19d69eb2273c56;" )
124+ req .Header .Set ("cookie" , cookie )
123125
124126 resp , err := client .Do (req )
125127 if err != nil {
126- return nil , err
128+ return
127129 }
128130
129131 data , err := ioutil .ReadAll (resp .Body )
130132 if err != nil {
131- return nil , err
133+ return
132134 }
133135
134136 post := new (DetailData )
135137 err = json .Unmarshal (data , post )
136138 if err != nil {
137- return nil , err
139+ return
138140 }
139141
140- go buildPost ( post .Data )
141-
142- return nil , nil
142+ if post .Data . Markdowncontent != "" {
143+ go buildPost ( post . Data )
144+ }
143145}
144146
145147func buildPost (post PostDetail ) {
146148
147149 date := postTime .Format ("2006-01-02 15:03:04" )
148150 header := fmt .Sprintf (HexoHeader , post .Title , date , post .Tags , post .Categories )
149151
150- ioutil .WriteFile (
152+ err := ioutil .WriteFile (
151153 fmt .Sprintf ("%s.md" , post .Title ),
152154 []byte (fmt .Sprintf ("%s\n %s" , header , post .Markdowncontent )),
153155 os .ModePerm )
154156
157+ if err != nil {
158+ return
159+ }
160+
155161 rand .Seed (time .Now ().UnixNano ())
156162 d := rand .Intn (3 ) + 1
157- postTime = postTime .AddDate (0 , 0 , - d )
163+ postTime = postTime .AddDate (0 , 0 , - d ). Add ( time . Hour )
158164
159165 count ++
160166
0 commit comments