github api 查询 github 上仓库的 star 和 fork 数

1,397 阅读3分钟

最近在学习 nodejsnestjs , 看到 ORM 主要有 TypeORMPrisma ,接着又看到 Blitz 这个全栈框架(内置了 prisma + typescirpt + nextjs ,生成前后端一体的项目很不错,还内置了邮箱发送、权限验证等!),看着 star 数很高,想要跟其他主流 nodejs 项目对比一下,但是又不想一个一个去查,还是用代码请求吧。

使用 github api 查询 github 仓库 的信息

因为只是查询,所以用不上token,直接请求就好了。

格式:https://api.github.com/repos/用户名/仓库名

比如,查询nestjs的仓库信息: api.github.com/repos/nestj… https://api.github.com/repos/nestjs/nest 会得到

{
  "id": 80945428,
  "node_id": "MDEwOlJlcG9zaXRvcnk4MDk0NTQyOA==",
  "name": "nest",
  "full_name": "nestjs/nest",
  "private": false,
  "owner": {
    "login": "nestjs",
    "id": 28507035,
    "node_id": "MDEyOk9yZ2FuaXphdGlvbjI4NTA3MDM1",
    "avatar_url": "<https://avatars.githubusercontent.com/u/28507035?v=4>",
    "gravatar_id": "",
    "url": "<https://api.github.com/users/nestjs>",
    "html_url": "<https://github.com/nestjs>",
    "followers_url": "<https://api.github.com/users/nestjs/followers>",
    "following_url": "[https://api.github.com/users/nestjs/following{/other_user}](https://api.github.com/users/nestjs/following%7B/other_user%7D)",
    "gists_url": "[https://api.github.com/users/nestjs/gists{/gist_id}](https://api.github.com/users/nestjs/gists%7B/gist_id%7D)",
    "starred_url": "[https://api.github.com/users/nestjs/starred{/owner}{/repo}](https://api.github.com/users/nestjs/starred%7B/owner%7D%7B/repo%7D)",
    "subscriptions_url": "<https://api.github.com/users/nestjs/subscriptions>",
    "organizations_url": "<https://api.github.com/users/nestjs/orgs>",
    "repos_url": "<https://api.github.com/users/nestjs/repos>",
    "events_url": "[https://api.github.com/users/nestjs/events{/privacy}](https://api.github.com/users/nestjs/events%7B/privacy%7D)",
    "received_events_url": "<https://api.github.com/users/nestjs/received_events>",
    "type": "Organization",
    "site_admin": false
  },
  "html_url": "<https://github.com/nestjs/nest>",
  "description": "A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) 🚀",
  "fork": false,
  "url": "<https://api.github.com/repos/nestjs/nest>",
  "forks_url": "<https://api.github.com/repos/nestjs/nest/forks>",
  "keys_url": "[https://api.github.com/repos/nestjs/nest/keys{/key_id}](https://api.github.com/repos/nestjs/nest/keys%7B/key_id%7D)",
  "collaborators_url": "[https://api.github.com/repos/nestjs/nest/collaborators{/collaborator}](https://api.github.com/repos/nestjs/nest/collaborators%7B/collaborator%7D)",
  "teams_url": "<https://api.github.com/repos/nestjs/nest/teams>",
  "hooks_url": "<https://api.github.com/repos/nestjs/nest/hooks>",
  "issue_events_url": "[https://api.github.com/repos/nestjs/nest/issues/events{/number}](https://api.github.com/repos/nestjs/nest/issues/events%7B/number%7D)",
  "events_url": "<https://api.github.com/repos/nestjs/nest/events>",
  "assignees_url": "[https://api.github.com/repos/nestjs/nest/assignees{/user}](https://api.github.com/repos/nestjs/nest/assignees%7B/user%7D)",
  "branches_url": "[https://api.github.com/repos/nestjs/nest/branches{/branch}](https://api.github.com/repos/nestjs/nest/branches%7B/branch%7D)",
  "tags_url": "<https://api.github.com/repos/nestjs/nest/tags>",
  "blobs_url": "[https://api.github.com/repos/nestjs/nest/git/blobs{/sha}](https://api.github.com/repos/nestjs/nest/git/blobs%7B/sha%7D)",
  "git_tags_url": "[https://api.github.com/repos/nestjs/nest/git/tags{/sha}](https://api.github.com/repos/nestjs/nest/git/tags%7B/sha%7D)",
  "git_refs_url": "[https://api.github.com/repos/nestjs/nest/git/refs{/sha}](https://api.github.com/repos/nestjs/nest/git/refs%7B/sha%7D)",
  "trees_url": "[https://api.github.com/repos/nestjs/nest/git/trees{/sha}](https://api.github.com/repos/nestjs/nest/git/trees%7B/sha%7D)",
  "statuses_url": "[https://api.github.com/repos/nestjs/nest/statuses/{sha}](https://api.github.com/repos/nestjs/nest/statuses/%7Bsha%7D)",
  "languages_url": "<https://api.github.com/repos/nestjs/nest/languages>",
  "stargazers_url": "<https://api.github.com/repos/nestjs/nest/stargazers>",
  "contributors_url": "<https://api.github.com/repos/nestjs/nest/contributors>",
  "subscribers_url": "<https://api.github.com/repos/nestjs/nest/subscribers>",
  "subscription_url": "<https://api.github.com/repos/nestjs/nest/subscription>",
  "commits_url": "[https://api.github.com/repos/nestjs/nest/commits{/sha}](https://api.github.com/repos/nestjs/nest/commits%7B/sha%7D)",
  "git_commits_url": "[https://api.github.com/repos/nestjs/nest/git/commits{/sha}](https://api.github.com/repos/nestjs/nest/git/commits%7B/sha%7D)",
  "comments_url": "[https://api.github.com/repos/nestjs/nest/comments{/number}](https://api.github.com/repos/nestjs/nest/comments%7B/number%7D)",
  "issue_comment_url": "[https://api.github.com/repos/nestjs/nest/issues/comments{/number}](https://api.github.com/repos/nestjs/nest/issues/comments%7B/number%7D)",
  "contents_url": "[https://api.github.com/repos/nestjs/nest/contents/{+path}](https://api.github.com/repos/nestjs/nest/contents/%7B+path%7D)",
  "compare_url": "[https://api.github.com/repos/nestjs/nest/compare/{base}...{head}](https://api.github.com/repos/nestjs/nest/compare/%7Bbase%7D...%7Bhead%7D)",
  "merges_url": "<https://api.github.com/repos/nestjs/nest/merges>",
  "archive_url": "[https://api.github.com/repos/nestjs/nest/{archive_format}{/ref}](https://api.github.com/repos/nestjs/nest/%7Barchive_format%7D%7B/ref%7D)",
  "downloads_url": "<https://api.github.com/repos/nestjs/nest/downloads>",
  "issues_url": "[https://api.github.com/repos/nestjs/nest/issues{/number}](https://api.github.com/repos/nestjs/nest/issues%7B/number%7D)",
  "pulls_url": "[https://api.github.com/repos/nestjs/nest/pulls{/number}](https://api.github.com/repos/nestjs/nest/pulls%7B/number%7D)",
  "milestones_url": "[https://api.github.com/repos/nestjs/nest/milestones{/number}](https://api.github.com/repos/nestjs/nest/milestones%7B/number%7D)",
  "notifications_url": "[https://api.github.com/repos/nestjs/nest/notifications{?since,all,participating}](https://api.github.com/repos/nestjs/nest/notifications%7B?since,all,participating})",
  "labels_url": "[https://api.github.com/repos/nestjs/nest/labels{/name}](https://api.github.com/repos/nestjs/nest/labels%7B/name%7D)",
  "releases_url": "[https://api.github.com/repos/nestjs/nest/releases{/id}](https://api.github.com/repos/nestjs/nest/releases%7B/id%7D)",
  "deployments_url": "<https://api.github.com/repos/nestjs/nest/deployments>",
  "created_at": "2017-02-04T20:12:52Z",
  "updated_at": "2022-11-04T06:44:49Z",
  "pushed_at": "2022-11-04T07:25:36Z",
  "git_url": "git://github.com/nestjs/nest.git",
  "ssh_url": "git@github.com:nestjs/nest.git",
  "clone_url": "<https://github.com/nestjs/nest.git>",
  "svn_url": "<https://github.com/nestjs/nest>",
  "homepage": "[https://nestjs.com](https://nestjs.com/)",
  "size": 268814,
  "stargazers_count": 51885,
  "watchers_count": 51885,
  "language": "TypeScript",
  "has_issues": true,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": true,
  "has_pages": false,
  "forks_count": 6106,
  "mirror_url": null,
  "archived": false,
  "disabled": false,
  "open_issues_count": 82,
  "license": {
    "key": "mit",
    "name": "MIT License",
    "spdx_id": "MIT",
    "url": "<https://api.github.com/licenses/mit>",
    "node_id": "MDc6TGljZW5zZTEz"
  },
  "allow_forking": true,
  "is_template": false,
  "web_commit_signoff_required": false,
  "topics": [
    "framework",
    "hacktoberfest",
    "javascript",
    "javascript-framework",
    "microservices",
    "nest",
    "nestjs",
    "node",
    "nodejs",
    "nodejs-framework",
    "typescript",
    "typescript-framework",
    "websockets"
  ],
  "visibility": "public",
  "forks": 6106,
  "open_issues": 82,
  "watchers": 51885,
  "default_branch": "master",
  "temp_clone_token": null,
  "organization": {
    "login": "nestjs",
    "id": 28507035,
    "node_id": "MDEyOk9yZ2FuaXphdGlvbjI4NTA3MDM1",
    "avatar_url": "<https://avatars.githubusercontent.com/u/28507035?v=4>",
    "gravatar_id": "",
    "url": "<https://api.github.com/users/nestjs>",
    "html_url": "<https://github.com/nestjs>",
    "followers_url": "<https://api.github.com/users/nestjs/followers>",
    "following_url": "[https://api.github.com/users/nestjs/following{/other_user}](https://api.github.com/users/nestjs/following%7B/other_user%7D)",
    "gists_url": "[https://api.github.com/users/nestjs/gists{/gist_id}](https://api.github.com/users/nestjs/gists%7B/gist_id%7D)",
    "starred_url": "[https://api.github.com/users/nestjs/starred{/owner}{/repo}](https://api.github.com/users/nestjs/starred%7B/owner%7D%7B/repo%7D)",
    "subscriptions_url": "<https://api.github.com/users/nestjs/subscriptions>",
    "organizations_url": "<https://api.github.com/users/nestjs/orgs>",
    "repos_url": "<https://api.github.com/users/nestjs/repos>",
    "events_url": "[https://api.github.com/users/nestjs/events{/privacy}](https://api.github.com/users/nestjs/events%7B/privacy%7D)",
    "received_events_url": "<https://api.github.com/users/nestjs/received_events>",
    "type": "Organization",
    "site_admin": false
  },
  "network_count": 6106,
  "subscribers_count": 693
}

