如何使用 github API 从 github 的 URL 获取主要语言或语言列表?

Vla*_*koy 8 api github github-api

我有一个 github repo 的 URL,例如: https://github.com/dotnet/corefx

在 github 的 API 中是否有任何可能的方法可以将“C#”作为存储库的主要语言?

Ber*_*tel 9

您可以使用列表语言 Github API,它将为您提供此存储库中使用的所有语言以及用该语言编写的代码字节数:

GET https://api.github.com/repos/dotnet/corefx/languages
Run Code Online (Sandbox Code Playgroud)

这会给你:

{
  "C#": 131055040,
  "C": 1078381,
  "Visual Basic": 829607,
  "C++": 622926,
  "XSLT": 462336,
  "OpenEdge ABL": 139178,
  "Shell": 70286,
  "CMake": 60136,
  "PowerShell": 51624,
  "DIGITAL Command Language": 26402,
  "Groovy": 25726,
  "Batchfile": 21796,
  "Objective-C": 9455,
  "Makefile": 9085,
  "Roff": 4236,
  "Perl": 3895,
  "ASP": 1687,
  "Python": 1535,
  "1C Enterprise": 903,
  "HTML": 653
}
Run Code Online (Sandbox Code Playgroud)

使用 bash,您可以jq用来解析和选择具有最大字节值的字段:

language=`curl -s https://api.github.com/repos/dotnet/corefx/languages | jq 'to_entries | max_by(.value) | .key'`
echo "$language"
Run Code Online (Sandbox Code Playgroud)