Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取每年GitHub用户贡献的数量_Javascript_Github_Client Side_Github Api_Curl_Xmlstarlet - Fatal编程技术网

Javascript 获取每年GitHub用户贡献的数量

Javascript 获取每年GitHub用户贡献的数量,javascript,github,client-side,github-api,curl,xmlstarlet,Javascript,Github,Client Side,Github Api,Curl,Xmlstarlet,GitHub上的用户配置文件显示用户在过去一年中所做贡献的数量: 我想在我的网站上显示这些信息,最好不用引入任何后端。我已经在好几个地方找过了。中没有列出任何内容。除了抓取页面源代码外,有没有办法检索这些信息?我没有看到任何记录…您看到了吗?使用它,您可以从js文件运行请求,而无需实现任何后端 出于您的目的,我认为您可以使用以下请求https://api.github.com/users/yourUserName/events。这将为您提供一个与youUserName相关的事件列表 e、 g

GitHub上的用户配置文件显示用户在过去一年中所做贡献的数量:

我想在我的网站上显示这些信息,最好不用引入任何后端。我已经在好几个地方找过了。中没有列出任何内容。除了抓取页面源代码外,有没有办法检索这些信息?我没有看到任何记录…

您看到了吗?使用它,您可以从js文件运行请求,而无需实现任何后端

出于您的目的,我认为您可以使用以下请求
https://api.github.com/users/yourUserName/events
。这将为您提供一个与youUserName相关的事件列表

e、 g

您应该浏览列表并筛选type=PushEvent和created\u at=lastYear

我希望这能帮助你



更新:该服务只是提供过去3个月的结果,所以其他可能是检查(users/username/repos),然后检查对它们的提交(repos/username/repoName/commits)

我已经决定使用PHP解析我个人资料页面的HTML。我首先下载我的个人资料页面的HTML:

$githubProfile = file_get_contents('https://github.com/controversial');
接下来,我在页面上找到短语
去年贡献的位置。去年
贡献
之间的空格很奇怪
,所以我使用正则表达式来匹配任意大小的空格

$contributionsIndex = preg_match(
  '/contributions\s+in the last year/',
  $githubProfile,
  $matches,
  PREG_OFFSET_CAPTURE
);
$index = $matches[0][1];
最后,我从这一点开始向后迭代,直到遇到一个既不是数字也不是逗号的字符。一旦此条件变为false,我知道我找到了数字的开头:

$endIndex = $index;

while (is_numeric($githubProfile[$index-2]) || $githubProfile[$index-2] == ',')
  $index--;

$contributionsCount = substr($githubProfile, $index-1, $endIndex-$index);
最后,我将数字回显(不带逗号)

最后,我使用JavaScript页面中的AJAX调用从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;
});

我相信,将来使用GitHub的GraphQL API可以更清晰地实现这一点,但这仍处于预览阶段。

也可以使用xml解析器解析svg日历数据,然后对所有
数据计数
项进行求和。为避免CORS问题,您可以使用代理,如“依靠生活”:

const user='有争议';
取('https://urlreq.appspot.com/req?method=GET&url=https://github.com/users/“+用户+”/contributions')
.然后(功能(响应){
返回response.text();
})
.然后(函数(文本){
xmlDoc=new DOMParser().parseFromString(text,'text/xml');
var nodes=xmlDoc.getElementsByTagName('rect');
var计数=0;
对于(var i=0;i});
不幸的是,第一个页面只能追溯到9天左右,而解析最后一个页面的所有内容将远远超过GitHub API的速率限制。没错,它将提供过去90天的提交,我在文档中没有看到限制。您是否尝试过获取您的回购协议(用户/用户名/回购协议),然后检查它们的提交(回购协议/用户名/回购协议/提交协议)?我可以试试。谢谢你,艾伦。
echo(str_replace(',', '', $contributionsCount));
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;
});