|
1 | 1 | import requests
|
2 | 2 | import sys
|
3 | 3 | import re
|
4 |
| -from datetime import datetime |
| 4 | + |
| 5 | +# GitHub API to fetch the latest commit |
| 6 | +GITHUB_API_URL = "https://api.github.com" |
5 | 7 |
|
6 | 8 | if __name__ == "__main__":
|
7 | 9 | assert(len(sys.argv) == 4)
|
8 | 10 | repository = sys.argv[1]
|
9 | 11 | token = sys.argv[2]
|
10 | 12 | readme_path = sys.argv[3]
|
11 | 13 |
|
12 |
| - # Get the current date |
13 |
| - today = datetime.today() |
14 |
| - day_of_month = today.strftime("%d") # Get current day of the month (e.g., 06) |
15 |
| - month = today.strftime("%b") # Get current month (e.g., Nov) |
16 |
| - |
17 |
| - # Prepare the solution filename for today (e.g., 06(Nov)Root to leaf paths sum.md) |
18 |
| - today_solution_filename = f"{day_of_month}({month}){today.strftime('%A')}.md" |
| 14 | + # Fetch the latest commit information for the repository |
| 15 | + commits_url = f"{GITHUB_API_URL}/repos/{repository}/commits?per_page=1" |
| 16 | + headers = {"Authorization": f"token {token}"} |
| 17 | + response = requests.get(commits_url, headers=headers) |
| 18 | + response.raise_for_status() |
| 19 | + commit_data = response.json()[0] # Get the latest commit |
19 | 20 |
|
20 |
| - # Prepare the commit URL for today (we don't need to change this as it will be dynamically picked from the latest commit) |
21 |
| - solution_url = f"https://github.com/{repository}/blob/main/{month}%202024%20GFG%20SOLUTION/{today_solution_filename}" |
| 21 | + # Extract the latest commit's file name and URL |
| 22 | + commit_message = commit_data["commit"]["message"] |
| 23 | + commit_date = commit_data["commit"]["committer"]["date"] |
| 24 | + |
| 25 | + # Assume the file is in the `GFG SOLUTION` folder and match the file name based on the commit message (assuming this naming convention is consistent) |
| 26 | + solution_filename = commit_message.strip().replace(" ", "%20") + ".md" |
| 27 | + |
| 28 | + # Construct the solution URL |
| 29 | + solution_url = f"https://github.com/{repository}/blob/main/November%202024%20GFG%20SOLUTION/{solution_filename}" |
22 | 30 |
|
23 | 31 | # Prepare the badge URL and commit link to update README
|
24 | 32 | badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue"
|
25 |
| - badge_link = f"[]({solution_url})" # This makes the badge link to the solution for today |
| 33 | + badge_link = f"[]({solution_url})" # This makes the badge link to the solution |
26 | 34 |
|
27 |
| - # Read the README file and update the sections for commit and badge |
| 35 | + # Read the README file and update the sections for the commit and badge |
28 | 36 | with open(readme_path, "r") as readme:
|
29 | 37 | content = readme.read()
|
30 | 38 |
|
31 |
| - # Update today's solution link (for the commit) |
32 |
| - # content = re.sub( |
33 |
| - # r"(?<=<!--START_SECTION:latest-commit-->).*?(?=<!--END_SECTION:latest-commit-->)", |
34 |
| - # f"\n{solution_url}\n", |
35 |
| - # content, |
36 |
| - # flags=re.DOTALL |
37 |
| - # ) |
38 |
| - |
39 |
| - # Update badge link |
| 39 | + # Update the badge link in the README |
40 | 40 | content = re.sub(
|
41 | 41 | r"(?<=<!--START_SECTION:potd-badge-->).*?(?=<!--END_SECTION:potd-badge-->)",
|
42 | 42 | f"\n{badge_link}\n",
|
|
0 commit comments