Maik Lowrey
Maik Lowrey

Reputation: 17556

How do I get the programming languages of the user from the GitHub Api?

I would like to get the programming languages of a GitHub user from the GitHub Api. I haven't found an endpoint for this in the documentation. How can I do this?

Expected result

PHP 33%,
Dockerfile 16%,
JavaScript 20%,
etc.

Important Note to my question

The question should actually be: Does an endpoint exist for this? Because if not, I have to solve it programmatically. Then I would probably know how to do it...

Upvotes: 2

Views: 3750

Answers (2)

pjeff61
pjeff61

Reputation: 11

Here is how I pull the languages from multiple repos.

I used javascript/jquery, the octokit package

Documenation here: https://octokit.github.io/rest.js/v18

I pulled all repos and put them into a const so I could pull any data from any repository I was the direct owner of:

const repositories = await octokit.request('GET /user/repos?page=1&per_page=1000', { type: 'owner' });

Then pulled languages from specific repo and assigned it to const languages

// returns languages of specific repository in bytes - 1 byte is enough to hold about 1 typed character, e.g. 'b' or 'X' or '$'
const languages = await octokit.request('GET /repos/{owner}/{repo}/languages', { owner: 'jpatterson933', repo: "resume" });

and it returns this as the response when you console.log(res):

console.log(languages)

//returns this in the console

{HTML: 6869, CSS: 5123, JavaScript: 2958}

So from here, you can add the total bytes of each language together and get a total which you can then use to create "percentages of languages I have ever used on Github" or use the individual stats to make cool graphs of languages used in individual repositories and show something like "percentages of each language used in x repo"

Upvotes: 1

Maik Lowrey
Maik Lowrey

Reputation: 17556

I did some more research. GitHub does not offer an endpoint for this particular case. Therefore, the solution is to solve it programmatically.

Here are the steps how to do it:

https://api.github.com/users/JohnDoe/repos returns a json with all repositories of the user (JohnDoe). Iterate these objects and collect the language. The rest is just simple percentage calculation.

Upvotes: 3

Related Questions