Spaces:
Sleeping
Sleeping
elfsong
commited on
Commit
·
aba0227
1
Parent(s):
d99c80d
Update
Browse files
app.py
CHANGED
|
@@ -7,8 +7,10 @@ import os
|
|
| 7 |
import json
|
| 8 |
import requests
|
| 9 |
import streamlit as st
|
|
|
|
| 10 |
|
| 11 |
nextbus_token = os.getenv("NEXTBUS_TOKEN")
|
|
|
|
| 12 |
|
| 13 |
def wide_space_default():
|
| 14 |
st.set_page_config(layout='wide')
|
|
@@ -32,7 +34,7 @@ def get_all_stops():
|
|
| 32 |
response = requests.request("GET", url, headers=headers, data=payload)
|
| 33 |
return response.json()["BusStopsResult"]["busstops"]
|
| 34 |
|
| 35 |
-
def
|
| 36 |
url = f"https://nnextbus.nus.edu.sg/ShuttleService?busstopname={bus_stop_code}"
|
| 37 |
|
| 38 |
payload = {}
|
|
@@ -51,81 +53,70 @@ def get_bus_arrival(bus_stop_code):
|
|
| 51 |
|
| 52 |
return response.json()['ShuttleServiceResult']['shuttles']
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# stops = get_all_stops()
|
| 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 |
-
"ShortName": "PGP Foyer",
|
| 109 |
-
"latitude": 1.290994,
|
| 110 |
-
"longitude": 103.781153
|
| 111 |
-
},
|
| 112 |
-
{
|
| 113 |
-
"caption": "Opp Kent Ridge MRT",
|
| 114 |
-
"name": "KR-MRT-OPP",
|
| 115 |
-
"LongName": "Opp Kent Ridge MRT",
|
| 116 |
-
"ShortName": "Opp KR MRT",
|
| 117 |
-
"latitude": 1.294962,
|
| 118 |
-
"longitude": 103.784556
|
| 119 |
-
},
|
| 120 |
-
{
|
| 121 |
-
"caption": "Kent Ridge MRT",
|
| 122 |
-
"name": "KR-MRT",
|
| 123 |
-
"LongName": "Kent Ridge MRT",
|
| 124 |
-
"ShortName": "KR MRT",
|
| 125 |
-
"latitude": 1.29482,
|
| 126 |
-
"longitude": 103.784413
|
| 127 |
-
},
|
| 128 |
-
]
|
| 129 |
|
| 130 |
st.write(
|
| 131 |
"""
|
|
@@ -138,14 +129,14 @@ st.write(
|
|
| 138 |
unsafe_allow_html=True,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
for stop_info in
|
| 143 |
-
shuttle_info =
|
| 144 |
|
| 145 |
buses = list()
|
| 146 |
|
| 147 |
for shuttle in shuttle_info:
|
| 148 |
-
if
|
| 149 |
for bus in shuttle["_etas"]:
|
| 150 |
plate = bus["plate"]
|
| 151 |
eta = bus["eta"]
|
|
@@ -160,7 +151,41 @@ for stop_info in stops:
|
|
| 160 |
|
| 161 |
with st.container(border=True):
|
| 162 |
cols = st.columns(6)
|
| 163 |
-
cols[0].metric("
|
| 164 |
|
| 165 |
for i, bus in enumerate(buses[:5]):
|
| 166 |
cols[i+1].metric(bus['plate'], bus["shuttle_name"], str(bus["eta"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import json
|
| 8 |
import requests
|
| 9 |
import streamlit as st
|
| 10 |
+
from datetime import datetime
|
| 11 |
|
| 12 |
nextbus_token = os.getenv("NEXTBUS_TOKEN")
|
| 13 |
+
datamall_token = os.getenv("DATAMALL_TOKEN")
|
| 14 |
|
| 15 |
def wide_space_default():
|
| 16 |
st.set_page_config(layout='wide')
|
|
|
|
| 34 |
response = requests.request("GET", url, headers=headers, data=payload)
|
| 35 |
return response.json()["BusStopsResult"]["busstops"]
|
| 36 |
|
| 37 |
+
def get_nus_bus_arrival(bus_stop_code):
|
| 38 |
url = f"https://nnextbus.nus.edu.sg/ShuttleService?busstopname={bus_stop_code}"
|
| 39 |
|
| 40 |
payload = {}
|
|
|
|
| 53 |
|
| 54 |
return response.json()['ShuttleServiceResult']['shuttles']
|
| 55 |
|
| 56 |
+
def get_lta_bus_arrival(bus_stop_code):
|
| 57 |
+
url = f"https://datamall2.mytransport.sg/ltaodataservice/v3/BusArrival?BusStopCode={bus_stop_code}"
|
| 58 |
+
payload = {}
|
| 59 |
+
headers = {
|
| 60 |
+
'AccountKey': datamall_token
|
| 61 |
+
}
|
| 62 |
+
response = requests.request("GET", url, headers=headers, data=payload)
|
| 63 |
+
return response.json()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
# stops = get_all_stops()
|
| 67 |
+
nus_stops = [
|
| 68 |
+
{
|
| 69 |
+
"caption": "COM 3",
|
| 70 |
+
"name": "COM3",
|
| 71 |
+
"LongName": "COM 3",
|
| 72 |
+
"ShortName": "COM 3",
|
| 73 |
+
"latitude": 1.294431,
|
| 74 |
+
"longitude": 103.775217
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"caption": "Opp TCOMS",
|
| 78 |
+
"name": "TCOMS-OPP",
|
| 79 |
+
"LongName": "Opp TCOMS",
|
| 80 |
+
"ShortName": "Opp TCOMS",
|
| 81 |
+
"latitude": 1.293789,
|
| 82 |
+
"longitude": 103.776715
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"caption": "TCOMS",
|
| 86 |
+
"name": "TCOMS",
|
| 87 |
+
"LongName": "TCOMS",
|
| 88 |
+
"ShortName": "TCOMS",
|
| 89 |
+
"latitude": 1.293654,
|
| 90 |
+
"longitude": 103.776898
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"caption": "Prince George's Park",
|
| 94 |
+
"name": "PGP",
|
| 95 |
+
"LongName": "Prince George's Park",
|
| 96 |
+
"ShortName": "PGP",
|
| 97 |
+
"latitude": 1.291765,
|
| 98 |
+
"longitude": 103.780419
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"caption": "Prince George's Park Foyer",
|
| 102 |
+
"name": "PGPR",
|
| 103 |
+
"LongName": "Prince George's Park Foyer",
|
| 104 |
+
"ShortName": "PGP Foyer",
|
| 105 |
+
"latitude": 1.290994,
|
| 106 |
+
"longitude": 103.781153
|
| 107 |
+
},
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
public_stops = [
|
| 111 |
+
{
|
| 112 |
+
'name': "Opp HMK",
|
| 113 |
+
'code': "16061"
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
'name': "HMK",
|
| 117 |
+
'code': "16069"
|
| 118 |
+
},
|
| 119 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
st.write(
|
| 122 |
"""
|
|
|
|
| 129 |
unsafe_allow_html=True,
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# NUS Stops
|
| 133 |
+
for stop_info in nus_stops:
|
| 134 |
+
shuttle_info = get_nus_bus_arrival(stop_info['name'])
|
| 135 |
|
| 136 |
buses = list()
|
| 137 |
|
| 138 |
for shuttle in shuttle_info:
|
| 139 |
+
if "_etas" not in shuttle: continue
|
| 140 |
for bus in shuttle["_etas"]:
|
| 141 |
plate = bus["plate"]
|
| 142 |
eta = bus["eta"]
|
|
|
|
| 151 |
|
| 152 |
with st.container(border=True):
|
| 153 |
cols = st.columns(6)
|
| 154 |
+
cols[0].metric("NUS Stop", stop_info['name'])
|
| 155 |
|
| 156 |
for i, bus in enumerate(buses[:5]):
|
| 157 |
cols[i+1].metric(bus['plate'], bus["shuttle_name"], str(bus["eta"]))
|
| 158 |
+
|
| 159 |
+
# Public Bus
|
| 160 |
+
for stop_info in public_stops:
|
| 161 |
+
bus_info = get_lta_bus_arrival(stop_info['code'])
|
| 162 |
+
buses = list()
|
| 163 |
+
|
| 164 |
+
for shuttle in bus_info["Services"]:
|
| 165 |
+
try:
|
| 166 |
+
service_no = shuttle["ServiceNo"]
|
| 167 |
+
for bus_seq in ['NextBus', 'NextBus2', 'NextBus3']:
|
| 168 |
+
bus_type = shuttle[bus_seq]['Type']
|
| 169 |
+
bus_load = shuttle[bus_seq]['Load']
|
| 170 |
+
arrival_time = datetime.fromisoformat(shuttle[bus_seq]['EstimatedArrival'])
|
| 171 |
+
now = datetime.now(arrival_time.tzinfo)
|
| 172 |
+
time_diff = arrival_time - now
|
| 173 |
+
eta = int(time_diff.total_seconds() / 60)
|
| 174 |
+
|
| 175 |
+
buses.append({
|
| 176 |
+
"service": service_no,
|
| 177 |
+
"eta": eta,
|
| 178 |
+
"type": f'{bus_type} - {bus_load}'
|
| 179 |
+
})
|
| 180 |
+
except Exception as e:
|
| 181 |
+
pass
|
| 182 |
+
|
| 183 |
+
buses.sort(key=lambda x: x["eta"])
|
| 184 |
+
|
| 185 |
+
with st.container(border=True):
|
| 186 |
+
cols = st.columns(6)
|
| 187 |
+
cols[0].metric("Public Stop", stop_info['name'])
|
| 188 |
+
|
| 189 |
+
for i, bus in enumerate(buses[:5]):
|
| 190 |
+
cols[i+1].metric(bus["type"], bus['service'], bus["eta"])
|
| 191 |
+
|