Created
September 27, 2025 22:09
-
-
Save gtracy/74f52c1c11937799103d014a98a44a12 to your computer and use it in GitHub Desktop.
Script that determines the number active days and total contributions in Github
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
| import requests | |
| import datetime | |
| # Replace with your details | |
| USERNAME = "FIXME" | |
| ACCESS_TOKEN = "FIXME" | |
| START_DATE = "2025-01-01" | |
| END_DATE = "2025-12-31" | |
| # Headers for authentication | |
| headers = { | |
| "Authorization": f"token {ACCESS_TOKEN}", | |
| "Accept": "application/vnd.github.v3+json" | |
| } | |
| def get_contributions_graphql(username, start_date, end_date): | |
| """Get the number of days with GitHub contributions using GraphQL API.""" | |
| try: | |
| # GraphQL query to get contribution data | |
| query = """ | |
| query($username: String!, $from: DateTime!, $to: DateTime!) { | |
| user(login: $username) { | |
| contributionsCollection(from: $from, to: $to) { | |
| contributionCalendar { | |
| totalContributions | |
| weeks { | |
| contributionDays { | |
| date | |
| contributionCount | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| """ | |
| # Convert dates to ISO format | |
| from_date = f"{start_date}T00:00:00Z" | |
| to_date = f"{end_date}T23:59:59Z" | |
| variables = { | |
| "username": username, | |
| "from": from_date, | |
| "to": to_date | |
| } | |
| response = requests.post( | |
| "https://api.github.com/graphql", | |
| headers=headers, | |
| json={"query": query, "variables": variables} | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| if "errors" in data: | |
| print(f"GraphQL errors: {data['errors']}") | |
| return 0 | |
| user_data = data.get("data", {}).get("user", {}) | |
| if not user_data: | |
| print("User not found") | |
| return 0 | |
| contributions = user_data.get("contributionsCollection", {}).get("contributionCalendar", {}) | |
| weeks = contributions.get("weeks", []) | |
| active_days = 0 | |
| total_contributions = contributions.get("totalContributions", 0) | |
| print(f"Total contributions: {total_contributions}") | |
| for week in weeks: | |
| for day in week.get("contributionDays", []): | |
| if day.get("contributionCount", 0) > 0: | |
| active_days += 1 | |
| return active_days | |
| else: | |
| print(f"GraphQL request failed: {response.status_code}") | |
| return 0 | |
| except Exception as e: | |
| print(f"Error with GraphQL API: {e}") | |
| return 0 | |
| if __name__ == "__main__": | |
| print("=" * 60) | |
| print(f"GitHub Activity Analysis for {USERNAME}") | |
| print(f"Date Range: {START_DATE} to {END_DATE}") | |
| print("=" * 60) | |
| # Get contributions using GraphQL API | |
| active_days = get_contributions_graphql(USERNAME, START_DATE, END_DATE) | |
| print("\n" + "=" * 60) | |
| print(f"Result: {active_days} active days with GitHub contributions") | |
| print("=" * 60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment