Skip to content

Commit

Permalink
"Updated fetch_github_events function to use pagination and authentic…
Browse files Browse the repository at this point in the history
…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
Vikranth3140 committed Jun 8, 2024
1 parent 320e23c commit c2fc69e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
54 changes: 36 additions & 18 deletions fetch_contributions.py
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
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import os

if __name__ == '__main__':
username = 'Vikranth3140'
username = 'your-username'
token = 'your-github-token' # Generate a GitHub token and use it here

# Ensure the output directory exists
if not os.path.exists('output'):
os.makedirs('output')

events = fetch_github_events(username)
events = fetch_github_events(username, token)
total_contributions, current_streak, longest_streak = calculate_streaks(events)
generate_svg(total_contributions, current_streak, longest_streak)
2 changes: 1 addition & 1 deletion output/contributions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c2fc69e

Please sign in to comment.