-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Updated fetch_github_events function to use pagination and authentic…
…ation token, modified calculate_streaks function to filter PushEvents and calculate current streak, and updated main.py to use the new function signature and generate SVG with updated contribution data."
- Loading branch information
1 parent
320e23c
commit c2fc69e
Showing
3 changed files
with
40 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
import requests | ||
from datetime import datetime | ||
|
||
def fetch_github_events(username): | ||
response = requests.get(f'https://api.github.com/users/{username}/events') | ||
return response.json() | ||
def fetch_github_events(username, token): | ||
events = [] | ||
page = 1 | ||
while True: | ||
url = f'https://api.github.com/users/{username}/events?page={page}&per_page=100' | ||
headers = {'Authorization': f'token {token}'} | ||
response = requests.get(url, headers=headers) | ||
if response.status_code != 200: | ||
break | ||
page_events = response.json() | ||
if not page_events: | ||
break | ||
events.extend(page_events) | ||
page += 1 | ||
return events | ||
|
||
def calculate_streaks(events): | ||
dates = [datetime.strptime(event['created_at'], '%Y-%m-%dT%H:%M:%SZ').date() for event in events] | ||
current_streak = longest_streak = 0 | ||
dates = [datetime.strptime(event['created_at'], '%Y-%m-%dT%H:%M:%SZ').date() for event in events if event['type'] == 'PushEvent'] | ||
if not dates: | ||
return 0, 0, 0 | ||
|
||
dates = sorted(set(dates)) # Sort and remove duplicates | ||
total_contributions = len(dates) | ||
|
||
if dates: | ||
dates.sort(reverse=True) | ||
current_streak = 1 | ||
longest_streak = 1 | ||
streak = 1 | ||
|
||
for i in range(1, len(dates)): | ||
if (dates[i - 1] - dates[i]).days == 1: | ||
streak += 1 | ||
current_streak = max(current_streak, streak) | ||
else: | ||
streak = 1 | ||
longest_streak = max(longest_streak, streak) | ||
current_streak = 1 | ||
longest_streak = 1 | ||
streak = 1 | ||
|
||
for i in range(1, len(dates)): | ||
if (dates[i] - dates[i - 1]).days == 1: | ||
streak += 1 | ||
else: | ||
streak = 1 | ||
longest_streak = max(longest_streak, streak) | ||
|
||
# Calculate current streak | ||
if (datetime.now().date() - dates[-1]).days == 0: | ||
current_streak = streak | ||
else: | ||
current_streak = 0 | ||
|
||
return total_contributions, current_streak, longest_streak |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.