update problems
(result)
| 347 | |
| 348 | |
| 349 | def refresh(result): |
| 350 | """update problems""" |
| 351 | pattern = re.compile('src="(.*?)"') |
| 352 | for question in result: |
| 353 | front_question_id = question["frontend_question_id"] |
| 354 | print(front_question_id) |
| 355 | paid_only = " 🔒" if question["paid_only"] else "" |
| 356 | |
| 357 | path_cn = unquote(str(question["relative_path_cn"]).replace("/solution", ".")) |
| 358 | path_en = unquote(str(question["relative_path_en"]).replace("/solution", ".")) |
| 359 | |
| 360 | try: |
| 361 | with open(path_cn, "r", encoding="utf-8") as f1: |
| 362 | cn_content = f1.read() |
| 363 | except Exception as e: |
| 364 | print(f"Failed to open {path_cn}: {e}") |
| 365 | continue |
| 366 | try: |
| 367 | with open(path_en, "r", encoding="utf-8") as f2: |
| 368 | en_content = f2.read() |
| 369 | except Exception as e: |
| 370 | print(f"Failed to open {path_en}: {e}") |
| 371 | continue |
| 372 | |
| 373 | category = question["category"] |
| 374 | |
| 375 | readme_template_cn, readme_template_en = select_templates(category) |
| 376 | rating_item = rating_dict.get(str(front_question_id)) |
| 377 | rating = int(rating_item.get("Rating", 0)) if rating_item else "" |
| 378 | source = ( |
| 379 | rating_item.get("ContestID_zh") + " " + rating_item.get("ProblemIndex") |
| 380 | if rating_item |
| 381 | else "" |
| 382 | ) |
| 383 | cat = category_dict.get(category, category) |
| 384 | cat = cat.title() if cat and cat[0].islower() else cat |
| 385 | metadata = { |
| 386 | "tags": question["tags_cn"] or [cat], |
| 387 | "difficulty": question["difficulty_cn"], |
| 388 | "rating": rating, |
| 389 | "comments": True, |
| 390 | "edit_url": f'https://github.com/doocs/leetcode/edit/main{question["relative_path_cn"]}', |
| 391 | "source": source, |
| 392 | } |
| 393 | |
| 394 | if (not question["tags_cn"] and not cat) or metadata["tags"] == ["Algorithms"]: |
| 395 | metadata.pop("tags") |
| 396 | if not rating: |
| 397 | metadata.pop("rating") |
| 398 | if not source: |
| 399 | metadata.pop("source") |
| 400 | yaml_metadata = yaml.dump( |
| 401 | metadata, default_flow_style=False, allow_unicode=True |
| 402 | ) |
| 403 | metadata_section = f"---\n{yaml_metadata}---" |
| 404 | readme_template_cn = readme_template_cn.format( |
| 405 | metadata_section, |
| 406 | int(question["frontend_question_id"]), |