elfsong commited on
Commit
e269391
·
1 Parent(s): a245764
Files changed (1) hide show
  1. app.py +136 -4
app.py CHANGED
@@ -10,6 +10,10 @@ import streamlit as st
10
 
11
  nextbus_token = os.getenv("NEXTBUS_TOKEN")
12
 
 
 
 
 
13
  def get_all_stops():
14
  url = "https://nnextbus.nus.edu.sg/BusStops"
15
 
@@ -26,9 +30,137 @@ def get_all_stops():
26
  }
27
 
28
  response = requests.request("GET", url, headers=headers, data=payload)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- return response.json()
31
 
32
- st.title("NUS NextBus")
33
- stops = get_all_stops()
34
- st.write(stops)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  nextbus_token = os.getenv("NEXTBUS_TOKEN")
12
 
13
+ def wide_space_default():
14
+ st.set_page_config(layout='wide')
15
+ wide_space_default()
16
+
17
  def get_all_stops():
18
  url = "https://nnextbus.nus.edu.sg/BusStops"
19
 
 
30
  }
31
 
32
  response = requests.request("GET", url, headers=headers, data=payload)
33
+ return response.json()["BusStopsResult"]["busstops"]
34
+
35
+ def get_bus_arrival(bus_stop_code):
36
+ url = f"https://nnextbus.nus.edu.sg/ShuttleService?busstopname={bus_stop_code}"
37
+
38
+ payload = {}
39
+ headers = {
40
+ 'Host': 'nnextbus.nus.edu.sg',
41
+ 'Content-Type': 'application/json',
42
+ 'Connection': 'keep-alive',
43
+ 'Accept': 'application/json',
44
+ 'User-Agent': 'nusnextbusv2/1 CFNetwork/978.0.7 Darwin/18.7.0',
45
+ 'Authorization': 'Basic TlVTbmV4dGJ1czoxM2RMP3pZLDNmZVdSXiJU',
46
+ 'Accept-Language': 'en-us',
47
+ 'Accept-Encoding': 'br, gzip, deflate'
48
+ }
49
+
50
+ response = requests.request("GET", url, headers=headers, data=payload)
51
+
52
+ return response.json()['ShuttleServiceResult']['shuttles']
53
+
54
+ # stops = get_all_stops()
55
+ stops = [
56
+ {
57
+ "caption": "COM 3",
58
+ "name": "COM3",
59
+ "LongName": "COM 3",
60
+ "ShortName": "COM 3",
61
+ "latitude": 1.294431,
62
+ "longitude": 103.775217
63
+ },
64
+ {
65
+ "caption": "Opp TCOMS",
66
+ "name": "TCOMS-OPP",
67
+ "LongName": "Opp TCOMS",
68
+ "ShortName": "Opp TCOMS",
69
+ "latitude": 1.293789,
70
+ "longitude": 103.776715
71
+ },
72
+ {
73
+ "caption": "TCOMS",
74
+ "name": "TCOMS",
75
+ "LongName": "TCOMS",
76
+ "ShortName": "TCOMS",
77
+ "latitude": 1.293654,
78
+ "longitude": 103.776898
79
+ },
80
+ {
81
+ "caption": "BIZ 2",
82
+ "name": "BIZ2",
83
+ "LongName": "BIZ 2",
84
+ "ShortName": "BIZ 2",
85
+ "latitude": 1.293273,
86
+ "longitude": 103.775074
87
+ },
88
+ {
89
+ "caption": "University Town",
90
+ "name": "UTOWN",
91
+ "LongName": "University Town",
92
+ "ShortName": "UTown",
93
+ "latitude": 1.303623,
94
+ "longitude": 103.774388
95
+ },
96
+ {
97
+ "caption": "Prince George's Park",
98
+ "name": "PGP",
99
+ "LongName": "Prince George's Park",
100
+ "ShortName": "PGP",
101
+ "latitude": 1.291765,
102
+ "longitude": 103.780419
103
+ },
104
+ {
105
+ "caption": "Prince George's Park Foyer",
106
+ "name": "PGPR",
107
+ "LongName": "Prince George's Park Foyer",
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
+ """
132
+ <style>
133
+ [data-testid="stMetricDelta"] svg {
134
+ display: none;
135
+ }
136
+ </style>
137
+ """,
138
+ unsafe_allow_html=True,
139
+ )
140
+
141
 
 
142
 
143
+ for stop_info in stops:
144
+ shuttle_info = get_bus_arrival(stop_info['name'])
145
+
146
+ buses = list()
147
+
148
+ for shuttle in shuttle_info:
149
+ for bus in shuttle["_etas"]:
150
+ plate = bus["plate"]
151
+ eta = bus["eta"]
152
+ shuttle_name = shuttle['name']
153
+ buses.append({
154
+ "shuttle_name": shuttle_name,
155
+ "plate": plate,
156
+ "eta": eta
157
+ })
158
+
159
+ buses.sort(key=lambda x: x["eta"])
160
+
161
+ with st.container(border=True):
162
+ cols = st.columns(6)
163
+ cols[0].metric("Bus Stop", stop_info['name'])
164
+
165
+ for i, bus in enumerate(buses[:5]):
166
+ cols[i+1].metric(bus['plate'], bus["shuttle_name"], str(bus["eta"]))