rewritten and using slack
This commit is contained in:
parent
24a606b54e
commit
7a6f142a74
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
86
tflnotif.py
86
tflnotif.py
@ -1,91 +1,35 @@
|
|||||||
import requests, smtplib, os
|
import requests
|
||||||
|
import os
|
||||||
|
|
||||||
url = 'https://api.tfl.gov.uk/'
|
url = 'https://api.tfl.gov.uk/'
|
||||||
app_id = os.environ['TFLID']
|
app_id = os.environ['TFLID']
|
||||||
app_key = os.environ['TFLKEY']
|
app_key = os.environ['TFLKEY']
|
||||||
|
|
||||||
def _TFLURL(path):
|
|
||||||
return 'https://api.tfl.gov.uk/' + path + '?app_id={}&app_key={}'.format(app_id, app_key)
|
|
||||||
|
|
||||||
def getLineStatusRequest(line):
|
def getLineStatusRequest(lines):
|
||||||
|
|
||||||
payload = {'app_id': app_id, 'app_key': app_key}
|
payload = {'app_id': app_id, 'app_key': app_key}
|
||||||
|
|
||||||
return requests.get(url + 'Line/{}/Status'.format(line), params = payload)
|
return requests.get(url + 'Line/{}/Status'.format(','.join(lines)), params=payload)
|
||||||
|
|
||||||
def lineStatus(line):
|
|
||||||
resp = getLineStatusRequest(line)
|
|
||||||
|
|
||||||
print(resp.json()[0]['name'])
|
def sendNotification(linename, message):
|
||||||
#print(resp.json()['name'])
|
|
||||||
#print(resp.json()[0]['lineStatuses'][0]['statusSeverityDescription'])
|
|
||||||
|
|
||||||
for status in resp.json()[0]['lineStatuses']:
|
json = {"text": "{}: {}".format(linename, message)}
|
||||||
print(status['statusSeverityDescription'])
|
|
||||||
|
|
||||||
def isDelayed(line):
|
requests.post(os.environ['SLACKHOOK'], json=json)
|
||||||
resp = getLineStatusRequest(line)
|
|
||||||
|
|
||||||
for status in resp.json()[0]['lineStatuses']:
|
|
||||||
if status['statusSeverity'] < 10:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def getLineStatusDescription(line):
|
if __name__ == '__main__':
|
||||||
resp = getLineStatusRequest(line)
|
|
||||||
|
|
||||||
if len(resp.json()[0]['lineStatuses']) > 1:
|
statuses = getLineStatusRequest(['metropolitan', 'piccadilly']).json()
|
||||||
list = []
|
|
||||||
for status in resp.json()[0]['lineStatuses']:
|
|
||||||
list.append(status['statusSeverityDescription'])
|
|
||||||
return list
|
|
||||||
else:
|
|
||||||
return resp.json()[0]['lineStatuses'][0]['statusSeverityDescription']
|
|
||||||
|
|
||||||
def getLineStatusReason(line):
|
for line in statuses:
|
||||||
resp = getLineStatusRequest(line)
|
|
||||||
if isDelayed(line):
|
|
||||||
list = []
|
|
||||||
for status in resp.json()[0]['lineStatuses']:
|
|
||||||
list.append(status['reason'])
|
|
||||||
return list
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def sendNotification(message, subject = ''):
|
statustext = ''
|
||||||
server = smtplib.SMTP('smtp.gmail.com', 587)
|
|
||||||
server.ehlo()
|
|
||||||
server.starttls()
|
|
||||||
server.ehlo()
|
|
||||||
server.login(os.environ['TFLMAILFROM'], os.environ['TFLPW'])
|
|
||||||
|
|
||||||
msg = 'Subject: {}\n\n{}'.format(subject, message)
|
for status in line['lineStatuses']:
|
||||||
server.sendmail(os.environ['TFLMAILFROM'], os.environ['TFLMAILTO'], msg)
|
if status['statusSeverity'] < 10:
|
||||||
server.quit()
|
statustext += '\n' + status['reason']
|
||||||
|
|
||||||
message = 'met line: '
|
sendNotification(line['id'][:3], ', '.join([i['statusSeverityDescription'] for i in line['lineStatuses']]) + statustext)
|
||||||
for description in getLineStatusDescription('metropolitan'):
|
|
||||||
message += description + ' '
|
|
||||||
metReasons = getLineStatusReason('metropolitan')
|
|
||||||
if metReasons:
|
|
||||||
for reason in metReasons:
|
|
||||||
message += '\n\t\t' + reason
|
|
||||||
message += '\n'
|
|
||||||
message += 'pic line: '
|
|
||||||
for description in getLineStatusDescription('piccadilly'):
|
|
||||||
message += description + ' '
|
|
||||||
picReasons = getLineStatusReason('piccadilly')
|
|
||||||
if picReasons:
|
|
||||||
for reason in picReasons:
|
|
||||||
message += '\n\t\t' + reason
|
|
||||||
message += '\n'
|
|
||||||
message += 'dis line: '
|
|
||||||
for description in getLineStatusDescription('district'):
|
|
||||||
message += description + ' '
|
|
||||||
disReasons = getLineStatusReason('district')
|
|
||||||
if disReasons:
|
|
||||||
for reason in disReasons:
|
|
||||||
message += '\n\t\t' + reason
|
|
||||||
|
|
||||||
#print(message)
|
|
||||||
sendNotification(message, 'tfl update')
|
|
||||||
|
Loading…
Reference in New Issue
Block a user