metro-info/cgi-bin/platform_data.py
2025-05-11 09:10:02 +00:00

31 lines
No EOL
755 B
Python
Executable file

#!/srv/think.somethingorotherwhatever.com/venv/bin/python
import cgi
import cgitb
import requests
from pathlib import Path
from datetime import datetime, timedelta
import getpass
cgitb.enable()
form = cgi.FieldStorage()
print('Content-Type: application/json\n')
station = form['station'].value
platform = form['platform'].value
data_root = Path('..') / 'platform_data'
data_root.mkdir(exist_ok=True)
fname = data_root / f'{station}-{platform}.json'
if (not fname.exists()) or (datetime.now() - datetime.fromtimestamp(fname.stat().st_mtime) > timedelta(seconds=20)):
req = requests.get(f'https://metro-rti.nexus.org.uk/api/times/{station}/{platform}')
with open(fname, 'w') as f:
f.write(req.text)
with open(fname) as f:
print(f.read())