diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-04-20 19:07:29 +0200 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-04-20 19:07:29 +0200 |
| commit | 340865ad234f974ec4c0afcbfb6ff06f50a11b2f (patch) | |
| tree | 06b6a76b6dfc535f9091990eaaec82b50d5f851e /data/download-stops.py | |
| parent | 32bc4914759845516de2b7592b05afb3c3cc5c42 (diff) | |
Load stops from local data files
Diffstat (limited to 'data/download-stops.py')
| -rw-r--r-- | data/download-stops.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/data/download-stops.py b/data/download-stops.py new file mode 100644 index 0000000..2720014 --- /dev/null +++ b/data/download-stops.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 +import json +import os +import sys +import urllib.request +import yaml # Add YAML support for overrides + +def load_stop_overrides(file_path): + """Load stop overrides from a YAML file""" + if not os.path.exists(file_path): + print(f"Warning: Overrides file {file_path} not found") + return {} + + try: + with open(file_path, 'r', encoding='utf-8') as f: + overrides = yaml.safe_load(f) + print(f"Loaded {len(overrides) if overrides else 0} stop overrides") + return overrides or {} + except Exception as e: + print(f"Error loading overrides: {e}", file=sys.stderr) + return {} + +def apply_overrides(stops, overrides): + """Apply overrides to the stop data""" + for stop in stops: + stop_id = stop.get("stopId") + if stop_id in overrides: + override = overrides[stop_id] + + # Apply name override + if "name" in override: + stop["name"] = override["name"] + + # Apply or add alternate names + if "alternateNames" in override: + if isinstance(stop["name"], str): + # Convert simple name to object if it's a string + original_name = stop["name"] + stop["name"] = {"original": original_name} + + # Add alternate names + for key, value in override["alternateNames"].items(): + stop["name"][key] = value + + # Apply location override + if "location" in override: + if "latitude" in override["location"]: + stop["latitude"] = override["location"]["latitude"] + if "longitude" in override["location"]: + stop["longitude"] = override["location"]["longitude"] + + # Add notes + if "notes" in override: + stop["notes"] = override["notes"] + + # Add active status + if "active" in override: + stop["active"] = override["active"] + + # Add amenities + if "amenities" in override: + stop["amenities"] = override["amenities"] + + print(f"Applied overrides to stop {stop_id} ({stop['name']})") + + return stops + +def main(): + print("Fetching stop list data...") + + # Download stop list data + url = "https://datos.vigo.org/vci_api_app/api2.jsp?tipo=TRANSPORTE_PARADAS" + req = urllib.request.Request(url) + + try: + with urllib.request.urlopen(req) as response: + # Read the response and decode from ISO-8859-1 to UTF-8 + content = response.read().decode('iso-8859-1') + data = json.loads(content) + + print(f"Downloaded {len(data)} stops") + + # Process the data + processed_stops = [] + for stop in data: + processed_stop = { + "stopId": stop.get("id"), + "name": { + "original": stop.get("nombre", "") + }, + "latitude": stop.get("lat"), + "longitude": stop.get("lon"), + "lines": [line.strip() for line in stop.get("lineas", "").split(",")] if stop.get("lineas") else [] + } + processed_stops.append(processed_stop) + + # Load and apply overrides + script_dir = os.path.dirname(os.path.abspath(__file__)) + overrides_file = os.path.join(script_dir, "stop-overrides.yaml") + overrides = load_stop_overrides(overrides_file) + processed_stops = apply_overrides(processed_stops, overrides) + + # Save to public directory + output_file = os.path.join(script_dir, "..", "public", "stops.json") + + with open(output_file, 'w', encoding='utf-8') as f: + json.dump(processed_stops, f, ensure_ascii=False, indent=2) + + print(f"Saved processed stops data to {output_file}") + return 0 + + except Exception as e: + print(f"Error processing stops data: {e}", file=sys.stderr) + return 1 + +if __name__ == "__main__": + sys.exit(main())
\ No newline at end of file |
