File size: 1,463 Bytes
05d6299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cac383d
05d6299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Query, HTTPException
from MultiFeatures.IndianRailway import whereismytrain

app = FastAPI(
    title="Where Is My Train API",
    version="1.0.0"
)

wit = whereismytrain.WhereIsMyTrain()


@app.get("/")
async def root():
    return {
        "status": "online",
        "service": "Where Is My Train wrapper",
        "endpoints": [
            "/live",
            "/station"
        ]
    }


@app.get("/live")
async def live(
    train: str = Query(...),
    date: str = Query(...),
    from_station: str = Query(..., alias="from"),
    to_station: str = Query(..., alias="to"),
    from_day: int = Query(1),
    lang: str = Query("en")
):
    try:
        result = await wit.live_status(
            train_no=train,
            date=date,
            from_station=from_station,
            to_station=to_station,
            from_day=from_day,
            lang=lang
        )

        return result

    except Exception as e:
        raise HTTPException(
            status_code=502,
            detail=str(e)
        )


@app.get("/station")
async def station(
    code: str = Query(...),
    hours: int = Query(8),
    lang: str = Query("en")
):
    try:
        result = await wit.live_station(
            station_code=code,
            hrs=hours,
            lang=lang
        )

        return result

    except Exception as e:
        raise HTTPException(
            status_code=502,
            detail=str(e)
        )