11import os
22import re
3- from pathlib import Path
43import requests
4+ from pathlib import Path
55
66SRC_DIR = "Solutions"
77SITE_README_PATH = Path ("Site_README.md" )
1010LINK_ICON = "🔗" # Unicode link icon
1111
1212def fetch_problem_data (slug : str ):
13- """Fetch difficulty, tags , and real problem number from LeetCode GraphQL API."""
13+ """Fetch problem number, difficulty , and tags from LeetCode GraphQL API."""
1414 url = "https://leetcode.com/graphql"
1515 query = {
1616 "query" : """
@@ -24,28 +24,31 @@ def fetch_problem_data(slug: str):
2424 """ ,
2525 "variables" : {"titleSlug" : slug },
2626 }
27+ headers = {
28+ "Content-Type" : "application/json" ,
29+ "User-Agent" : "Mozilla/5.0"
30+ }
2731 try :
28- resp = requests .post (url , json = query , timeout = 10 )
32+ resp = requests .post (url , json = query , headers = headers , timeout = 10 )
2933 resp .raise_for_status ()
30- data = resp .json ()
31- question = data .get ("data" , {}).get ("question" , {})
32- number = question .get ("frontendQuestionId" , "" )
33- difficulty = question .get ("difficulty" , "" )
34- tags = [t ['name' ] for t in question .get ("topicTags" , [])]
34+ data = resp .json ()["data" ]["question" ]
35+ number = data .get ("frontendQuestionId" , "" )
36+ difficulty = data .get ("difficulty" , "" )
37+ tags = [t ["name" ] for t in data .get ("topicTags" , [])]
3538 return number , difficulty , tags
3639 except Exception as e :
3740 print (f"Failed to fetch { slug } : { e } " )
3841 return "" , "" , []
3942
40- # MkDocs badge ( HTML)
43+ # MkDocs HTML badge
4144def difficulty_badge_site (difficulty : str ) -> str :
4245 colors = {"Easy" : "#46c6c2" , "Medium" : "#fac31d" , "Hard" : "#f8615c" }
43- color = colors .get (difficulty , "#9E9E9E" )
4446 if not difficulty :
4547 return ""
48+ color = colors .get (difficulty , "#9E9E9E" )
4649 return f'<span style="background-color:#ffffff1a; color:{ color } ; padding:2px 6px; border-radius:6px;">{ difficulty } </span>'
4750
48- # GitHub badge ( Shields.io)
51+ # GitHub Shields.io badge
4952def difficulty_badge_github (difficulty : str ) -> str :
5053 colors = {"Easy" : "4c1" , "Medium" : "f9c851" , "Hard" : "e05d44" }
5154 if not difficulty :
@@ -54,16 +57,13 @@ def difficulty_badge_github(difficulty: str) -> str:
5457 badge_url = f"https://img.shields.io/badge/Difficulty-{ difficulty } -{ color } .svg"
5558 return f""
5659
57- def generate_table_rows (cached_data , badge_func ):
60+ def generate_table_rows (problems , badge_func ):
5861 rows = []
59- for slug , real_number , title_slug in cached_data :
60- difficulty , tags = cached_data [(slug , real_number )]
61- title = f"{ real_number } . " + ' ' .join ([w .capitalize () for w in title_slug .split ('-' )])
62+ for slug , number , title , difficulty , tags in problems :
6263 diff_badge = badge_func (difficulty )
63- tags_str = ', ' .join (tags )
64- solution_link = f"https://leetcode.romitsagu.com/solutions/leetcode{ real_number } #/"
64+ tags_str = ", " .join (tags )
65+ solution_link = f"https://leetcode.romitsagu.com/solutions/leetcode{ number } #/"
6566 icon_md = f"[{ LINK_ICON } ]({ solution_link } )"
66-
6767 rows .append (f"| { title } | { icon_md } | { diff_badge } | { tags_str } |" )
6868 return "\n " .join (rows )
6969
@@ -93,32 +93,30 @@ def update_readme(readme_path, rows_str):
9393 print (f"{ readme_path .name } updated successfully." )
9494
9595def main ():
96- cached_data = []
97- api_cache = {}
96+ problems = []
9897
99- # Fetch real number, difficulty, and tags for each problem
98+ # Step 1: Read all problem folders and fetch data
10099 for folder in sorted (os .listdir (SRC_DIR )):
101100 folder_path = Path (SRC_DIR ) / folder
102101 if not folder_path .is_dir ():
103102 continue
103+
104104 match = re .match (r"(\d+)-(.+)" , folder )
105105 if not match :
106106 continue
107- _ , slug = match .groups ()
108-
109- if slug not in api_cache :
110- real_number , difficulty , tags = fetch_problem_data (slug )
111- api_cache [slug ] = (difficulty , tags )
112- else :
113- real_number , _ , _ = fetch_problem_data (slug )
107+ folder_number , slug = match .groups ()
108+ folder_number = str (int (folder_number ))
109+ title = f"{ folder_number } . " + " " .join ([w .capitalize () for w in slug .split ("-" )])
114110
115- cached_data .append ((slug , real_number , slug ))
111+ # Fetch API data once
112+ number , difficulty , tags = fetch_problem_data (slug )
113+ problems .append ((slug , number , title , difficulty , tags ))
116114
117- # Generate rows for both READMEs
118- site_rows = generate_table_rows (api_cache , difficulty_badge_site )
119- github_rows = generate_table_rows (api_cache , difficulty_badge_github )
115+ # Step 2: Generate table rows
116+ site_rows = generate_table_rows (problems , difficulty_badge_site )
117+ github_rows = generate_table_rows (problems , difficulty_badge_github )
120118
121- # Update both READMEs
119+ # Step 3: Update both READMEs
122120 update_readme (SITE_README_PATH , site_rows )
123121 update_readme (GITHUB_README_PATH , github_rows )
124122
0 commit comments