GitHub 获得项目中每种编程语言占比

目录

首先使用 GitHub API 获得项目中每种语言的代码行数,
API: https://api.github.com/repos/<user/repo>/languages,替换其中的 <user/repo>
例如 curl https://api.github.com/repos/cli/cli/languages
返回的结果示例:

{
  "Go": 2843345,
  "Shell": 3801,
  "Makefile": 1900,
  "PowerShell": 445,
  "Batchfile": 26
}

然后计算百分比。

Python 脚本

import json
import urllib.request

user = input("Input user name:")
repo = input("Input repo name:")
ret = urllib.request.urlopen(f"https://api.github.com/repos/{user}/{repo}/languages")
d = json.loads(ret.read().decode("utf-8"))
total = sum(d.values())
for lang in d:
    p = round(d[lang] / total * 100, 2)
    print(f"{lang}, {d[lang]} ({p}%)")