tflnotifier/tflnotif.py

36 lines
828 B
Python
Raw Permalink Normal View History

2019-05-16 09:06:29 +01:00
import requests
import os
2019-03-04 23:19:40 +00:00
url = 'https://api.tfl.gov.uk/'
app_id = os.environ['TFLID']
app_key = os.environ['TFLKEY']
2019-05-16 09:06:29 +01:00
def getLineStatusRequest(lines):
2019-03-04 23:19:40 +00:00
payload = {'app_id': app_id, 'app_key': app_key}
2019-05-16 09:06:29 +01:00
return requests.get(url + 'Line/{}/Status'.format(','.join(lines)), params=payload)
2019-03-04 23:19:40 +00:00
2019-05-16 09:06:29 +01:00
def sendNotification(linename, message):
2019-03-04 23:19:40 +00:00
2019-05-16 09:06:29 +01:00
json = {"text": "{}: {}".format(linename, message)}
2019-03-04 23:19:40 +00:00
2019-05-16 09:06:29 +01:00
requests.post(os.environ['SLACKHOOK'], json=json)
2019-03-04 23:19:40 +00:00
2019-05-16 09:06:29 +01:00
if __name__ == '__main__':
statuses = getLineStatusRequest(['metropolitan', 'piccadilly']).json()
for line in statuses:
statustext = ''
for status in line['lineStatuses']:
if status['statusSeverity'] < 10:
statustext += '\n' + status['reason']
2019-03-04 23:19:40 +00:00
2019-05-16 09:06:29 +01:00
sendNotification(line['id'][:3], ', '.join([i['statusSeverityDescription'] for i in line['lineStatuses']]) + statustext)