-
Notifications
You must be signed in to change notification settings - Fork 2
/
all_contributions.py
144 lines (106 loc) · 3.21 KB
/
all_contributions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import requests
import os
from dotenv import load_dotenv
load_dotenv()
# username = "madhav-mknc"
username = 'AryanGodara'
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'])
# Sort the data based on 'date' key
all_contribution_data = sorted(all_contribution_data, key=lambda x: x['date'])
# Create a set to store unique dates
unique_dates = set()
# List to store unique contributions (without duplicates)
unique_contributions = []
# Iterate through the sorted data and add only unique dates to the list
for contribution in all_contribution_data:
date = contribution['date']
if date not in unique_dates:
unique_dates.add(date)
unique_contributions.append(contribution)
# Now, 'unique_contributions' contains only unique contributions based on the 'date' key
data = unique_contributions
streaks = []
current_streak = 0
for day in data:
if day['contributionCount'] > 0:
current_streak += 1
else:
current_streak = 0
streaks.append({
"date": day['date'],
"streak": current_streak
})
print({
"date": day['date'],
"streak": current_streak
})
# import matplotlib.pyplot as plt
# from datetime import datetime
# # Extract date and streak values
# dates = [entry['date'] for entry in streaks]
# streak_values = [entry['streak'] for entry in streaks]
# # Extract and format month names
# month_names = [datetime.strptime(date, '%Y-%m-%d').strftime('%b %Y') for date in dates]
# # Create a list to store unique month names
# unique_month_names = []
# unique_dates = []
# # Loop through dates to get unique month names
# for date, month_name in zip(dates, month_names):
# if month_name not in unique_month_names:
# unique_month_names.append(month_name)
# unique_dates.append(date)