Skip to content

Commit fa8da00

Browse files
authoredNov 6, 2024··
Update update_commit.py
1 parent 2e94d60 commit fa8da00

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed
 

‎src/update_commit.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
import requests
22
import sys
33
import re
4-
from datetime import datetime
4+
5+
# GitHub API to fetch the latest commit
6+
GITHUB_API_URL = "https://api.github.com"
57

68
if __name__ == "__main__":
79
assert(len(sys.argv) == 4)
810
repository = sys.argv[1]
911
token = sys.argv[2]
1012
readme_path = sys.argv[3]
1113

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
1920

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

2331
# Prepare the badge URL and commit link to update README
2432
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
33+
badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})" # This makes the badge link to the solution
2634

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
2836
with open(readme_path, "r") as readme:
2937
content = readme.read()
3038

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

0 commit comments

Comments
 (0)
Please sign in to comment.