-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlirrNotify.py
155 lines (123 loc) · 4.46 KB
/
lirrNotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import requests
import json
import time
from dateutil.parser import parse
from pprint import pprint
import pandas as pd
from flask import *
import threading
import datetime
import pytz
#init flask
app = Flask(__name__)
PUSHOVER_TOKEN = 'PUSHOVER TOKEN'
def loadStationInfo():
#resp = requests.get('https://traintime.lirr.org/api/Stations-All')
#data = resp.json()
file = open('./Stations-All.json')
text = file.read()
file.close()
data = json.loads(text)
stations = pd.DataFrame(data['Stations']).T.set_index('ABBR')['NAME']
return stations
#pulls train data from LIRR and return it processed in DF
def getTrains():
stations = loadStationInfo()
resp = requests.get('https://traintime.lirr.org/api/Departure?loc=NYK')
data = json.loads(resp.text)
trains = data['TRAINS']
tz = pytz.timezone("America/New_York")
#Convert time to time
for train in trains:
train['time'] = tz.localize(parse(train['SCHED']))
train['destination'] = stations.loc[train['DEST']]
train['descrip'] = train['time'].strftime('%I:%M %p') + ' towards ' + train['destination']
df = pd.DataFrame(trains).set_index('TRAIN_ID')
return df
@app.route("/getTrains")
def getTrainsRest():
df = getTrains()
descriptions = df['descrip'].to_dict()
print(descriptions)
#Converts to list of lists so stays ordered, AND switches the key and the value
desc_list =[[v, u] for (u, v) in descriptions.items()]
headers = {'Content-Type' :'application/json'}
js = json.dumps(desc_list)
print(js)
return js,200,headers
@app.route("/selectTrain",methods=['POST','GET'])
def selectTrainAPI():
#put in check to see tingz there +valid
body = request.get_json()
train_id = body['train_id']
push_key = body['push_key']
print("New selecTrains Request with content:")
print("train_id :",train_id)
print("push_key :", push_key)
#check if train_id is valid
df = getTrains()
if(train_id in df.index):
#Check if train is in more than 40 min
depart = df.loc[train_id]['time']
print("departireTime : ", depart.isoformat())
tz = pytz.timezone("America/New_York")
now = datetime.datetime.now(tz = tz)
timeDiff = depart - now
print("Curr time : ", now.isoformat())
print("Time Diff", timeDiff)
if(timeDiff > datetime.timedelta(hours=1)):
print("Train leaves over an hour from now. We're not doing this sis")
sendNotification(push_key,'Error: Train leaves too long from now','Please request again less than an hour before departure.')
return {'status':403,'message':'Error: Train leaves over an hour from now. Please Select and Earlier train'},403
t = threading.Thread(target=selectTrain,args=(train_id,push_key,))
t.start()
return {'status':200,'message' : 'Successfully selected train. Notification setup.' },200
else:
return {'status':400,'message': 'Error: train_id not valid'},400
def selectTrain(train_id,push_key):
df = getTrains()
#error count
errorCount = 0
track = ''
while(track == ''):
df = getTrains()
if(train_id in df.index ):
track = df.loc[train_id]['TRACK']
if(track == ''):
print("Track not assigned yet. Try again in 5 seconds")
time.sleep(10)
else:
print('error: TRAIN_ID,',train_id,', not found in index, continuing')
print("Error Count:", errorCount)
time.sleep(2)
errorCount+=1
#if error more than 10 times, kill the thread
if(errorCount > 10):
print('Reached 10 errors, quitting thread')
return
#notify
string = df.loc[train_id]['descrip'] + " is on Track "+ track
print(string)
#send notification using pushover
sendNotification(push_key,'Track ' + track,string)
#send notification using pushover
def sendNotification(push_key,title,message):
data = {'token' : PUSHOVER_TOKEN,
'user' : push_key,
'title' : title,
'message' : message}
resp = requests.post('https://api.pushover.net/1/messages.json',params=data)
def waitForNext():
df = getTrains()
next = df[df['TRACK'] == ''].iloc[0]
print(next)
train_id = next.name
selectTrain(train_id)
def main():
df = getTrains()
print(df)
app.run('0.0.0.0')
#waitForNext()
pass
if __name__ == '__main__':
main()