elfsong commited on
Commit
de9fc43
·
1 Parent(s): aba0227
Files changed (2) hide show
  1. app.py +76 -0
  2. requirments.txt +1 -0
app.py CHANGED
@@ -4,13 +4,16 @@
4
  # Date: 2025/03/22
5
 
6
  import os
 
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')
@@ -62,6 +65,53 @@ def get_lta_bus_arrival(bus_stop_code):
62
  response = requests.request("GET", url, headers=headers, data=payload)
63
  return response.json()
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # stops = get_all_stops()
67
  nus_stops = [
@@ -118,6 +168,13 @@ public_stops = [
118
  },
119
  ]
120
 
 
 
 
 
 
 
 
121
  st.write(
122
  """
123
  <style>
@@ -189,3 +246,22 @@ for stop_info in public_stops:
189
  for i, bus in enumerate(buses[:5]):
190
  cols[i+1].metric(bus["type"], bus['service'], bus["eta"])
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  # Date: 2025/03/22
5
 
6
  import os
7
+ import re
8
  import json
9
  import requests
10
  import streamlit as st
11
  from datetime import datetime
12
+ from bs4 import BeautifulSoup
13
 
14
  nextbus_token = os.getenv("NEXTBUS_TOKEN")
15
  datamall_token = os.getenv("DATAMALL_TOKEN")
16
+ mrt_token = os.getenv("MRT_TOKEN")
17
 
18
  def wide_space_default():
19
  st.set_page_config(layout='wide')
 
65
  response = requests.request("GET", url, headers=headers, data=payload)
66
  return response.json()
67
 
68
+ def get_SMRT_train_arrival(station_code):
69
+ url = "https://trainarrivalweb.smrt.com.sg/"
70
+
71
+ payload = f"ScriptManager1=UP1%7CddlStation&stnCode=&stnName=&ddlStation={station_code}&{mrt_token}"
72
+ headers = {
73
+ 'Accept': '*/*',
74
+ 'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7,zh-HK;q=0.6,zh-TW;q=0.5,zh;q=0.4',
75
+ 'Cache-Control': 'no-cache',
76
+ 'Connection': 'keep-alive',
77
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
78
+ 'Origin': 'https://trainarrivalweb.smrt.com.sg',
79
+ 'Referer': 'https://trainarrivalweb.smrt.com.sg/',
80
+ 'Sec-Fetch-Dest': 'empty',
81
+ 'Sec-Fetch-Mode': 'cors',
82
+ 'Sec-Fetch-Site': 'same-origin',
83
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
84
+ 'X-MicrosoftAjax': 'Delta=true',
85
+ 'X-Requested-With': 'XMLHttpRequest',
86
+ 'sec-ch-ua': '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
87
+ 'sec-ch-ua-mobile': '?0',
88
+ 'sec-ch-ua-platform': '"macOS"',
89
+ 'Cookie': 'ASP.NET_SessionId=bwmeirfwzq30nj0ddk0qzyav; _ga=GA1.1.974289719.1742578510; _ga_78C66TP2RM=GS1.1.1742578509.1.1.1742578584.0.0.0'
90
+ }
91
+ response = requests.request("POST", url, headers=headers, data=payload)
92
+ raw_html = response.text
93
+ soup = BeautifulSoup(raw_html, 'html.parser')
94
+
95
+ tables = soup.find_all("table", id="gvTime")
96
+
97
+ trains = list()
98
+
99
+ for table in tables:
100
+ time_row = table.find_all("tr")[1]
101
+ time_cells = [td.get_text(strip=True) for td in time_row.find_all("td")]
102
+
103
+ direction_row = table.find_all("tr")[2]
104
+ direction_cells = [td.get_text(strip=True) for td in direction_row.find_all("td")]
105
+
106
+ for eta_text, direction in zip(time_cells, direction_cells):
107
+ eta = re.findall(r'\d+', eta_text)[0]
108
+ trains.append({
109
+ "direction": direction,
110
+ "eta": eta
111
+ })
112
+
113
+ return trains
114
+
115
 
116
  # stops = get_all_stops()
117
  nus_stops = [
 
168
  },
169
  ]
170
 
171
+ public_mrt_station = [
172
+ {
173
+ 'name': "KR MRT",
174
+ 'code': 'CKRG'
175
+ }
176
+ ]
177
+
178
  st.write(
179
  """
180
  <style>
 
246
  for i, bus in enumerate(buses[:5]):
247
  cols[i+1].metric(bus["type"], bus['service'], bus["eta"])
248
 
249
+ # SMRT
250
+ for station in public_mrt_station:
251
+ smrt_data = get_SMRT_train_arrival(station['code'])
252
+ trains = list()
253
+
254
+ for train in smrt_data:
255
+ trains.append({
256
+ "direction": train["direction"],
257
+ "eta": train["eta"]
258
+ })
259
+
260
+ trains.sort(key=lambda x: x["eta"])
261
+
262
+ with st.container(border=True):
263
+ cols = st.columns(6)
264
+ cols[0].metric("MRT Station", station['name'])
265
+
266
+ for i, train in enumerate(trains[:5]):
267
+ cols[i+1].metric(train['direction'], 'CC', str(train['eta']))
requirments.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ BeautifulSoup