metro-info/cgi-bin/platform_data.py

31 lines
755 B
Python
Raw Normal View History

2025-02-09 20:35:44 +00:00
#!/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)):
2025-02-09 20:35:44 +00:00
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())