|
3 | 3 | import json |
4 | 4 | from github import Github |
5 | 5 | from dotenv import load_dotenv |
| 6 | + |
6 | 7 | # Access the API key |
7 | 8 | load_dotenv() |
8 | 9 | GITHUB_ACCESS_TOKEN = os.getenv('GITHUB_ACCESS_TOKEN') |
9 | 10 |
|
10 | | -# Set up the GitHub API client |
11 | | -g = Github(GITHUB_ACCESS_TOKEN) |
| 11 | +# Set up the GitHub API client with no caching |
| 12 | +g = Github(GITHUB_ACCESS_TOKEN, per_page=100) |
12 | 13 |
|
13 | 14 | # Set up the repository and file path |
14 | 15 | repo = g.get_repo("TobiasMue91/tobiasmue91.github.io") |
15 | 16 | file_path = "index.html" |
16 | 17 |
|
17 | | -# Fetch the commit history |
18 | | -commits = repo.get_commits(path=file_path) |
| 18 | +# Fetch the commit history for index.html |
| 19 | +index_commits = list(repo.get_commits(path=file_path, sha="main")) |
| 20 | + |
| 21 | +# Also fetch the latest commit from the repository (regardless of file) |
| 22 | +latest_commit = repo.get_commits()[0] # First commit is the latest |
| 23 | + |
| 24 | +# Print the date ranges to verify |
| 25 | +if index_commits: |
| 26 | + print(f"Oldest index.html commit: {index_commits[-1].commit.author.date}") |
| 27 | + print(f"Latest index.html commit: {index_commits[0].commit.author.date}") |
| 28 | + print(f"Total index.html commits found: {len(index_commits)}") |
| 29 | + |
| 30 | +print(f"Latest repository commit: {latest_commit.commit.author.date} (hash: {latest_commit.sha})") |
| 31 | + |
| 32 | +# Load existing data to preserve GptVersion values |
| 33 | +existing_data = {} |
| 34 | +try: |
| 35 | + with open("timeline_data.json", "r") as f: |
| 36 | + existing_entries = json.load(f) |
| 37 | + # Create a dictionary with commit hash as key for faster lookup |
| 38 | + for entry in existing_entries: |
| 39 | + existing_data[entry["hash"]] = entry |
| 40 | +except FileNotFoundError: |
| 41 | + pass # No existing file, that's OK |
19 | 42 |
|
20 | 43 | # Process the commits and generate the JSON data |
21 | 44 | timeline_data = [] |
22 | 45 | last_date = None |
23 | 46 |
|
24 | | -for commit in commits: |
| 47 | +for commit in index_commits: |
25 | 48 | commit_date = commit.commit.author.date.date() |
26 | 49 |
|
27 | 50 | if commit_date != last_date: |
28 | | - timeline_data.append({ |
| 51 | + # Create new entry |
| 52 | + new_entry = { |
29 | 53 | "hash": commit.sha, |
30 | 54 | "timestamp": commit.commit.author.date.isoformat(), |
31 | | - "GptVersion": None # This can be added manually later |
32 | | - }) |
| 55 | + "GptVersion": None # Default value |
| 56 | + } |
| 57 | + |
| 58 | + # If this hash exists in our existing data and has a GptVersion, preserve it |
| 59 | + if commit.sha in existing_data and existing_data[commit.sha].get("GptVersion"): |
| 60 | + new_entry["GptVersion"] = existing_data[commit.sha]["GptVersion"] |
| 61 | + |
| 62 | + timeline_data.append(new_entry) |
33 | 63 | last_date = commit_date |
34 | 64 |
|
| 65 | +# Check if today's date is already in the timeline |
| 66 | +today = datetime.now().date() |
| 67 | +has_today_entry = any(datetime.fromisoformat(entry["timestamp"]).date() == today for entry in timeline_data) |
| 68 | + |
| 69 | +# If not, add the latest commit from the repository |
| 70 | +if not has_today_entry: |
| 71 | + latest_entry = { |
| 72 | + "hash": latest_commit.sha, |
| 73 | + "timestamp": latest_commit.commit.author.date.isoformat(), |
| 74 | + "GptVersion": None # You can set this manually later |
| 75 | + } |
| 76 | + |
| 77 | + # If this hash exists in our existing data and has a GptVersion, preserve it |
| 78 | + if latest_commit.sha in existing_data and existing_data[latest_commit.sha].get("GptVersion"): |
| 79 | + latest_entry["GptVersion"] = existing_data[latest_commit.sha]["GptVersion"] |
| 80 | + |
| 81 | + print(f"Adding latest repository commit for today") |
| 82 | + timeline_data.insert(0, latest_entry) # Insert at the beginning (most recent) |
| 83 | + |
35 | 84 | # Save the JSON data to a file |
36 | 85 | with open("timeline_data.json", "w") as f: |
37 | 86 | json.dump(timeline_data, f, indent=2) |
|
0 commit comments