[英]Github API How to get a count of all the files of a git repository with a particular extension (.c, .cpp, .py etc) using python language?
I am trying to implement a python code to count files with a particular extension (of a repository) in Github.我正在尝试实现一个 python 代码来计算 Github 中具有特定扩展名(存储库)的文件。 Any help in this direction will be appreciated.
在这个方向上的任何帮助将不胜感激。
EDIT : I have been able only to list the repositories of a particular language: For example by requesting the below url: url=' https://api.github.com/legacy/repos/search/Go?language=Go '编辑:我只能列出特定语言的存储库:例如,通过请求以下 url: url=' https://api.github.com/legacy/repos/search/Go?language=Go '
But, is there any way I can check all the files with a particular extension and count.但是,有什么方法可以检查具有特定扩展名和计数的所有文件。 I have never used an api before.
我以前从未使用过api。
This isn't easy if your GitHub repo has many folders and files.如果您的 GitHub 存储库有很多文件夹和文件,这并不容易。
You would use the Get Content API for a given repo:您将为给定的 repo 使用Get Content API :
GET /repos/:owner/:repo/contents/:path
You can call it in python, using a JSON library to decode the result: see this answer .您可以在 python 中调用它,使用 JSON 库来解码结果:请参阅此答案。
But it isn't a recursive function, so the result list the files and folders directly under :path
.但它不是递归函数,因此结果会直接列出
:path
下的文件和文件夹。
So you would have to repeat that call in order to go into all subfolders, get all the files and make your tally.因此,您必须重复该调用才能进入所有子文件夹,获取所有文件并进行计数。
And that for all repos you have listed for a particular language.对于您为特定语言列出的所有存储库。
This could be accomplished using the search/code
API.这可以使用
search/code
API 来完成。 Documentation can be found here.文档可以在这里找到。 Search API Docs
搜索 API 文档
The URL below will show the typescript results for the Angular repository.下面的 URL 将显示 Angular 存储库的打字稿结果。
GitHub Search Code API → Angular Repo GitHub 搜索代码 API → Angular 存储库
An example query for this usage would be:这种用法的示例查询是:
q=*+extensions:py+repo:ORG/REPO
q=*+扩展:py+repo:ORG/REPO
A value of total_count will be returned that will give the total files matching the extension provided for an org and repo.将返回一个 total_count 值,该值将给出与为 org 和 repo 提供的扩展名匹配的文件总数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.