skeledance/tools/build_constellations.py

87 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Gera constellations.js com figuras convencionais em coordenadas J2000.
Fonte: d3-celestial, de Olaf Frohn, licença BSD-3-Clause.
Os dados são compactados para uso offline pelo wallpaper.
"""
import json
import urllib.request
from pathlib import Path
BASE = "https://raw.githubusercontent.com/ofrohn/d3-celestial/master/data/"
OUT = Path(__file__).resolve().parent.parent / "plugin/contents/ui/constellations.js"
LICENSE = """Copyright (c) 2015, Olaf Frohn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor contributors may be used to
endorse or promote products derived from this software without permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DAMAGES ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE.
"""
def fetch(name: str) -> dict:
req = urllib.request.Request(BASE + name, headers={"User-Agent": "skeledance-build/1.0"})
return json.loads(urllib.request.urlopen(req, timeout=30).read())
def rounded_point(point: list[float]) -> list[float]:
return [round(float(point[0]), 4), round(float(point[1]), 4)]
def main() -> None:
line_data = fetch("constellations.lines.json")["features"]
name_data = fetch("constellations.json")["features"]
metadata = {
feature["id"]: {
"name": feature["properties"].get("name", feature["id"]),
"label": rounded_point(feature["geometry"]["coordinates"]),
}
for feature in name_data
}
merged: dict[str, dict] = {}
for feature in line_data:
ident = feature["id"]
item = merged.setdefault(
ident,
{
"id": ident,
"name": metadata.get(ident, {}).get("name", ident),
"rank": int(feature["properties"].get("rank", 3)),
"label": metadata.get(ident, {}).get("label"),
"lines": [],
},
)
item["rank"] = min(item["rank"], int(feature["properties"].get("rank", 3)))
item["lines"].extend(
[[rounded_point(point) for point in line]
for line in feature["geometry"]["coordinates"]]
)
payload = json.dumps(list(merged.values()), ensure_ascii=False, separators=(",", ":"))
OUT.write_text(
"/* Gerado por tools/build_constellations.py.\n"
" d3-celestial constellation lines, coordenadas equatoriais J2000.\n\n"
+ "\n".join(" " + line for line in LICENSE.rstrip().splitlines())
+ " */\nconst CONSTELLATIONS=" + payload + ";\n",
encoding="utf-8",
)
print(f"{len(merged)} constelações -> {OUT} ({OUT.stat().st_size / 1024:.0f} KB)")
if __name__ == "__main__":
main()