直接调接口同步 Solo 和 Hacpai 的文章

本贴最后更新于 2036 天前,其中的信息可能已经沧海桑田

场景是 solo 里发了文章没有同步到 hacpai (老生常谈的问题了啊- - !

然后根据内容 API 开放,欢迎各位独立博客主进行连接这篇文章的内容作了一个同步小脚本

1. 使用跨域模式开启浏览器(以 chrome 为例)

跨域模式开启方法
因为有一个接口调用跨域了,所以要使用跨域模式

2. 获取需要同步的文章 id

接口和对应字段如图

3. 在控制台中执行以下代码

const _ajax = (url, cb, opt) => {
  $.ajax(Object.assign({
    url: url,
    dataType: 'json',
    success: cb
  }, opt))
}

const getArticle = ({
  id,
  host
}) => {
  return new Promise((resolve, reject) => {
    _ajax(`${host}/console/article/${id}`, (data) => resolve(data))
  }).then(res => {
    return {
      id: id,
      title: res.article.articleTitle,
      permalink: res.article.articlePermalink,
      tags: res.article.articleTags.map(t => t.tagTitle).join(','),
      content: res.article.articleContent
    }
  })
}

const getClient = ({
  id,
  host
}) => {
  return new Promise((resolve, reject) => {
    _ajax(`${host}/console/preference/`, (data) => resolve(data))
  }).then(res => {
    return {
      key: res.preference.keyOfSolo,
      title: res.preference.blogTitle,
      host: host,
      email: res.preference.adminEmail
    }
  })
}

const connect = (data) => {
  return new Promise((resolve, reject) => {
    _ajax(`https://rhythm.b3log.org/api/article`, (data) => resolve(data), {
      method: 'post',
      data: JSON.stringify(data),
      contentType: 'application/json;charset=utf-8'
    })
  })
}

(async function (config) {
  const article = await getArticle(config)
  const client = await getClient(config)
  const result = await connect({
    article,
    client
  })
  console.log(result)
})({
  id: '这里替换成文章id',
  host: '这里写你的solo博客的域名,比如我的是https://blog.hduzplus.xyz'
})

4. 执行完成后

应该就能同步过来了吧- - (祈祷 🙏

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...