|
import csv |
|
import json |
|
import os |
|
import sys |
|
|
|
import requests |
|
import requests_ratelimiter |
|
import tqdm |
|
|
|
|
|
def get_commits(session, repo, path): |
|
query = {"path": path} |
|
headers = { |
|
"Accept": "application/vnd.github+json", |
|
"Authorization": "Bearer " + os.environ["GITHUB_TOKEN"], |
|
"X-GitHub-Api-Version": "2022-11-28", |
|
} |
|
|
|
try: |
|
r = session.get( |
|
"https://api.github.com/repos/" + repo + "/commits", |
|
params=query, |
|
headers=headers, |
|
timeout=10, |
|
) |
|
except ( |
|
requests.exceptions.ConnectionError, |
|
requests.exceptions.ReadTimeout, |
|
): |
|
|
|
return None |
|
else: |
|
|
|
obj = r.json() |
|
if isinstance(obj, list): |
|
commits = [] |
|
for c in obj: |
|
try: |
|
commits.append( |
|
{"sha": c["sha"], "date": c["commit"]["committer"]["date"]} |
|
) |
|
except KeyError: |
|
pass |
|
return commits |
|
else: |
|
return None |
|
|
|
|
|
def main(): |
|
|
|
session = requests.Session() |
|
adapter = requests_ratelimiter.LimiterAdapter(per_second=2) |
|
session.mount("http://", adapter) |
|
session.mount("https://", adapter) |
|
|
|
with open("repos.csv", "r") as csvfile: |
|
|
|
reader = csv.DictReader(csvfile) |
|
rows = sum(1 for row in reader) |
|
csvfile.seek(0) |
|
|
|
reader = csv.DictReader(csvfile) |
|
for row in tqdm.tqdm(reader, total=rows): |
|
|
|
repo = row["repository"].split("/", maxsplit=1)[1] |
|
commits = get_commits(session, repo, row["path"]) |
|
|
|
|
|
if commits: |
|
obj = { |
|
"repository": repo, |
|
"path": row["path"], |
|
"repoStars": row["repoStars"], |
|
"repoLastFetched": row["repoLastFetched"], |
|
"commits": list(commits), |
|
} |
|
json.dump(obj, sys.stdout) |
|
sys.stdout.write("\n") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|