用户配置文件的年度GitHub的用户贡献数中显示的贡献用户在过去一年取得的数量:获取GitHub上
我想显示在我的网站这个信息,最好而不必引入任何类型的后端。我在几个地方找过它。没有任何内容在/users/(me)/中列出。抓取页面源的短处,有没有办法检索这些信息?我没有看到任何记录...
用户配置文件的年度GitHub的用户贡献数中显示的贡献用户在过去一年取得的数量:获取GitHub上
我想显示在我的网站这个信息,最好而不必引入任何类型的后端。我在几个地方找过它。没有任何内容在/users/(me)/中列出。抓取页面源的短处,有没有办法检索这些信息?我没有看到任何记录...
我已经解决使用PHP我的个人资料页面的HTML。我开始通过下载HTML为我的个人资料页:
$githubProfile = file_get_contents('https://github.com/controversial');
下一个我在网页上找到的短语contributions in the last year
的位置。 contributions
和in the last year
之间的空格是奇怪的,所以我使用正则表达式来匹配任意数量的空间。
$contributionsIndex = preg_match(
'/contributions\s+in the last year/',
$githubProfile,
$matches,
PREG_OFFSET_CAPTURE
);
$index = $matches[0][1];
最后,我从这个点向后迭代,直到我遇到一个既不是数字也不是逗号的字符。一旦这个条件变为假的,我知道我已经找到了号码的开头:
$endIndex = $index;
while (is_numeric($githubProfile[$index-2]) || $githubProfile[$index-2] == ',')
$index--;
$contributionsCount = substr($githubProfile, $index-1, $endIndex-$index);
最后,我赞同数出(不含引号)
echo(str_replace(',', '', $contributionsCount));
最后,我用AJAX调用从我的JavaScript页面获取PHP脚本的值。
function get(url, callback) {
/* eslint-disable no-console */
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = e => (callback || console.log)(e.target.responseText);
xhr.onerror =() => console.log('error');
xhr.send();
/* eslint-enable no-console */
}
// Fill in GitHub contributions count
get('contributions-count.php', (resp) => {
document.getElementById('gh-contributions-count').innerText = resp;
});
我敢肯定,这将是accomplishable更清洁的未来与GitHub的GraphQL API,但是这仍然处于预览阶段。
你见过GithubAPI?使用它,你可以从你的js文件运行请求,而不需要实现任何后端。
为了您的目的,我认为您可以使用以下请求https://api.github.com/users/yourUserName/events
。这会为您提供与youUserName相关的事件列表。
e.g
[
{
"id": "xxxxxxx",
"type": "PushEvent",
"actor": {.......},
"repo": {......},
"payload": {.......},
"public": true,
"created_at": "2016-11-17T21:33:15Z"
},
.....
]
你应该去通过列表和过滤型= PushEvent和created_at = lastYear。
我希望这能帮助你!
更新:该服务只给结果的最后3个月,所以另一种可能是,检查(用户/用户名/回购),之后检查提交了他们(回购/用户名/ repoName /提交)
也可以使用xml解析器解析svg日历数据,然后求和所有data-count
条目。使用命令行
const user = 'controversial';
fetch('https://urlreq.appspot.com/req?method=GET&url=https://github.com/users/' + user + '/contributions')
.then(function(response) {
return response.text();
})
.then(function(text) {
xmlDoc = new DOMParser().parseFromString(text, 'text/xml');
var nodes = xmlDoc.getElementsByTagName('rect');
var count = 0;
for (var i = 0; i < nodes.length; i++) {
count += parseInt(nodes[i].getAttribute('data-count'));
}
console.log('contributions count : ' + count);
})
.catch(function(error) {
console.log('Request failed', error)
});
又如与curl & xmlstarlet可以发现here
不幸的是,第一:要避免CORS问题,您可以在http://urlreq.appspot.com/req使用代理像urlreq生活页面只能回溯大约9天,解析最后一页中的所有内容将远远超过GitHub API的速率限制。 –
你说得对,它会给你过去90天的承诺,我没有看到文档中的限制。你有没有试图让你的回购(用户/用户名/回购),然后检查他们提交(回购/用户名/回购/提交)? – Alan
我可以试试。谢谢@Alan。 –