File size: 10,556 Bytes
90776a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import httpx
import os
import random
from datetime import datetime
from dotenv import load_dotenv

load_dotenv()

RAPIDAPI_KEY  = os.getenv("RAPIDAPI_KEY")
RAPIDAPI_HOST = "irctc1.p.rapidapi.com"


# ── Train Availability (IRCTC via RapidAPI) ───────────────────
async def get_train_availability(
    from_city: str,
    to_city:   str,
    date:      str       # YYYYMMDD format
) -> dict:
    """
    Fetch train seat availability from RapidAPI IRCTC.
    Falls back to smart estimation if API unavailable.
    """
    if not RAPIDAPI_KEY:
        return _estimate_availability("train", date)

    try:
        async with httpx.AsyncClient() as client:
            response = await client.get(
                "https://irctc1.p.rapidapi.com/api/v3/trainBetweenStations",
                headers={
                    "x-rapidapi-key":  RAPIDAPI_KEY,
                    "x-rapidapi-host": RAPIDAPI_HOST
                },
                params={
                    "fromStationCode": _city_to_station_code(from_city),
                    "toStationCode":   _city_to_station_code(to_city),
                    "dateOfJourney":   date
                },
                timeout=10.0
            )
            data = response.json()

            if data.get("status") and data.get("data"):
                trains      = data["data"]
                total       = len(trains)
                # Count trains with available seats
                available   = sum(
                    1 for t in trains
                    if any(
                        cls.get("available_seats", 0) > 0
                        for cls in t.get("class_type", [])
                    )
                )
                booked_pct  = round((1 - available / max(total, 1)) * 100, 1)
                return {
                    "source":          "irctc_api",
                    "total_trains":    total,
                    "available_trains":available,
                    "booked_percent":  booked_pct,
                    "crowd_percent":   booked_pct,
                    "status":          _crowd_status(booked_pct)
                }
    except Exception as e:
        print(f"⚠️  IRCTC API failed: {e}")

    return _estimate_availability("train", date)


# ── Bus Availability (RedBus via RapidAPI) ────────────────────
async def get_bus_availability(
    from_city: str,
    to_city:   str,
    date:      str
) -> dict:
    """
    Fetch bus seat availability.
    Falls back to smart estimation if API unavailable.
    """
    if not RAPIDAPI_KEY:
        return _estimate_availability("bus", date)

    try:
        async with httpx.AsyncClient() as client:
            response = await client.get(
                "https://redbus1.p.rapidapi.com/api/v1/searchBuses",
                headers={
                    "x-rapidapi-key":  RAPIDAPI_KEY,
                    "x-rapidapi-host": "redbus1.p.rapidapi.com"
                },
                params={
                    "fromCity": from_city,
                    "toCity":   to_city,
                    "doj":      date       # DD-MMM-YYYY format
                },
                timeout=10.0
            )
            data = response.json()

            if data.get("inventoryItems"):
                buses       = data["inventoryItems"]
                total_seats = sum(b.get("seatsAvailable", 0) + b.get("seatsBooked", 0) for b in buses)
                booked      = sum(b.get("seatsBooked", 0) for b in buses)
                booked_pct  = round((booked / max(total_seats, 1)) * 100, 1)
                return {
                    "source":         "redbus_api",
                    "total_buses":    len(buses),
                    "total_seats":    total_seats,
                    "booked_seats":   booked,
                    "booked_percent": booked_pct,
                    "crowd_percent":  booked_pct,
                    "status":         _crowd_status(booked_pct)
                }
    except Exception as e:
        print(f"⚠️  RedBus API failed: {e}")

    return _estimate_availability("bus", date)


# ── Metro Crowd Estimation ────────────────────────────────────
def get_metro_crowd(city: str, datetime_str: str) -> dict:
    """
    Estimate metro crowd based on time patterns.
    Metro APIs are not publicly available in India β€”
    we use smart time-based estimation.
    """
    from app.data.metro_cities import get_metro_info, has_metro

    if not has_metro(city):
        return None

    metro_info  = get_metro_info(city)
    dt          = datetime.fromisoformat(datetime_str)
    hour        = dt.hour
    dow         = dt.weekday()    # 0=Mon, 6=Sun
    is_weekend  = dow >= 5

    # Base crowd % by hour (metro peak hours pattern)
    hourly_pattern = {
        0: 5,  1: 3,  2: 2,  3: 2,  4: 5,  5: 15,
        6: 35, 7: 75, 8: 92, 9: 85, 10: 55, 11: 45,
        12: 50, 13: 55, 14: 45, 15: 50, 16: 65, 17: 88,
        18: 95, 19: 82, 20: 65, 21: 45, 22: 30, 23: 15
    }

    base_crowd = hourly_pattern.get(hour, 40)

    # Weekend adjustment
    if is_weekend:
        base_crowd = int(base_crowd * 0.65)

    # City-specific ridership factor
    city_factors = {
        "Delhi":     1.2,
        "Mumbai":    1.1,
        "Bengaluru": 1.0,
        "Chennai":   0.9,
        "Kolkata":   1.0,
        "Hyderabad": 0.85,
        "Kochi":     0.7,
        "Jaipur":    0.6,
    }
    factor     = city_factors.get(city, 0.8)
    crowd_pct  = min(int(base_crowd * factor), 99)

    return {
        "source":          "smart_estimation",
        "city":            city,
        "operator":        metro_info["operator"],
        "lines":           metro_info["lines"],
        "total_stations":  metro_info["total_stations"],
        "crowd_percent":   crowd_pct,
        "status":          _crowd_status(crowd_pct),
        "peak_hours":      "7-9 AM & 5-7 PM",
        "note":            "Based on historical ridership patterns"
    }


