-
Notifications
You must be signed in to change notification settings - Fork 1
/
hwdata.py
56 lines (45 loc) · 1.76 KB
/
hwdata.py
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
import csv
import json
from io import BytesIO, TextIOWrapper
from pathlib import Path
from urllib.request import urlopen
from zipfile import ZipFile
# Path to the output files
output_dir = Path().cwd()
json_dir = output_dir / "static/devices"
page_dir = output_dir / "content/devices"
# Create the output directory if it doesn't exist
json_dir.mkdir(exist_ok=True, parents=True)
page_dir.mkdir(exist_ok=True, parents=True)
# URL to the CSV file
toh_csv_url = "https://openwrt.org/_media/toh_dump_tab_separated.zip"
def parse_csv():
# Download the CSV file and parse it
print("Downloading CSV file...")
with ZipFile(BytesIO(urlopen(toh_csv_url).read())) as zf:
csv_filename = zf.namelist()[0]
with zf.open(csv_filename, "r") as infile:
reader = csv.DictReader(
TextIOWrapper(infile, "utf-8", errors="ignore"), delimiter="\t"
)
for device in reader:
# Remove empty keys and replace them with None
for key, value in device.items():
if value == "NULL" or value == "":
device[key] = None
# Generate ID based on the page value
device["id"] = device["page"].split(":")[-1]
print(device["id"])
# Generate JSON and Markdown files
json_file = json_dir / f"{device['id']}.json"
json_file.write_text(json.dumps(device, indent=4, sort_keys=True))
page_file = page_dir / f"{device['id']}.md"
page_file.write_text(
f"""+++
title = "{device["brand"]} {device["model"]} {device.get('version', '')}"
date = 2019-11-28
[extra]
device_id = "{device['id']}"
+++"""
)
parse_csv()