社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

有没有办法在Javascript中获取GitHub概要文件的贡献数?

Murtada Altarouti • 3 年前 • 1649 次点击  

有没有办法使用Javascript(客户端)获取去年的捐款数量?请注意,这个数字代表公共和私有存储库

enter image description here

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128541
 
1649 次点击  
文章 [ 3 ]  |  最新文章 3 年前
awesomemaker3000
Reply   •   1 楼
awesomemaker3000    3 年前

我建议使用非官方的 Github Contributions API 位于 this site

下面是一个例子

async function getContributions(username) {
  var data = await (await fetch(`https://corsanywhere.herokuapp.com/https://github-contributions-api.deno.dev/${username}.json`)).json();
  console.log(data.totalContributions)
}
getContributions("octocat") // Returns 0, as Octocat has zero contributions
Murtada Altarouti
Reply   •   2 楼
Murtada Altarouti    3 年前

我尝试了许多解决方案,并改进了其中一个,以获得以下最适合我的解决方案:

  1. 在Javascript中创建函数
function get_contribution() {
    profile_url = "https://cors-anywhere.herokuapp.com/https://github.com/users/USERNAME/contributions";

    var xhr = new XMLHttpRequest();
    xhr.responseType = 'document';

    xhr.open('GET', profile_url, true);
    xhr.onload = function () {
        if (this.status == 200) {
            var response = xhr.responseXML.getElementsByClassName('f4 text-normal mb-2')[0].innerText;
            // get only the numbers in response
            contribution = response.replace(/[^0-9]/g, '');
            
            // The number of contributions is now saved in the contribution variable
        }
    };
    xhr.send();
}
  • 将用户名更改为所需的GitHub用户名
  • 请注意,您必须使用“cors”,否则它将不起作用
  1. 现在,您可以在任何地方使用该函数,但在我的情况下,我将在页面加载时调用它,并将其设置在HTML中的某个位置:
onload = function(){
    get_contribution();
  } 
VonC
Reply   •   3 楼
VonC    3 年前

使用GraphQL查询( GitHub API v4 )还有 ContributionsCollection 对象

定义 from 通过 now 减去一年,然后 to 通过 现在 .

   var to = new Date();
   var year  = to.getFullYear();
   var month = to.getMonth();
   var day   = to.getDate();
   var from = new Date(year - 1, month, day);

使用 graphql.js 然后打电话 in here :

query ContributionsView($username: String!, $from: DateTime!, $to: DateTime!) {
  user(login: $username) {
    contributionsCollection(from: $from, to: $to) {
      totalCommitContributions
      totalIssueContributions
      totalPullRequestContributions
      totalPullRequestReviewContributions
    }
  }
}

一旦脚本在你的博客页面外运行, you can include it 用一个 <script src=...> 要素