99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Refresh only the display names (properties.nt) of the already extracted markers.
|
|
|
|
The marker files were extracted while interactivemap.app still delivered Chinese
|
|
names in "nt"; the source now delivers English names. This script matches markers
|
|
by id and replaces "nt" only - coordinates, categories and icons stay untouched.
|
|
|
|
Usage: python refresh_marker_names.py [--map SLUG]
|
|
"""
|
|
|
|
import argparse
|
|
import glob
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from extract_all_markers import fetch_map_markers
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Refresh marker names from interactivemap.app")
|
|
parser.add_argument("--map", type=str, default=None, help="Only this map slug")
|
|
args = parser.parse_args()
|
|
|
|
marker_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "data", "markers")
|
|
|
|
for path in sorted(glob.glob(os.path.join(marker_dir, "*_markers.json"))):
|
|
with open(path, encoding="utf-8") as fh:
|
|
export = json.load(fh)
|
|
map_info = export["map"]
|
|
if args.map and map_info["slug"] != args.map:
|
|
continue
|
|
|
|
try:
|
|
_, data, _ = fetch_map_markers(map_info)
|
|
except Exception as e:
|
|
print(f" Error fetching {map_info['slug']}: {e}", file=sys.stderr)
|
|
continue
|
|
|
|
names = {}
|
|
for group in data.get("groups") or []:
|
|
for cat in group.get("categories") or []:
|
|
for item in cat.get("data") or []:
|
|
props = item.get("properties") or {}
|
|
if props.get("id") is not None and props.get("nt"):
|
|
names[str(props["id"])] = str(props["nt"]).strip()
|
|
|
|
changed = 0
|
|
for m in export["markers"]:
|
|
props = m.get("properties") or {}
|
|
new = names.get(str(m.get("id")))
|
|
if new and props.get("nt") != new:
|
|
props["nt"] = new
|
|
changed += 1
|
|
|
|
with open(path, "w", encoding="utf-8") as fh:
|
|
json.dump(export, fh, ensure_ascii=False, indent=2)
|
|
print(f" {map_info['slug']}: {changed} names updated ({len(names)} names from source)")
|
|
|
|
fill_pet_names_by_icon(marker_dir)
|
|
|
|
|
|
def fill_pet_names_by_icon(marker_dir):
|
|
"""Pets no longer in the source keep their old name: take it from another
|
|
spot of the same pet (same icon) whose name is already up to date."""
|
|
files = sorted(glob.glob(os.path.join(marker_dir, "*_markers.json")))
|
|
exports = {}
|
|
by_icon = {}
|
|
for path in files:
|
|
with open(path, encoding="utf-8") as fh:
|
|
exports[path] = json.load(fh)
|
|
for m in exports[path]["markers"]:
|
|
props = m.get("properties") or {}
|
|
nt = props.get("nt") or ""
|
|
if m.get("group_name") == "Pets" and nt and not has_cjk(nt):
|
|
by_icon.setdefault(props.get("icon_name"), nt)
|
|
|
|
for path, export in exports.items():
|
|
changed = 0
|
|
for m in export["markers"]:
|
|
props = m.get("properties") or {}
|
|
if m.get("group_name") == "Pets" and has_cjk(props.get("nt") or ""):
|
|
new = by_icon.get(props.get("icon_name"))
|
|
if new:
|
|
props["nt"] = new
|
|
changed += 1
|
|
if changed:
|
|
with open(path, "w", encoding="utf-8") as fh:
|
|
json.dump(export, fh, ensure_ascii=False, indent=2)
|
|
print(f" {os.path.basename(path)}: {changed} pet names taken from the same icon")
|
|
|
|
|
|
def has_cjk(s):
|
|
return any("一" <= ch <= "鿿" for ch in s)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|