Last active
January 29, 2022 08:09
-
-
Save NachoToast/e315015030bebf566d16ebdec9fee18f to your computer and use it in GitHub Desktop.
Gets a GitHub user's contributions using the graphql API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from filecmp import cmp | |
from requests import post | |
from json import loads | |
from datetime import datetime | |
# generate your token at https://github.com/settings/tokens (i'm not sure what scopes you will need) | |
token = 'api_token' | |
username = 'NachoToast' # doesn't have to be your username | |
url = 'https://api.github.com/graphql' | |
body = r'{"query":"query {user(login:\"' + username + \ | |
r'\"){name\ncontributionsCollection{contributionCalendar {colors\ntota' + \ | |
r'lContributions\nweeks {contributionDays {color\ncontributionCount\nd' + \ | |
r'ate\nweekday}firstDay}}}}}"}' | |
headers = { | |
'Authorization': f'bearer {token}' | |
} | |
# making the request | |
obj: list[dict] = loads(post(url, data=body, headers=headers).content)[ | |
'data']['user']['contributionsCollection']['contributionCalendar']['weeks'] | |
dayDict = {} | |
for num in obj: | |
for data in num['contributionDays']: | |
date = data['date'] | |
contributions = data['contributionCount'] | |
# uncomment these lines if you don't want to include days with 0 activity | |
# if (contributions == 0): | |
# continue | |
if (date in dayDict): | |
dayDict[date] += contributions | |
else: | |
dayDict[date] = contributions | |
def timestamp(a: str): | |
year, month, day = [int(i) for i in a.split('-')] | |
return datetime(year, month, day).timestamp() | |
# make a list of all the dates recorded, sorted by most recent first | |
sortedKeys = sorted(dayDict.keys(), key=timestamp) | |
# example output | |
for key in sortedKeys: | |
print(f'on {key} {username} made {dayDict[key]} contributions') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment