-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_readme.py
More file actions
175 lines (143 loc) · 6.23 KB
/
update_readme.py
File metadata and controls
175 lines (143 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import requests
import json
from datetime import datetime
import re
from collections import defaultdict
# Set variables
date_str = datetime.now().strftime('%B %-d, %Y')
repo_url = "https://github.com/ContextLab/leetcode-solutions"
readme_file = "README.md"
leetcode_api_url = "https://leetcode.com/graphql"
daily_challenge_query = {
"query": """query questionOfToday {
activeDailyCodingChallengeQuestion {
date
link
question {
questionFrontendId
title
titleSlug
difficulty
}
}
}""",
"operationName": "questionOfToday"
}
def parse_date(date_str):
"""Parse date string and return datetime object and (year, month) tuple."""
try:
dt = datetime.strptime(date_str, '%B %d, %Y')
return dt, (dt.year, dt.month)
except:
return None, None
def format_month_year(year, month):
"""Format month and year as 'Month, Year'."""
return datetime(year, month, 1).strftime('%B, %Y')
def parse_table_entries(lines):
"""Parse existing table entries and group them by month."""
entries_by_month = defaultdict(list)
table_pattern = re.compile(r'^\| (.+?) \| (.+?) \| (.+?) \| (.+?) \|')
for line in lines:
line = line.strip()
if not line or line.startswith('|---') or line.startswith('| 📆'):
continue
match = table_pattern.match(line)
if match:
date_str = match.group(1).strip()
dt, month_key = parse_date(date_str)
if month_key:
entries_by_month[month_key].append(line)
return entries_by_month
def generate_collapsible_sections(entries_by_month, current_month_key):
"""
Generate HTML collapsible sections for each month.
Behavior:
- Months are displayed in reverse chronological order (newest first)
- Only the current month is expanded (has 'open' attribute)
- On the first day of a new month, the previous month automatically closes
- Entries within each month remain in chronological order
"""
sections = []
# Sort months in descending order (most recent first)
sorted_months = sorted(entries_by_month.keys(), reverse=True)
table_header = """| 📆 Date | ⚙️ Problem | 📝 Link to notes | 🚦 Difficulty |
|--------------|-------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|------------|"""
for month_key in sorted_months:
year, month = month_key
month_name = format_month_year(year, month)
entries = entries_by_month[month_key]
# Only the current month should be open
open_attr = ' open' if month_key == current_month_key else ''
sections.append(f'<details{open_attr}>')
sections.append(f'<summary><b>Problems from {month_name}</b></summary>')
sections.append('')
sections.append(table_header)
for entry in entries:
sections.append(entry)
sections.append('')
sections.append('</details>')
sections.append('')
return '\n'.join(sections)
# Fetch daily challenge details from API
response = requests.post(leetcode_api_url, json=daily_challenge_query)
data = response.json()
# Debug: Print the entire response to check its structure
print(json.dumps(data, indent=2))
# Check if the expected data is present in the API response
if 'data' in data and 'activeDailyCodingChallengeQuestion' in data['data']:
problem = data['data']['activeDailyCodingChallengeQuestion']['question']
problem_id = problem['questionFrontendId']
title = problem['title']
title_slug = problem['titleSlug']
link = f"https://leetcode.com/problems/{title_slug}/description/?envType=daily-question"
difficulty = problem['difficulty']
difficulty_icon = "🟢" if difficulty == "Easy" else "🟡" if difficulty == "Medium" else "🔴"
note_link = f"{repo_url}/tree/main/problems/{problem_id}"
# Print the fetched problem details for debugging
print(f"Problem ID: {problem_id}")
print(f"Title: {title}")
print(f"Title Slug: {title_slug}")
print(f"Link: {link}")
print(f"Difficulty: {difficulty}")
# Create the new entry
new_entry = f"| {date_str} | [{problem_id}]({link}) | [Click here]({note_link}) | {difficulty_icon} {difficulty} |"
# Parse current date to get month key
current_dt, current_month_key = parse_date(date_str)
# Read the entire README file
with open(readme_file, 'r') as file:
lines = file.readlines()
# Initialize variables to store parts of the README
before_table = []
table_content = []
after_table = []
in_table_section = False
# Process the README line by line
for line in lines:
if "Problems we've attempted so far:" in line:
in_table_section = True
before_table.append(line)
elif "# Join our discussion!" in line:
in_table_section = False
after_table.append(line)
elif in_table_section:
table_content.append(line)
else:
if table_content:
after_table.append(line)
else:
before_table.append(line)
# Parse existing table entries
entries_by_month = parse_table_entries(table_content)
# Add the new entry to the appropriate month
if current_month_key:
entries_by_month[current_month_key].append(new_entry)
# Generate collapsible sections
formatted_table = generate_collapsible_sections(entries_by_month, current_month_key)
# Rebuild the README with collapsible sections
updated_readme = ''.join(before_table) + '\n' + formatted_table + '\n' + ''.join(after_table)
# Overwrite the README file with the updated content
with open(readme_file, 'w') as file:
file.write(updated_readme)
print("README file updated successfully with collapsible month sections!")
else:
print("Error: Unexpected response structure.")