发布markdown格式的博客
首先通过get拿到写博客的网页
然后在里面提取出分类,user_code 等信息
最后拼接数据, 将markdown格式的blog发布
返回码为1时表示发布成功
代码
const axios = require('axios')
const fs = require('fs')
const $ = require('cheerio')
const qs = require('qs');
const path = './t.md'
const CONTENT = fs.readFileSync(path, 'utf8')
axios.interceptors.request.use(function (config) {
if (config.method === 'post') {
config.data = qs.stringify(config.data)
}
return config;
}, function (error) {
return Promise.reject(error);
});
const headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"cookie": "===",
"referrer": "https://my.oschina.net/ahaoboy",
}
async function main() {
let info = getInfo()
const url = 'https://my.oschina.net/ahaoboy/blog/save'
const data = {
'draft': '',
'id': "",
"user_code": '===',
"title": 'markdown',
"content": CONTENT,
"content_type": 3,
"catalog": "7027981",
classification: "428609",
type: 1,
origin_url: "",
privacy: 0,
deny_comment: 0,
as_top: 0,
downloadImg: 0,
isRecommend: 0,
}
console.log(info)
let resp = await axios.post(url, data, {headers})
console.log(resp.data)
//{
// code: 1,
// message: '发表成功',
return resp.data
}
// 获取发布时的信息, 分类, user_code, 类别等
async function getInfo() {
const url = 'https://my.oschina.net/ahaoboy/blog/write'
let resp = await axios.get(url, {headers})
let html = resp.data
// catalog classification
let dom = $.load(html)
let classificationDom = dom('select[name="classification"]>option')
console.log(classificationDom.length)
let classification = classificationDom.toArray().reduce(
(pre, now) => {
console.log(now.attribs.value, $(now).text())
pre[$(now).text()] = now.attribs.value
return pre
}, {}
)
console.log(classification)
let catalogDom = dom('select[name="catalog"]>option')
console.log(catalogDom.length)
let catalog = catalogDom.toArray().reduce(
(pre, now) => {
pre[$(now).text()] = now.attribs.value
return pre
}, {}
)
let userCodeDom = dom('input[name=user_code]')
console.log(userCodeDom.length)
userCode = userCodeDom[0].attribs.value
return {
classification,
catalog,
userCode
}
}
async function addCatalog() {
const url = 'https://my.oschina.net/ahaoboy/blog/quick_add_blog_catalog'
const data = {
space: 2856757,
userCode: '===',
user_code: '===',
name: 'test22'
}
let resp = await axios.post(url, data, {headers})
console.log(resp.data)
// 新增加的分类的id号
return resp.data.result.id
}
main()
来源:oschina
链接:https://my.oschina.net/ahaoboy/blog/4327751