|
9 | 9 | token = sys.argv[2]
|
10 | 10 | readme_path = sys.argv[3]
|
11 | 11 |
|
| 12 | + headers = { |
| 13 | + "Authorization": f"token {token}" |
| 14 | + } |
| 15 | + |
12 | 16 | # Get the current date
|
13 | 17 | today = datetime.today()
|
14 | 18 | day_of_month = today.strftime("%d") # Get current day of the month (e.g., 06)
|
15 | 19 | month = today.strftime("%b") # Get current month (e.g., Nov)
|
16 | 20 |
|
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" |
| 21 | + # Generate the filename based on the day of the month (e.g., 06(Nov) Root to leaf paths sum.md) |
| 22 | + # The name structure might differ but should follow a predictable pattern |
| 23 | + today_solution_filename = f"{day_of_month}({month})" # The pattern that matches the filename format. |
| 24 | + |
| 25 | + # Make a request to GitHub to list files in the directory |
| 26 | + dir_url = f"https://api.github.com/repos/{repository}/contents/{month}%202024%20GFG%20SOLUTION" |
| 27 | + response = requests.get(dir_url, headers=headers) |
| 28 | + |
| 29 | + if not response.ok: |
| 30 | + print(f"Failed to fetch files: {response.status_code} {response.text}") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + files = response.json() |
| 34 | + |
| 35 | + # Search for the correct file based on the date |
| 36 | + today_file = None |
| 37 | + for file in files: |
| 38 | + if file['name'].startswith(today_solution_filename): |
| 39 | + today_file = file['name'] |
| 40 | + break |
| 41 | + |
| 42 | + if not today_file: |
| 43 | + print(f"No solution file found for {today_solution_filename}. Please check the directory.") |
| 44 | + sys.exit(1) |
19 | 45 |
|
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}" |
| 46 | + # Prepare the correct file URL based on the found filename |
| 47 | + solution_url = f"https://github.com/{repository}/blob/main/{month}%202024%20GFG%20SOLUTION/{today_file}" |
22 | 48 |
|
23 |
| - # Prepare the badge URL and commit link to update README |
| 49 | + # Prepare the badge URL with the correct link to today's solution |
24 | 50 | 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 |
| 51 | + badge_link = f"[]({solution_url})" |
26 | 52 |
|
27 |
| - # Read the README file and update the sections for commit and badge |
| 53 | + # Read the README file and update the sections for the badge |
28 | 54 | with open(readme_path, "r") as readme:
|
29 | 55 | content = readme.read()
|
30 | 56 |
|
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 |
| 57 | + # Update the badge with the dynamic link to today's solution |
40 | 58 | content = re.sub(
|
41 | 59 | r"(?<=<!--START_SECTION:potd-badge-->).*?(?=<!--END_SECTION:potd-badge-->)",
|
42 | 60 | f"\n{badge_link}\n",
|
|
0 commit comments