使用代码将github仓库里某个issue同步到CSDN博客上

Posted sap-jerry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用代码将github仓库里某个issue同步到CSDN博客上相关的知识,希望对你有一定的参考价值。

我是一个懒惰的程序员。我在github仓库里用issue的方式写了很多分享文章,想同步到CSDN上。但是我又不想一篇篇手动复制粘贴,因此想用代码来实现自动化。

例子:
https://github.com/i042416/KnowlegeRepository/issues/2215

这是我的一个issue:

技术图片

我使用下面这些nodejs代码实现从github 仓库issue到CSDN博客的拷贝:

var config = require("./mcConfig");
var request = require('request');
var querystring = require('querystring');

function createPost(oPost) {

  var url = "https://mp.csdn.net/mdeditor/saveArticle";
  var oBody = {
          title: oPost.title,
          markdowncontent: oPost.body,
          tags:"Fiori",
          categories:"Fiori",
          channel:"14",
          type:"original",
          articleedittype:"1",
          content: oPost.body
        };

var formData = querystring.stringify(oBody);
var contentLength = formData.length;

var createPostOptions = {
        url: url,
        method: "POST",  
        headers: {
            "content-type": "application/x-www-form-urlencoded",
            "Content-Length": contentLength,
            "origin" :"https://mp.csdn.net",
            "referer" :"https://mp.csdn.net/mdeditor",
            "User-Agent" :"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
            "cookie": config.cookie
        },
        body: formData
};

  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      console.log("Step1: create post via url: "   url );

      requestC(createPostOptions,function(error,response,body){
       
       if(error){
          reject(error);
       }
       console.log("response: "   body);
       resolve(body);
      }); 
     });
}

module.exports = createPost;

var request = require('request');

function getIssue(issueNumber) {

  var url = "https://api.github.com/repos/i042416/KnowlegeRepository/issues/"   issueNumber;

  var getIssueOptions = {
        url: url,
        method: "GET",
        json:true,
        headers: {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
        }
  };

  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      console.log("Step1: get issue detail via url: "   url );

      requestC(getIssueOptions,function(error,response,body){
        if(error){
          console.log("error occurred: "   error);
          reject(error);
        }
        console.log("title:"   body.title);
        console.log("body: "   body.body);
        for( var i = 0; i < body.labels.length; i  ){
          console.log("label: "   body.labels[i].name);
        }
        resolve(body);
      }); 
     });
}

module.exports = getIssue;

var readIssue = require("./readIssueMod");
var createPost = require("./createPostMod");

readIssue(2215).then(createPost).catch((error)=>{console.log("error: "   error)});

执行结果:

技术图片

已经自动同步到CSDN了,方便!
技术图片

要获取更多Jerry的原创文章,请关注公众号"汪子熙":
技术图片

以上是关于使用代码将github仓库里某个issue同步到CSDN博客上的主要内容,如果未能解决你的问题,请参考以下文章

使用github同步网站

使用git将本地仓库同步到github远程仓库

将本地代码上传到github

学习github -- 同步到github多人协作

Git同步更新操作GitHub和码云仓库上面的代码

git将远程某个仓库的代码合并到本地