mine-hyprdots/waybar/scripts/weekly_commits
2025-06-07 14:47:24 +05:30

132 lines
No EOL
3.5 KiB
Python
Executable file

#!/usr/bin/env python3
import requests
import datetime
import json
from collections import OrderedDict
import os
from dotenv import load_dotenv
load_dotenv()
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME")
GITHUB_PAT = os.getenv("GITHUB_PAT")
today = datetime.date.today()
start_of_week = today - datetime.timedelta(days=(today.weekday() + 1) % 7) # Start from Sunday
from_date = start_of_week.isoformat() + "T00:00:00Z"
to_date = today.isoformat() + "T23:59:59Z"
query = """
query($user: String!, $from: DateTime!, $to: DateTime!) {
user(login: $user) {
contributionsCollection(from: $from, to: $to) {
contributionCalendar {
weeks {
contributionDays {
date
contributionCount
weekday
}
}
}
}
}
}
"""
variables = {
"user": GITHUB_USERNAME,
"from": from_date,
"to": to_date
}
headers = {
"Authorization": f"Bearer {GITHUB_PAT}",
"Accept": "application/vnd.github.v4.idl"
}
COLOR_SCHEME = {
0: "#ebedf0", # No contributions
1: "#9be9a8", # 1-3 commits
2: "#40c463", # 4-6 commits
3: "#30a14e", # 7-9 commits
4: "#216e39" # 10+ commits
}
def get_color(count):
"""Get color based on commit count"""
if count == 0:
return COLOR_SCHEME[0]
elif 1 <= count <= 3:
return COLOR_SCHEME[1]
elif 4 <= count <= 6:
return COLOR_SCHEME[2]
elif 7 <= count <= 9:
return COLOR_SCHEME[3]
else:
return COLOR_SCHEME[4]
def get_weekday_name(weekday):
"""Convert weekday number to name"""
return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][weekday]
try:
response = requests.post(
"https://api.github.com/graphql",
json={"query": query, "variables": variables},
headers=headers,
timeout=10
)
response.raise_for_status()
data = response.json()
weeks = data["data"]["user"]["contributionsCollection"]["contributionCalendar"]["weeks"]
contributions = OrderedDict()
for i in range(7):
day = start_of_week + datetime.timedelta(days=i)
contributions[day] = 0
for week in weeks:
for day in week["contributionDays"]:
date = datetime.date.fromisoformat(day["date"])
if start_of_week <= date <= today:
contributions[date] = day["contributionCount"]
dots = []
tooltip_lines = []
total_contributions = 0
for date, count in contributions.items():
color = get_color(count)
dots.append(f'<span foreground="{color}">■</span>')
weekday = get_weekday_name(date.weekday())
# Format: "Sun (5/1): 4 commits"
tooltip_lines.append(f"{weekday} ({date.day}/{date.month}): {count} contribution{'s' if count != 1 else ''}")
total_contributions += count
output = {
"text": " ".join(dots),
"tooltip": "\n".join([f"GitHub Contributions ({start_of_week.strftime('%d/%m')} to {today.strftime('%d/%m')})",
f"Total: {total_contributions}"] + tooltip_lines),
"class": "github-contributions",
"alt": "GitHub Contributions"
}
except requests.exceptions.RequestException as e:
output = {
"text": "",
"tooltip": f"GitHub API error: {str(e)}",
"class": "error",
"alt": "GitHub Error"
}
except Exception as e:
output = {
"text": "",
"tooltip": f"Error: {str(e)}",
"class": "error",
"alt": "Error"
}
print(json.dumps(output))