An advanced data scraping and analysis suite designed to quantify and visualize train delays for the Thai Railway system. This project specifically focuses on the route between Bangkok (Krung Thep Aphiwat) and Thung Song Junction.
This tool allows users to:
- Scrape: Extract historical tracking data from the official Thai Railway TTS API.
- Analyze: Process lateness patterns across hundreds of historical trips.
- Visualize: Interact with a modern dashboard to evaluate travel risks and identify common causes of delays.
- Live Status: Pull real-time tracking for train 169 directly from
ttsview.railway.co.thand watch its current delay update every 30 seconds.
Ensure you have Python 3.8+ installed. Install dependencies via requirements.txt:
pip install -r requirements.txtThe scraper uses a predefined mapping of dates and runhashes to fetch specific tracking information.
- Script:
scrape_delays.py - Input:
date_runhash_map.csv - Execution:
python3 scrape_delays.py
- Output: Generates
station_delays.csvcontaining arrival/departure lateness and reported delay causes.
The interactive dashboard is built with Streamlit and provides a premium light-themed interface.
- Script:
analyze_delays.py - Execution:
streamlit run analyze_delays.py
- Station Selector: Compare performance across multiple stations simultaneously.
- Time Granularity: View data in Daily, Monthly, or Yearly averages.
- Risk Threshold (N): Set your custom lateness threshold (e.g., 30 mins) to calculate probability of failure.
- Analysis Period: Select a specific historical date range for focused research.
- Worst/Best Lateness: Identify the extreme outliers in the dataset.
- Reliability Score: Percentage of trips arriving within a 10-minute window of the schedule.
- Major Delays Counter: Instant count of incidents exceeding 1 hour.
- Root Cause Analysis: A breakdown of official reasons for delays (e.g., track clearance, construction).
- Major Incidents (>1h): A dedicated deep-dive table for critical timing failures, showing exactly what went wrong and when.
- Connects to the SRT WebSocket at
wss://ttsview.railway.co.th:5000and emitsviewSubTrainwith today's runhash to receive every station's live status. - Renders the current station, latest delay, and per-station ETAs (actual → estimated → schedule + delay propagation).
- Auto-refreshes every 30 seconds while the tab is open.
The WebSocket bypasses Cloudflare's Turnstile so no JWT or browser automation is required, but you do need to tell the app which runhash represents today's train 169:
- Visit https://ttsview.railway.co.th/v3/ and open the train 169 row. The browser opens a
/v3/search/?qType=21&qParam=...URL. - Paste either the full URL or just the
qParamvalue into the runhash input on the Live Status tab. - The runhash is cached at
~/.cache/train_analysis/runhash_169.jsonfor the rest of the day. The next morning you'll be asked to provide the new one.
scrape_delays.py: Python script for automated historical data extraction.analyze_delays.py: Streamlit-based intelligence dashboard.live_view.py: Live Status tab (WebSocket fetch + auto-refresh + ETA rendering).srt_socket.py: Socket.IO client forviewSubTrainlive tracking.station_delays.csv: The primary dataset (generated after scraping).date_runhash_map.csv: Mapping file required for API queries.station_data.json: Metadata for railway stations.
This tool is intended for personal analysis and journey planning. It relies on publicly available data from the Thai Railway API. Arrival times are subject to real-time changes and operational incidents.