冰老师的 api 年久失修了,所以重新用 node 写一个 githubcalendar 的爬虫

可以在冰老师原有的教程修改修改即可使用该 api

fork 如下仓库

修改 vercel.json,替换 headers 的跨域设置为自己的域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "https://blog.keyiqingxin.cn"
},
{
"key": "Access-Control-Allow-Headers",
"value": "content-type"
},
{
"key": "Access-Control-Allow-Methods",
"value": "DELETE,PUT,POST,GET,OPTIONS"
}
]
}
]
}

vercel 部署此 api

网页访问:vercel访问域名/api?name=github名字

接下来就是替换 api 了,可以看到该 api 和 冰老师提供的 api 有所差异,所以使用的话,需要修改一下 npm包中的内容
1. 首先得安装 hexo-githubcalendar 插件包 npm i hexo-githubcalendar
2. 在 node_modules 中找到该包,修改以下代码

    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function GithubCalendarConfig(){
- var git_githubapiurl ="${github_api}?${github_user}"
+ var git_githubapiurl ="${github_api}?name=${github_user}";
var git_color =${github_color};
var git_user ="${github_user}";
var parent_div_git = ${get_layout};
var git_div_html = '${githubcalendar_html}';
if(parent_div_git && location.pathname =='${calendar_enable_page}'){
console.log('已挂载github calendar')
// parent_div_git.innerHTML=git_div_html+parent_div_git.innerHTML // 无报错,但不影响使用(支持pjax跳转)
parent_div_git.insertAdjacentHTML("afterbegin",git_div_html) // 有报错,但不影响使用(支持pjax跳转)
};
GithubCalendar(git_githubapiurl,git_color,git_user)
}
if(${get_layout}){
GithubCalendarConfig()
}
  1. 初始化 npm

    1
    npm init -y
  2. 安装依赖

    1
    npm i cheerio -S
  3. 目录结构

    1
    2
    3
    4
    5
    ├─package-lock.json
    ├─package.json
    ├─vercel.json
    ├─api
    | └index.js
  4. index.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    /*
    * @Author: 可以清心
    * @Description:
    * @Date: 2023-01-21 21:43:31
    * @LastEditTime: 2023-01-22 11:26:23
    * @FilePath: \github-contribute\api\index.js
    */
    const https = require("https");
    const cheerio = require("cheerio");

    module.exports = (req, res) => {
    const NAME = req.query.name;
    let html = "";

    https.get(`https://github.com/${NAME}`, function(resp){
    resp.on("data", function(chunk){
    html += chunk;
    });

    resp.on("end", function(){
    let result = [];
    let total = 0;

    const $ = cheerio.load(html);


    $(".js-yearly-contributions > .position-relative .js-calendar-graph > .js-calendar-graph-svg > g > g").each((index, g) => {
    let item = [];
    $(g).find("rect").each((index, rect) => {
    const $r = $(rect);

    const date = $r.attr("data-date");

    const text = $r.text();
    let count = parseInt(text);

    if(!isNaN(count)){
    total += count;
    }else{
    count = 0;
    }

    item.push({
    date,
    count
    })
    })

    result.push(item);
    })

    res.status(200).json({
    total,
    contributions: result
    })
    });

    resp.on("error", error => {
    res.status(500).json({
    code: 500,
    message: "请求超时"
    })
    })
    })
    }
  5. vercel.json

    • 替换为自己的博客域名
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    {
    "headers": [
    {
    "source": "/(.*)",
    "headers": [
    {
    "key": "Access-Control-Allow-Origin",
    "value": "https://blog.keyiqingxin.cn"
    },
    {
    "key": "Access-Control-Allow-Headers",
    "value": "content-type"
    },
    {
    "key": "Access-Control-Allow-Methods",
    "value": "DELETE,PUT,POST,GET,OPTIONS"
    }
    ]
    }
    ]
    }
  6. 上传至 githubvercel 拉取部署

  7. 浅浅修改插件 hexo-githubcalendar 的源码(步骤见 tab fork源码方式)