Skip to content

Commit 5a8bd41

Browse files
author
kw.lei
committed
add set cookie
1 parent b76c261 commit 5a8bd41

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
# 批量导出CSDN博客
2-
> 批量导出csnd博客,并转化为hexo博客样式
2+
> 批量导出`csnd`博客,并转化为`hexo`博客样式,只能导出`markdown`编写的博客
33
4-
## 使用
4+
# Quick start
5+
```bash
6+
go run main.go -username 你的csdn用户名 -cookie 你csdn的cookie -page 1
7+
```
8+
> page不写,默认为下载全部页
9+
10+
# Demo
511

612
```bash
7-
go run main.go -username 你的csdn用户名 -page 1
13+
go run main.go -username "junmoxi" -cookie "UserName=junmoxi; UserToken=c3c29cca48be43c4884fe36d052d5851"
814
```
9-
> page不写,默认为下载全部页
15+
> 如果想下载别人的文章,那么将`username`更换为别人的即可,cookie还是用你的
16+
17+
# cookie获取
18+
![](cookie.png)
19+
20+
# 关注
21+
> 如果对你有所帮助,请给个star,你的支持是我最大的动力,欢迎关注我微信公众号,一起学习Go语言
22+
23+
![](weixin.png)

cookie.png

117 KB
Loading

main.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2022
const (
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 {
4749
var (
4850
username string
4951
page int
52+
cookie string
5053
currentPage = 1
5154
count int
5255
wg sync.WaitGroup
@@ -55,6 +58,7 @@ var (
5558

5659
func 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

145147
func 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

weixin.png

31.5 KB
Loading

0 commit comments

Comments
 (0)