#!/usr/bin/python3 import re import requests import json import os import sys url = sys.argv[1] outdir = sys.argv[2] base = url.split("/map/")[0] r = requests.get(url) regexp = re.compile(r'U.MAP = new U.Map[(]"map", (.+) }\)', re.DOTALL) data = json.loads(regexp.findall(r.text, re.DOTALL)[0].replace("})","}")) properties = data["properties"] layers = properties["datalayers"] map_id = properties["umap_id"] config = {} config["name"] = properties["name"] colors = {} config["layers"] = colors if "tilelayer" in properties and properties['tilelayer'] != {} : config["tilelayer"] = properties["tilelayer"] config["tilelayer"]["attribution"] = re.sub(r'\[\[([^|]+)\|([^|]+)\]\]', r'\2', config["tilelayer"]["attribution"]) else : config["tilelayer"] = { "attribution" : 'Map data © OpenStreetMap contributors', "url_template" : "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" } def normalize_name(name) : return name.replace("/", "_").replace("-","").replace(" ","").replace(".","") datadir = os.path.join(outdir,"data") if not os.path.exists(datadir) : os.mkdir(datadir) for layer in layers : layer_id = layer["id"] req = requests.get(f"{base}/datalayer/{map_id}/{layer_id}/") options = req.json()["_umap_options"] nname = normalize_name(options['name']) if not os.path.exists(os.path.join(datadir,nname)) : os.mkdir(os.path.join(datadir,nname)) with open(os.path.join(datadir,nname,f"{nname}.json"), "w") as f : f.write(req.text) colors[nname] = { "color" : options["color"] if "color" in options else "DarkGreen" , "humanname" : options["name"] } if "weight" in options : colors[nname]["width"] = options["weight"] else: colors[nname]["width"] = 2 with open(os.path.join(outdir,"layers.json"), "w") as f : json.dump(config, f, indent=4)