简体   繁体   English

Github API 如何使用 python 语言获取具有特定扩展名(.c、.cpp、.py 等)的 git 存储库的所有文件的计数?

[英]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.

相关问题 用于Python的Github API:PyGithub - 获取特定存储库的所有提交的列表 - Github API for Python : PyGithub - get a list of all commits for a particular repository 使用python函数计算扩展名为.py的文件的数量 - Count number of files with .py extension using python function 使用 Github API 获取所有成员与 Python - Using Github API to Get All Members with Python 如何使用Python通过GitHub API获取最新存储库? - How to get latest repository through GitHub API with Python? 如何读取Github存储库中的所有文本文件? - How to read all text files in Github repository? 从git仓库中删除并忽略所有具有扩展名的文件 - Remove and ignore all files that have an extension from a git repository 如何将 python 文件添加到 github 中的新存储库 - How to add python files to a new repository in github Python How to download repository zip file from GitHub using github api - Python How to download repository zip file from GitHub using github api 如何使用 python 在 git 存储库中获取当前分支名称 - How to get name current bramch name in a git repository using python 使用过滤器和地图在目录中找到扩展名为.txt和.py的所有文件(使用python) - Find all files in directory with extension .txt and .py with python using filter and map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM