我已经编写了代码来返回用户已经提交给GitHub的“连胜”(连续多少天)。不幸的是,它递归地发出GitHub API请求,最终很快就会遇到速率限制问题(即使使用API令牌)。我有更好的方法来检索这些信息吗?
基本上,我正在寻找的是在用户帐户页面上显示的“绿色方块”数据:
我遇到速率限制的示例代码:
const express = require('express');
const request = require('request');
const moment = require('moment');
require('dotenv').config();
const app = express();
const port = 5000;
app.get('/streak/:user', async function (req, res) {
const yesterdaysDate = moment().subtract(1, 'day').format('YYYY-MM-DD');
try {
const streakCountTotal = await checkUserCommitForDate(req.params.user, yesterdaysDate);
res.send({ streakCountTotal });
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
async function checkUserCommitForDate(user, date) {
const authorOptions = {
url: `https://api.github.com/search/commits?q=author:${user}+author-date:${date}`,
headers: {
'User-Agent': 'request',
'Accept': 'application/vnd.github.cloak-preview',
'Authorization': `token ${process.env.GITHUB_SECRET_ACCESS_TOKEN}`
}
};
const committerOptions = {
url: `https://api.github.com/search/commits?q=committer:${user}+committer-date:${date}`,
headers: {
'User-Agent': 'request',
'Accept': 'application/vnd.github.cloak-preview',
'Authorization': `token ${process.env.GITHUB_SECRET_ACCESS_TOKEN}`
}
};
const githubAuthorResponse = await promisify(request)(authorOptions);
const githubCommitterResponse = await promisify(request)(committerOptions);
const githubAuthorCount = Number(JSON.parse(githubAuthorResponse.body).total_count);
const githubCommitterCount = Number(JSON.parse(githubCommitterResponse.body).total_count);
if (isNaN(githubAuthorCount) || isNaN(githubAuthorCount)) {
throw new Error('GitHub contribution count was not a number. Body of response was:', githubAuthorResponse.body);
} else if (githubAuthorCount + githubCommitterCount > 0) {
const previousDaysDate = moment(date).subtract(1, 'day').format('YYYY-MM-DD');
let streakCounter = await checkUserCommitForDate(user, previousDaysDate);
streakCounter++;
console.log('streakCounter', streakCounter);
return streakCounter;
} else {
return 0;
}
}
function promisify(fn) {
return function (...args) {
return new Promise((resolve, reject) => {
fn(...args, (err, result) => {
if (err) {
console.log('error', err);
return reject(err);
}
resolve(result);
});
});
};
};
app.listen(port, function () {
console.log('listening on port', port);
});
答案 0 :(得分:2)
您可以从个人资料页面中使用的svg贡献日历数据中提取贡献列表:
https://github.com/users/bertrandmartel/contributions
解析xml非常简单,只需一个请求就可以处理用户贡献统计数据。以下是使用cheerio解析xml:
的nodeJS示例Select
请注意,您还可以按照here
显示处理贡献统计信息需要注意的是,它没有使用官方API,但被视为日历API贡献的事件可能会发生变化。使用API,您需要重新定义与定义here
相同的条件