Debian Codegraph @

scripts/process-localization-extract.py

raw
1 """Process the extract for a localization table from the cookidoo web application."""
2 #!/usr/bin/env python3
3
4 # Instructions found here: cookidoo-api/docs/localization.md
5
6 import json
7 import os
8
9 from bs4 import BeautifulSoup
10
11 script_dir = os.path.dirname(__file__)
12 input_file_path = os.path.join(script_dir, "../raw/localization-extract.html")
13 output_file_path = os.path.join(script_dir, "../cookidoo_api/localization.json")
14
15 # Load the HTML content
16 with open(input_file_path, encoding="utf-8") as file:
17 html_content = file.read()
18
19 # Parse the HTML
20 soup = BeautifulSoup(html_content, "html.parser")
21
22 # Extract the data
23 localization_data = [
24 {
25 "country_code": li["data-filter"],
26 "language": li["data-lang"],
27 "url": li.find("input")["value"],
28 }
29 for li in soup.find_all("li", {"class": "core-dropdown-list__item"})
30 if li.find("input", {"type": "checkbox"})
31 ]
32 print(f"Successfully extract {len(localization_data)} entries")
33
34 # Save the extracted data to JSON
35 with open(output_file_path, "w", encoding="utf-8") as file:
36 json.dump(localization_data, file, ensure_ascii=False, indent=4)