# ── Combined Availability Check ───────────────────────────────
async def get_all_availability(
    city:         str,
    datetime_str: str,
    to_city:      str = None
) -> dict:
    """Get availability for all transport modes for a city."""
    from app.data.metro_cities import has_metro, get_metro_info

    dt   = datetime.fromisoformat(datetime_str)
    date = dt.strftime("%Y%m%d")

    results = {}

    # Train availability
    if to_city:
        results["train"] = await get_train_availability(city, to_city, date)
    else:
        results["train"] = _estimate_availability("train", datetime_str)

    # Bus availability
    if to_city:
        results["bus"] = await get_bus_availability(city, to_city, date)
    else:
        results["bus"] = _estimate_availability("bus", datetime_str)

    # Metro β€” only if city has metro
    if has_metro(city):
        results["metro"] = get_metro_crowd(city, datetime_str)
        results["metro"]["metro_info"] = get_metro_info(city)
    else:
        results["metro"] = {
            "available":  False,
            "reason":     f"{city} does not have an operational metro system",
            "status":     "no_metro"
        }

    return results


# ── Helpers ───────────────────────────────────────────────────
def _crowd_status(percent: float) -> dict:
    if percent >= 85:
        return {"level": "HIGH",   "emoji": "πŸ”΄", "label": "Very Crowded",     "color": "red"}
    elif percent >= 60:
        return {"level": "MEDIUM", "emoji": "🟑", "label": "Moderately Busy",  "color": "yellow"}
    elif percent >= 30:
        return {"level": "LOW",    "emoji": "🟒", "label": "Comfortable",      "color": "green"}
    else:
        return {"level": "VERY_LOW","emoji":"🟒🟒","label": "Very Comfortable", "color": "green"}


def _estimate_availability(transport: str, datetime_str: str) -> dict:
    """
    Smart fallback estimation when APIs are unavailable.
    Based on time-of-day patterns.
    """
    try:
        dt   = datetime.fromisoformat(datetime_str)
        hour = dt.hour
        dow  = dt.weekday()
    except Exception:
        dt   = datetime.now()
        hour = dt.hour
        dow  = dt.weekday()

    is_weekend = dow >= 5

    # Time-based crowd patterns per transport
    patterns = {
        "train": {
            0: 20, 1: 15, 2: 10, 3: 10, 4: 20, 5: 40,
            6: 60, 7: 80, 8: 90, 9: 75, 10: 55, 11: 50,
            12: 55, 13: 60, 14: 50, 15: 55, 16: 65, 17: 85,
            18: 90, 19: 80, 20: 65, 21: 50, 22: 40, 23: 25
        },
        "bus": {
            0: 10, 1: 8,  2: 5,  3: 5,  4: 15, 5: 35,
            6: 55, 7: 85, 8: 95, 9: 80, 10: 60, 11: 55,
            12: 60, 13: 65, 14: 55, 15: 60, 16: 70, 17: 90,
            18: 95, 19: 80, 20: 60, 21: 40, 22: 25, 23: 15
        }
    }

    base   = patterns.get(transport, patterns["bus"]).get(hour, 50)
    if is_weekend:
        base = int(base * 0.7)

    # Add slight randomness for realism
    crowd  = min(max(base + random.randint(-5, 5), 0), 99)

    return {
        "source":         "smart_estimation",
        "crowd_percent":  crowd,
        "status":         _crowd_status(crowd),
        "note":           "Estimated based on time patterns (API unavailable)"
    }


def _city_to_station_code(city: str) -> str:
    """Map city name to IRCTC station code."""
    codes = {
        "Mumbai":        "CSTM",
        "Delhi":         "NDLS",
        "Bengaluru":     "SBC",
        "Chennai":       "MAS",
        "Kolkata":       "HWH",
        "Hyderabad":     "SC",
        "Pune":          "PUNE",
        "Ahmedabad":     "ADI",
        "Jaipur":        "JP",
        "Lucknow":       "LKO",
        "Patna":         "PNBE",
        "Bhopal":        "BPL",
        "Indore":        "INDB",
        "Nagpur":        "NGP",
        "Surat":         "ST",
        "Vadodara":      "BRC",
        "Chandigarh":    "CDG",
        "Amritsar":      "ASR",
        "Varanasi":      "BSB",
        "Agra":          "AGC",
        "Kanpur":        "CNB",
        "Guwahati":      "GHY",
        "Bhubaneswar":   "BBS",
        "Thiruvananthapuram": "TVC",
        "Kochi":         "ERS",
        "Coimbatore":    "CBE",
        "Madurai":       "MDU",
    }
    return codes.get(city, city[:3].upper())