Skip to content

Commit afe1732

Browse files
authored
Update update_commit.py
1 parent 7e3cf29 commit afe1732

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/update_commit.py

+34-16
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,52 @@
99
token = sys.argv[2]
1010
readme_path = sys.argv[3]
1111

12+
headers = {
13+
"Authorization": f"token {token}"
14+
}
15+
1216
# Get the current date
1317
today = datetime.today()
1418
day_of_month = today.strftime("%d") # Get current day of the month (e.g., 06)
1519
month = today.strftime("%b") # Get current month (e.g., Nov)
1620

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)
1945

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}"
2248

23-
# Prepare the badge URL and commit link to update README
49+
# Prepare the badge URL with the correct link to today's solution
2450
badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue"
25-
badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})" # This makes the badge link to the solution for today
51+
badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})"
2652

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
2854
with open(readme_path, "r") as readme:
2955
content = readme.read()
3056

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
4058
content = re.sub(
4159
r"(?<=<!--START_SECTION:potd-badge-->).*?(?=<!--END_SECTION:potd-badge-->)",
4260
f"\n{badge_link}\n",

0 commit comments

Comments
 (0)