-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.py
87 lines (63 loc) · 1.7 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
import os
from dotenv import load_dotenv
load_dotenv()
username = "madhav-mknc"
access_token = os.getenv("GITHUB_TOKEN")
api_url = 'https://api.github.com/graphql'
query = """
query {
user(login: "%s") {
login
contributionsCollection {
contributionCalendar {
totalContributions
weeks {
contributionDays {
date
contributionCount
}
}
}
}
}
}
""" % username
variables = {
'login': username,
'after': None
}
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
all_contribution_data = []
def fetch():
global all_contribution_data
global variables
while True:
response = requests.post(api_url, json={'query': query, 'variables': variables}, headers=headers)
data = response.json()
print(len(data))
# for i in data:
# print(i, data[i])
# if 'errors' in data:
# raise Exception(data['errors'][0]['message'])
contribution_data = data['data']['user']['contributionsCollection']
weeks = contribution_data['contributionCalendar']['weeks']
for week in weeks:
days = week['contributionDays']
for day in days:
all_contribution_data.append(day)
if not weeks or len(weeks) < 52:
break
variables['after'] = weeks[-1]['contributionDays'][-1]['date']
try:
fetch()
except KeyboardInterrupt:
pass
# printing
all_contribution_data = sorted(all_contribution_data, key=lambda x: x['date'])
for i in all_contribution_data:
print(f"date: {i['date']}\tcontributions: {i['contributionCount']}")