其中 star 数可以使用 stargazers_count 字段获取, fork 数可以使用 forks 或者 forks_count字段获取, watch 数可以使用 watchers_count 字段获取。

获取主流的Nodejs框架的 star 数和fork

编写nodejs脚本,获取需要的数据!

import fetch from 'node-fetch';

const fetchGithubRepo = async () => {
  //['https://api.github.com/repos/nestjs/nest'];
  const repos = [
    'nestjs/nest',
    'expressjs/express',
    'koajs/koa',
    'blitz-js/blitz',
    'fastify/fastify',
    'meteor/meteor',
    'eggjs/egg',
    'midwayjs/midway',
  ];
  const urls = repos.map(repo => `https://api.github.com/repos/${repo}`);
  const requests = urls.map(url => fetch(url).then(r => r.json()));
  const result = await Promise.all(requests);
  const data = result
    .map(e => ({
      name: e.name,
      star: e.stargazers_count,
      fork: e.forks,
      watch: e.watchers_count,
    }))
    .sort((a, b) => b.star - a.star);
  console.log(data);
};
fetchGithubRepo();
// 得到如下数据
[
//   { name: 'express', star: 58791, fork: 9977, watch: 58791 },
//   { name: 'nest', star: 51892, fork: 6107, watch: 51892 },
//   { name: 'meteor', star: 43121, fork: 5226, watch: 43121 },
//   { name: 'koa', star: 33265, fork: 3229, watch: 33265 },
//   { name: 'fastify', star: 25460, fork: 1879, watch: 25460 },
//   { name: 'egg', star: 18210, fork: 1780, watch: 18210 },
//   { name: 'blitz', star: 12257, fork: 716, watch: 12257 },
//   { name: 'midway', star: 6173, fork: 457, watch: 6173 }
// ]
框架starforkwatch
Express58791997758791
Nestjs51892610651892
Meteor43121522643121
Kao33265322933265
Fastify25460187925460
Egg18210178018210
Blitz1225771612257
Midway61734576173

对比了下,BlitzFastify 也比不上,还是放弃吧!