批量迁移文章到博客园
前言
最开始使用博客园是在2016年,后来由于博客园不支持markdown,就很少在cnblogs上更新笔记了。自己搭建了一个博客,运行了两年,自己懒得维护了,就打算迁移文章到博客园。让我惊讶的是博客园markdown支持已经很好了,以后还在博客园写笔记。
思路
博客园提供了metaweblog api,利用他们提供的接口编写node脚本,批量把文章导入到博客园。
从结果来开思路是可行的,但是由于博客园做了发布限制,30秒只能发布一篇文章,所以虽然用的脚本导入但还是用了不少时间。
导入效果如图所示:
metaWeblog使用方式
var MetaWeblog = require("metaweblog-api");
var apiUrl = "https://rpc.cnblogs.com/metaweblog/yesyes"; // use your blog API instead
var metaWeblog = new MetaWeblog(apiUrl);
var username = 'wmui', password = 'xxx'; // 博客园的账户和密码
const axios = require('axios')
// 获取博客信息示例
metaWeblog
.getUsersBlogs("yesyes",username, password);
.then((blogInfo) => {
console.log(blogInfo);
// handle the blog information here
})
.catch((error) => {
console.log(error);
});
// [
// {
// blogid: '306210',
// url: 'http://www.cnblogs.com/yesyes/',
// blogName: 'wmui'
// }
// ]
// 获取分类示例
metaWeblog.getCategories('306210', username, password).then(data => {
console.log(data)
}).catch(err => {
console.log(err)
})
简单的使用了几个api测试了下,可行,下面就是文章导入了。前提是被导入博客有api。
/* eslint-disable */
var MetaWeblog = require("metaweblog-api");
var apiUrl = "https://rpc.cnblogs.com/metaweblog/yesyes"; // use your blog API instead
var metaWeblog = new MetaWeblog(apiUrl);
var username = 'wmui', password = 'mc@1308017'
const axios = require('axios')
function sleep(time = 1000) {
return new Promise(resolve => setTimeout(resolve, time))
}
async function start() {
const { data } = await axios({
method: 'get',
url: 'https://blog.86886.wang/api/articles?category=5d35c5708c088deb2071570b&keywords=&page=1&limit=15', // api地址
headers: {
token: 'xxx' // 如果需要token
}
})
data.data.reverse(); // 按照博客原来的顺序
for (let index = 0; index < data.data.length; index++) {
const article = data.data[index];
console.log(article.title)
// if(index == 0) continue
await metaWeblog.newPost('', username, password, {
title: article.title, // 文章标题
description: article.content, // 文章内容
dateCreated: new Date(article.createdAt), // 创建时间,这个是无效的,。博客园官方不允许定义创建时间
categories: ['[Markdown]'], // '[Markdown]'表示使用markdown编辑器,这里要感谢github的一个大哥,参考了他的开源项目: https://github.com/kotcmm/writecnblog/issues/32
mt_keywords: 'Mac', // 标签,多个用逗号分隔
}, true).then(data => {
console.log(data)
}).catch(err => {
console.log(err)
})
// 没发布一篇文章,自动暂停45s,官方有限制
await sleep(45000)
}
}
start()
结语
如果文章少,还是建议直接复制好了,30s的时间间隔足够复制了😂
胖胖熊笔记,笔记已迁移