Skip to content

Commit 6e7123b

Browse files
authored
Update update_solution.py
1 parent ad0b82f commit 6e7123b

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

src/update_solution.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
1-
import requests
21
import sys
32
import re
3+
import requests
44

5-
def get_latest_commit(repo):
5+
def fetch_latest_commit(repo, token, file_pattern):
6+
headers = {"Authorization": f"token {token}"}
67
url = f"https://api.github.com/repos/{repo}/commits"
7-
response = requests.get(url)
8-
if response.status_code != 200:
9-
raise Exception(f"Failed to fetch commits: {response.status_code}, {response.text}")
10-
latest_commit = response.json()[0]
11-
commit_url = latest_commit["html_url"]
12-
commit_message = latest_commit["commit"]["message"]
13-
return commit_url, commit_message
8+
params = {"path": file_pattern, "per_page": 1}
9+
response = requests.get(url, headers=headers, params=params)
10+
11+
if response.ok and response.json():
12+
commit_data = response.json()[0]
13+
commit_url = commit_data["html_url"]
14+
return commit_url
15+
else:
16+
print("Error fetching commit data:", response.status_code, response.text)
17+
sys.exit(1)
1418

15-
def update_readme(readme_path, commit_url, commit_message):
19+
def update_readme(readme_path, commit_url):
20+
badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue"
21+
badge_link = f"[![Today's POTD Solution]({badge_url})]({commit_url})"
22+
1623
with open(readme_path, "r") as file:
1724
content = file.read()
18-
19-
# Construct badge and link
20-
badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue"
21-
badge_link = f"[![Today's POTD Solution]({badge_url})](https://github.com/Hunterdii/GeeksforGeeks-POTD)"
22-
23-
# Update the sections in the README
24-
updated_content = re.sub(
25-
r"(?<=<!\-\-START_SECTION:latest\-commit\-\->)[\s\S]*(?=<!\-\-END_SECTION:latest\-commit\-\->)",
25+
26+
new_content = re.sub(
27+
r"(?<=<!--START_SECTION:latest-commit-->)[\s\S]*(?=<!--END_SECTION:latest-commit-->)",
2628
f"\n{commit_url}\n",
2729
content
2830
)
29-
updated_content = re.sub(
30-
r"(?<=<!\-\-START_SECTION:potd\-badge\-\->)[\s\S]*(?=<!\-\-END_SECTION:potd\-badge\-\->)",
31+
32+
new_content = re.sub(
33+
r"(?<=<!--START_SECTION:potd-badge-->)[\s\S]*(?=<!--END_SECTION:potd-badge-->)",
3134
f"\n{badge_link}\n",
32-
updated_content
35+
new_content
3336
)
3437

3538
with open(readme_path, "w") as file:
36-
file.write(updated_content)
39+
file.write(new_content)
3740

3841
if __name__ == "__main__":
3942
repo = sys.argv[1]
40-
readme_path = sys.argv[2]
43+
token = sys.argv[2]
44+
readme_path = sys.argv[3]
45+
file_pattern = sys.argv[4] # Example pattern: "01(Nov)question name.md"
4146

42-
commit_url, commit_message = get_latest_commit(repo)
43-
update_readme(readme_path, commit_url, commit_message)
47+
commit_url = fetch_latest_commit(repo, token, file_pattern)
48+
update_readme(readme_path, commit_url)

0 commit comments

Comments
 (0)