Search code examples
github-pagesgithub-apigitpythonpygithub

How to get number of lines of code of a file in a remote repo using PyGithub/ Githubsearch api?


commit = repo.get_commit(sha="0adf369fda5c2d4231881d66e3bc0bd12fb86c9a")
print(commit.stats.total)
i = commit.files[0].filename

I can get the filename, even the file sha; but can't seem to get loc of the file. Any pointers?


Solution

  • So let's see this line

    commit = repo.get_commit(sha="0adf369fda5c2d4231881d66e3bc0bd12fb86c9a")
    

    Here the commit is of type github.Commit.Commit

    Now when you pick a file, it's of the type github.File.File

    If you checked that, you'll see that there is no real way of getting lines of code directly. But there is one important field raw_url.

    This will give you the raw_url of the file, which you can now get, perhaps like

    url = commit.files[0].raw_url
    r = requests.get(url)
    r.text
    

    This will give you the raw data of the file and you can use it to get the number of lines of code.