aboutsummaryrefslogtreecommitdiff
path: root/data/download-stops.py
blob: 957e50da79a29f1319c46e6a5aa14237e85fdba3 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/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:
                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 amenities
            if "amenities" in override:
                stop["amenities"] = override["amenities"]
                
            # Mark stop as hidden if needed
            if "hide" in override:
                stop["hide"] = override["hide"]
                
    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)
        
        # Filter out hidden stops
        visible_stops = [stop for stop in processed_stops if not stop.get("hide")]
        print(f"Removed {len(processed_stops) - len(visible_stops)} hidden stops")
        
        # 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(visible_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())