Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the total amount of contributors to a GitHub repository?

How can I get the total amount of contributors of a GitHub repository? The API makes it quite difficult because of the pagination.

This is what I tried so far using Python:

contributors = "https://api.github.com/repos/JetBrains/kotlin-web-site/contributors"
x = requests.get(contributors)
y = json.loads(x.text)
len(y) # maximum 30 because of pagination
like image 502
Max Avatar asked Nov 22 '18 12:11

Max


1 Answers

As a last resort you can scrape required value from GitHub HTML page (lxml.html lib required):

import requests
from lxml import html

r = requests.get('https://github.com/JetBrains/kotlin-web-site')
xpath = '//span[contains(@class, "num") and following-sibling::text()[normalize-space()="contributors"]]/text()'
contributors_number = int(html.fromstring(r.text).xpath(xpath)[0].strip())
print(contributors_number)
# 338
like image 155
Andersson Avatar answered Oct 14 '22 22:10

Andersson