简体   繁体   中英

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. 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 '

But, is there any way I can check all the files with a particular extension and count. I have never used an api before.

This isn't easy if your GitHub repo has many folders and files.
You would use the Get Content API for a given repo:

GET /repos/:owner/:repo/contents/:path

You can call it in python, using a JSON library to decode the result: see this answer .

But it isn't a recursive function, so the result list the files and folders directly under :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. Documentation can be found here. Search API Docs

The URL below will show the typescript results for the Angular repository.

GitHub Search Code API → Angular Repo

An example query for this usage would be:

q=*+extensions: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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM