F1 circuit layouts with year-by-year SVGs — manually traced track variations
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

97 строки
3.0KB

  1. import json
  2. from pathlib import Path
  3. from slugify import slugify
  4. def load_json_file(file_path: str):
  5. """Load and parse a JSON file"""
  6. try:
  7. with open(file_path, 'r', encoding='utf-8') as f:
  8. return json.load(f)
  9. except (FileNotFoundError, json.JSONDecodeError) as e:
  10. print(f"Error reading {file_path}: {str(e)}")
  11. return {}
  12. def save_json_file(data, file_path: str):
  13. """Save data to a JSON file"""
  14. try:
  15. with open(file_path, 'w', encoding='utf-8') as f:
  16. json.dump(data, f, indent=2, ensure_ascii=False)
  17. print(f"Successfully saved: {file_path}")
  18. except Exception as e:
  19. print(f"Error saving {file_path}: {str(e)}")
  20. def generate_layout_slug(layout_years: str) -> str:
  21. return layout_years
  22. def add_slugs_to_data(data):
  23. """Recursively add slugs to the circuit data structure"""
  24. new_data = {}
  25. for country, country_data in data.items():
  26. country_slug = slugify(country)
  27. new_country_data = {
  28. "slug": country_slug,
  29. "cities": {}
  30. }
  31. for city, city_data in country_data.items():
  32. city_slug = slugify(city)
  33. new_city_data = {
  34. "slug": city_slug,
  35. "circuits": {}
  36. }
  37. for circuit_name, circuit_data in city_data.items():
  38. circuit_slug = slugify(circuit_name)
  39. # Copy all existing circuit data
  40. new_circuit_data = circuit_data.copy()
  41. # Add circuit slug
  42. new_circuit_data["slug"] = circuit_slug
  43. # Add slugs to layouts
  44. if "layouts" in new_circuit_data:
  45. new_layouts = {}
  46. for layout_years, layout_data in new_circuit_data["layouts"].items():
  47. # Create new layout data with slug
  48. new_layout_data = layout_data.copy()
  49. new_layout_data["slug"] = generate_layout_slug(layout_years)
  50. new_layout_data["years"] = layout_years # Store original years string
  51. new_layouts[layout_years] = new_layout_data
  52. new_circuit_data["layouts"] = new_layouts
  53. new_city_data["circuits"][circuit_name] = new_circuit_data
  54. new_country_data["cities"][city] = new_city_data
  55. new_data[country] = new_country_data
  56. return new_data
  57. def main():
  58. # Define file paths
  59. input_file = Path("circuits.json")
  60. output_file = Path("circuits-with-slugs.json")
  61. # Load existing data
  62. print(f"Loading data from {input_file}")
  63. data = load_json_file(input_file)
  64. if not data:
  65. print("No data loaded. Exiting.")
  66. return
  67. # Add slugs to the data structure
  68. print("Adding slugs to data structure...")
  69. new_data = add_slugs_to_data(data)
  70. # Save the modified data
  71. print(f"Saving modified data to {output_file}")
  72. save_json_file(new_data, output_file)
  73. print("Done!")
  74. if __name__ == "__main__":
  75. main()