Skip to content

Commit 437cddb

Browse files
authored
Update update_commit.py
1 parent 7964888 commit 437cddb

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

src/update_commit.py

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

12-
headers = {
13-
"Authorization": f"token {token}"
14-
}
15-
1612
# Get the current date
1713
today = datetime.today()
1814
day_of_month = today.strftime("%d") # Get current day of the month (e.g., 06)
1915
month = today.strftime("%b") # Get current month (e.g., Nov)
2016

21-
# Prepare the solution filename for today (e.g., 06(Nov) Root to leaf paths sum.md)
22-
today_solution_filename = f"{day_of_month}({month}) Root to leaf paths sum.md"
23-
24-
# Make a request to GitHub to list files in the directory
25-
dir_url = f"https://api.github.com/repos/{repository}/contents/{month}%202024%20GFG%20SOLUTION"
26-
response = requests.get(dir_url, headers=headers)
27-
28-
if not response.ok:
29-
print(f"Failed to fetch files: {response.status_code} {response.text}")
30-
sys.exit(1)
31-
32-
files = response.json()
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"
3319

34-
# Check if the directory exists or if there are any issues
35-
if isinstance(files, dict) and 'message' in files and files['message'] == 'Not Found':
36-
print(f"Directory not found: {dir_url}")
37-
sys.exit(1)
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}"
3822

39-
# Now, we try to find the solution file for today
40-
today_file = None
41-
for file in files:
42-
if file['name'].startswith(f"{day_of_month}({month})"):
43-
today_file = file['name']
44-
break
45-
46-
if not today_file:
47-
print(f"Solution file for {today_solution_filename} not found.")
48-
sys.exit(1)
49-
50-
# Prepare the badge URL with the correct link to today's solution
51-
solution_url = f"https://github.com/{repository}/blob/main/{month}%202024%20GFG%20SOLUTION/{today_file}"
23+
# Prepare the badge URL and commit link to update README
5224
badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue"
53-
badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})"
25+
badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})" # This makes the badge link to the solution for today
5426

55-
# Read the README file and update the sections for the badge
27+
# Read the README file and update the sections for commit and badge
5628
with open(readme_path, "r") as readme:
5729
content = readme.read()
5830

59-
# Update the badge with the dynamic link to today's solution
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
6040
content = re.sub(
6141
r"(?<=<!--START_SECTION:potd-badge-->).*?(?=<!--END_SECTION:potd-badge-->)",
6242
f"\n{badge_link}\n",

0 commit comments

Comments
 (0)