Skip to content

Commit 9f2143c

Browse files
author
Martin
committed
fix get cookie; fix cnblogs
1 parent e0e37d2 commit 9f2143c

2 files changed

Lines changed: 60 additions & 34 deletions

File tree

src/blog/cnblogs.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ const jsdom = require("jsdom");
55
const querystring = require('querystring');
66
const FormData = require('form-data');
77
const fs = require('fs');
8-
const { Console } = require('console');
8+
const {Console} = require('console');
99
const axios = require('axios')
10-
const { session } = require('electron')
10+
const {session} = require('electron')
11+
1112
//上传图片到博客园
1213
function uploadPictureToCnBlogs(filePath) {
1314
return new Promise((resolve, reject) => {
@@ -29,8 +30,8 @@ function uploadPictureToCnBlogs(filePath) {
2930
}, function (res) {
3031
let str = '';
3132
res.on('data', function (buffer) {
32-
str += buffer;//用字符串拼接
33-
}
33+
str += buffer;//用字符串拼接
34+
}
3435
);
3536
res.on('end', () => {
3637
if (res.statusCode === 200) {
@@ -80,35 +81,68 @@ function publishArticleToCnBlogs(title, content, isPublish) {
8081
}
8182

8283
function publishArticleToCnBlogFact(title, content, resolve,
83-
reject, isPublish) {
84-
let params = {
84+
reject, isPublish) {
85+
const params = {
8586
"id": null,
8687
"postType": 1,
88+
"accessPermission": 0,
8789
"title": title,
90+
"url": null,
8891
"postBody": content,
92+
"categoryIds": null,
93+
"inSiteCandidate": false,
94+
"inSiteHome": false,
95+
"siteCategoryId": null,
96+
"blogTeamIds": null,
97+
"isPublished": true,
98+
"displayOnHomePage": true,
99+
"isAllowComments": true,
100+
"includeInMainSyndication": true,
101+
"isPinned": false,
102+
"isOnlyForRegisterUser": false,
103+
"isUpdateDateAdded": false,
104+
"entryName": null,
105+
"description": null,
106+
"featuredImage": null,
107+
"tags": null,
108+
"password": null,
109+
"datePublished": new Date(),
110+
"isMarkdown": true,
111+
"isDraft": true,
112+
"autoDesc": null,
113+
"changePostType": false,
114+
"blogId": 0,
115+
"author": null,
116+
"removeScript": false,
117+
"clientInfo": null,
118+
"changeCreatedTime": false,
119+
"canChangeCreatedTime": false,
120+
"isContributeToImpressiveBugActivity": false
89121
}
90122
axios({
91123
method: 'post',
92124
url: 'https://i.cnblogs.com/api/posts',
93125
data: JSON.stringify(params),
94126
headers: {
95-
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
96-
'Referer': 'https://i.cnblogs.com',
97-
'Accept': '*/*',
98-
'Origin': 'https://i.cnblogs.com',
99-
'Content-Type': 'application/json',
100-
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
101-
'Cookie': dataStore.getCnBlogCookies(),
127+
"accept-language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
128+
'referer': 'https://i.cnblogs.com',
129+
'accept': '*/*',
130+
'origin': 'https://i.cnblogs.com',
131+
'content-type': 'application/json',
132+
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
133+
'cookie': dataStore.getCnBlogCookies(),
102134
'x-xsrf-token': dataStore.GetCnblogsToken()
103135
}
104136
}).then(res => {
105-
if (res.status == 200) {
137+
if (res.status === 200) {
106138
resolve(res.data.url)
107139
}
108140
// console.log(res)
109-
}).catch(res => {
110-
reject('发布失败!\n1.文章标题已存在\n2.尚未登录博客园\n3.发布频繁请稍后再试\n4.超过当日100篇限制')
111-
141+
}).catch(reason => {
142+
reject('发布失败!\n' + JSON.stringify(reason.response.data))
143+
console.log("cookie: " + dataStore.getCnBlogCookies())
144+
console.log('x-xsrf-token: ' + dataStore.GetCnblogsToken())
145+
console.log(reason.response.data)
112146
})
113147

114148

src/cookie/app-login.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { BrowserWindow, session } = require('electron');
1+
const {BrowserWindow, session} = require('electron');
22
const https = require('https');
33
const jsDom = require("jsdom");
44
const icon = require('../common/app-icon');
@@ -8,33 +8,25 @@ const dataStore = new DataStore();
88
//登录某网站获取Cookie通用方法
99
function getSiteCookie(url, callback) {
1010
let win = new BrowserWindow(
11-
{ width: 700, height: 600, icon: icon.iconFile, title: '【登陆成功后关闭窗口即可完成设置】' });
11+
{width: 700, height: 600, icon: icon.iconFile, title: '【登陆成功后关闭窗口即可完成设置】'});
1212
win.loadURL(url).then();
1313
win.on('close', () => {
1414
// 查询所有与设置的 URL 相关的所有 cookies.
15-
if (url == 'https://www.cnblogs.com/') {
16-
url = '.cnblogs.com'
17-
} else if (url == 'https://blog.csdn.net/') {
18-
url = '.csdn.net'
19-
} else if (url.indexOf('jianshu') != -1) {
20-
url = '.jianshu.com'
21-
} else if (url.indexOf('oschina') != -1) {
22-
url = '.oschina.net'
23-
}
24-
session.defaultSession.cookies.get({ domain: url })
15+
session.defaultSession.cookies.get({url: url})
2516
.then((cookies) => {
2617
let cookieString = '';
2718
for (let cookie of cookies) {
28-
if (cookie.name == 'XSRF-TOKEN') {
19+
if (cookie.name === 'XSRF-TOKEN') {
2920
dataStore.SetCnblogsToken(cookie.value)
3021
}
3122
cookieString += cookie.name + '=' + cookie.value + '; '
3223
}
24+
console.log("cookie: " + cookieString.trim())
3325
callback(cookieString.trim())
34-
}).catch((error) => {
26+
})
27+
.catch((error) => {
3528
console.log(error)
36-
});
37-
29+
})
3830
win = null
3931
});
4032
win.on('page-title-updated', (e) => {
@@ -53,7 +45,7 @@ exports.loginWebBoPicture = loginWebBoPicture;
5345

5446
// 登录博客园
5547
const loginCnBlog = function (item, focusedWindow, event) {
56-
getSiteCookie('https://www.cnblogs.com/', (cookie) => {
48+
getSiteCookie('https://i.cnblogs.com/', (cookie) => {
5749
dataStore.setCnBlogCookie(cookie)
5850
})
5951
};

0 commit comments

Comments
 (0)