This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
forked from WRI-Cities/static-GTFS-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GTFSManager.py
1432 lines (1183 loc) · 49.9 KB
/
GTFSManager.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
print('\n\nstatic GTFS Manager')
print('Fork it on Github: https://github.com/WRI-Cities/static-GTFS-manager/')
print('Starting up the program, loading dependencies, please wait...\n\n')
import tornado.web
import tornado.ioloop
import json
import os
import time, datetime
import xmltodict
import pandas as pd
from collections import OrderedDict
import zipfile, zlib
from tinydb import TinyDB, Query
from tinydb.operations import delete
import webbrowser
from Cryptodome.PublicKey import RSA #uses pycryptodomex package.. disambiguates from pycrypto, pycryptodome
import shutil # used in fareChartUpload to fix header if changed
import pathlib
from math import sin, cos, sqrt, atan2, radians # for lat-long distance calculations
# import requests # nope, not needed for now
from json.decoder import JSONDecodeError # used to catch corrupted DB file when tinyDB loads it.
import signal, sys # for catching Ctrl+C and exiting gracefully.
import gc # garbage collector, from https://stackoverflow.com/a/1316793/4355695
import csv
import numpy as np
import io # used in hyd csv import
import requests, platform # used to log user stats
# setting constants
root = os.path.dirname(__file__) # needed for tornado
uploadFolder = os.path.join(root,'uploads/')
xmlFolder = os.path.join(root,'xml_related/')
logFolder = os.path.join(root,'logs/')
configFolder = os.path.join(root,'config/')
dbFolder = os.path.join(root,'db/') # 12.5.18 new pandas DB storage
exportFolder = os.path.join(root,'export/') # 4.9.18 putting exports here now
sequenceDBfile = os.path.join(root,'db/sequence.json')
passwordFile = os.path.join(root,'pw/rsa_key.bin')
chunkRulesFile = 'chunkRules.json'
configFile = 'config.json'
thisURL = ''
debugMode = False # using this flag at various places to do or not do things based on whether we're in development or production
requiredFeeds = ['agency.txt','calendar.txt','stops.txt','routes.txt','trips.txt','stop_times.txt']
optionalFeeds = ['calendar_dates.txt','fare_attributes.txt','fare_rules.txt','shapes.txt','frequencies.txt','transfers.txt','feed_info.txt']
# for checking imported ZIP against
# to do: don't make this a HARD requirement. Simply logmessage about it.
# load parameters from config folder
with open(configFolder + chunkRulesFile) as f:
chunkRules = json.load(f)
with open(configFolder + configFile) as f:
configRules = json.load(f)
# create folders if they don't exist
for folder in [uploadFolder, xmlFolder, logFolder, configFolder, dbFolder, exportFolder]:
if not os.path.exists(folder):
os.makedirs(folder)
# importing GTFSserverfunctions.py, embedding it inline to avoid re-declarations etc
exec(open(os.path.join(root,"GTFSserverfunctions.py"), encoding='utf8').read())
exec(open(os.path.join(root,"xml2GTFSfunction.py"), encoding='utf8').read())
exec(open(os.path.join(root,"hydCSV2GTFS.py"), encoding='utf8').read())
logmessage('Loaded dependencies, starting static GTFS Manager program.')
'''
# Tornado API functions template:
class APIHandler(tornado.web.RequestHandler):
def get(self):
#get the Argument that User had passed as name in the get request
userInput=self.get_argument('name')
welcomeString=sayHello(userInput)
#return this as JSON
self.write(json.dumps(welcomeString))
def post(self):
user = self.get_argument("username")
passwd = self.get_argument("password")
time.sleep(10)
self.write("Your username is %s and password is %s" % (user, passwd))
'''
class allStops(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nallStops GET call')
allStopsJson = readTableDB('stops').to_json(orient='records', force_ascii=False)
self.write(allStopsJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("allStops GET call took {} seconds.".format(round(end-start,2)))
def post(self):
start = time.time()
logmessage('\nallStops POST call')
pw=self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
if replaceTableDB('stops', data): #replaceTableDB(tablename, data)
self.write('Saved stops data to DB.')
else:
self.set_status(400)
self.write("Error: Could not save to DB.")
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("allStops POST call took {} seconds.".format(round(end-start,2)))
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
def options(self):
# no body
self.set_status(204)
self.finish()
class allStopsKeyed(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nallStopsKeyed GET call')
stopsDF = readTableDB('stops')
# putting in a check for empty df, because set_index() errors out with empty df.
if len(stopsDF):
keyedStopsJson = stopsDF.set_index('stop_id').to_json(orient='index', force_ascii=False)
# change index to stop_id and make json keyed by index
else : keyedStopsJson = '{}'
self.write(keyedStopsJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("allStopsKeyed GET call took {} seconds.".format(round(end-start,2)))
class routes(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nroutes GET call')
allRoutesJson = readTableDB('routes').to_json(orient='records', force_ascii=False)
self.write(allRoutesJson)
end = time.time()
logmessage("routes GET call took {} seconds.".format(round(end-start,2)))
def post(self):
start = time.time()
logmessage('\nroutes POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
# writing back to db now
if replaceTableDB('routes', data): #replaceTableDB(tablename, data)
self.write('Saved routes data to DB')
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("routes POST call took {} seconds.".format(round(end-start,2)))
class fareAttributes(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nfareAttributes GET call')
fareAttributesJson = readTableDB('fare_attributes').to_json(orient='records', force_ascii=False)
self.write(fareAttributesJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("fareAttributes GET call took {} seconds.".format(round(end-start,2)))
def post(self):
# API/fareAttributes
start = time.time()
logmessage('\nfareAttributes POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
# writing back to db
if replaceTableDB('fare_attributes', data): #replaceTableDB(tablename, data)
self.write('Saved Fare Attributes data to DB.')
else:
self.set_status(400)
self.write("Error: could not save to DB.")
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("API/fareAttributes POST call took {} seconds.".format(round(end-start,2)))
class fareRules(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nfareRules GET call')
fareRulesSimpleJson = readTableDB('fare_rules').to_json(orient='records', force_ascii=False)
self.write(fareRulesSimpleJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("fareRules GET call took {} seconds.".format(round(end-start,2)))
def post(self):
# API/fareRules
start = time.time()
logmessage('\nfareRules POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
# writing back to db
if replaceTableDB('fare_rules', data): #replaceTableDB(tablename, data)
self.write('Saved Fare Rules data to DB.')
else:
self.set_status(400)
self.write("Error: could not save to DB.")
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("API/fareRules POST call took {} seconds.".format(round(end-start,2)))
class fareRulesPivoted(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nfareRulesPivoted GET call')
fareRulesDf = readTableDB('fare_rules')
# do pivoting operation only if there is data. Else send a blank array.
# Solves part of https://github.com/WRI-Cities/static-GTFS-manager/issues/35
if len(fareRulesDf):
df = fareRulesDf.drop_duplicates()
# skipping duplicate entries if any, as pivoting errors out if there are duplicates.
fareRulesPivotedJson = df.pivot(index='origin_id',\
columns='destination_id', values='fare_id')\
.reset_index()\
.rename(columns={'origin_id':'zone_id'})\
.to_json(orient='records', force_ascii=False)
else:
fareRulesPivotedJson = '[]'
# multiple pandas ops..
# .pivot() : pivoting. Keep origin as vertical axis, destination as horizontal axis, and fill the 2D matrix with values from fare_id column.
# .reset_index() : move the index in as a column so that we can export it to dict further along. from https://stackoverflow.com/a/20461206/4355695
# .rename() : rename the index column which we've moved in. Proper name is zone_id.
# .to_dict(orient='records', into=OrderedDict) : convert to flat list of dicts. from https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html
# to do: if we get a route or two, then order these by the route's sequence.
self.write(fareRulesPivotedJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("fareRulesPivoted GET call took {} seconds.".format(round(end-start,2)))
def post(self):
# API/fareRulesPivoted?pw=${pw}
start = time.time()
logmessage('\nfareRulesPivoted POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
# need to unpivot this. We basically need to do the exact same steps as the get(self) function did, in reverse.
df = pd.DataFrame(data)
fareRulesArray = pd.melt(df, id_vars=['zone_id'],\
var_name='destination_id',value_name='fare_id')\
.rename(columns={'zone_id': 'origin_id'})\
.replace('', pd.np.nan)\
.dropna()\
.sort_values(by=['origin_id','destination_id'])\
.to_dict('records',into=OrderedDict)
# pandas: chained many commands together. explaining..
# .melt(df.. : that's the UNPIVOT command. id_vars: columns to keep. Everthing else "melts" down. var_name: new column name of the remaining cols serialized into one.
# .rename(.. : renaming the zone_id ('from' station) to origin_id
# .replace(.. At frontend some cells might have been set to blank. That comes thru as empty strings instead of null/NaN values. This replaces all empty strings with NaN, so they can be dropped subsequently. From https://stackoverflow.com/a/29314880/4355695
# .dropna() : drop all entries having null/None values. Example ALVA to ALVA has nothing; drop it.
# sort_values(.. : sort the table by col1 then col2
# to_dict(.. : output as an OrderedDict.
# writing back to db
if replaceTableDB('fare_rules', fareRulesArray):
self.write('Saved Fare Rules data to DB.')
else:
self.set_status(400)
self.write("Error: could not save to DB.")
end = time.time()
logmessage("API/fareAttributes POST call took {} seconds.".format(round(end-start,2)))
class agency(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nagency GET call')
agencyJson = readTableDB('agency').to_json(orient='records', force_ascii=False)
self.write(agencyJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("agency GET call took {} seconds.".format(round(end-start,2)))
def post(self):
start = time.time()
logmessage('\nagency POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
if replaceTableDB('agency', data): #replaceTableDB(tablename, data)
self.write('Saved Agency data to DB.')
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("saveAgency POST call took {} seconds.".format(round(end-start,2)))
class sequence(tornado.web.RequestHandler):
def get(self):
# API/sequence?route=${route_id}
start = time.time()
logmessage('\nsequence GET call')
route_id = self.get_argument('route',default='')
if not len(route_id):
self.set_status(400)
self.write("Error: invalid route.")
return
#to do: first check in sequence db. If not found there, then for first time, scan trips and stop_times to load sequence. And store that sequence in sequence db so that next time we fetch from there.
sequence = sequenceReadDB(sequenceDBfile, route_id)
# read sequence db and return sequence array. If not found in db, return false.
message = '<span class="alert alert-success">Loaded default sequence for this route from DB.</span>'
if not sequence:
logmessage('sequence not found in sequence DB file, so extracting from gtfs tables instead.')
# Picking the first trip instance for each direction of the route.
sequence = extractSequencefromGTFS(route_id)
if sequence == [ [], [] ] :
message = '<span class="alert alert-info">This seems to be a new route. Please create a sequence below and save to DB.</span>'
else:
message = '<span class="alert alert-warning">Loaded a computed sequence from existing trips. Please finalize and save to DB.</span>'
# we have computed a sequence from the first existing trip's entry in trips and stop_times tables for that route (one sequence for each direction)
# Passing it along. Let the user finalize it and consensually save it.
# so either way, we now have a sequence array.
returnJson = { 'data':sequence, 'message':message }
self.write(json.dumps(returnJson))
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("sequence GET call took {} seconds.".format(round(end-start,2)))
#using same API endpoint, post request for saving.
def post(self):
# ${APIpath}sequence?pw=${pw}&route=${selected_route_id}&shape0=${chosenShape0}&shape1=${chosenShape1}
start = time.time()
logmessage('\nsequence POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
route_id = self.get_argument('route', default='')
shape0 = self.get_argument('shape0', default='')
shape1 = self.get_argument('shape1', default='')
if not len(route_id):
self.set_status(400)
self.write("Error: invalid route.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
data = json.loads( self.request.body.decode('UTF-8') )
'''
This is what the data would look like: [
['ALVA','PNCU','CPPY','ATTK','MUTT','KLMT','CCUV','PDPM','EDAP','CGPP','PARV','JLSD','KALR','LSSE','MGRD'],
['MACE','MGRD','LSSE','KALR','JLSD','PARV','CGPP','EDAP','PDPM','CCUV','KLMT','MUTT','ATTK','CPPY','PNCU','ALVA']
];
'''
# to do: the shape string can be empty. Or one of the shapes might be there and the other might be an empty string. Handle it gracefully.
# related to https://github.com/WRI-Cities/static-GTFS-manager/issues/35
# and : https://github.com/WRI-Cities/static-GTFS-manager/issues/38
shapes = [shape0, shape1]
if sequenceSaveDB(sequenceDBfile, route_id, data, shapes):
self.write('saved sequence to sequence db file.')
else:
self.set_status(400)
self.write("Error, could not save to sequence db for some reason.")
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("API/sequence POST call took {} seconds.".format(round(end-start,2)))
class trips(tornado.web.RequestHandler):
def get(self):
# API/trips?route=${route_id}
start = time.time()
logmessage('\ntrips GET call')
route_id = self.get_argument('route', default='')
if not len(route_id):
self.set_status(400)
self.write("Error: invalid route.")
return
tripsArray = readTableDB('trips', key='route_id', value=route_id).to_dict(orient='records')
# also read sequence for that route and send.
sequence = sequenceFull(sequenceDBfile, route_id)
# if there is no sequence saved yet, sequence=False which will be caught on JS side to inform the user and disable new trips creation.
returnJson = {'trips':tripsArray, 'sequence':sequence }
self.write(json.dumps(returnJson))
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("trips GET call took {} seconds.".format(round(end-start,2)))
def post(self):
start = time.time() # time check, from https://stackoverflow.com/a/24878413/4355695
# ${APIpath}trips?pw=${pw}&route=${route_id}
logmessage('\ntrips POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
route_id = self.get_argument('route',default='')
if not len(route_id) :
self.set_status(400)
self.write("Error: invalid route_id.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
tripsData = json.loads( self.request.body.decode('UTF-8') )
# heres where all the action happens:
result = replaceTableDB('trips', tripsData, key='route_id', value=route_id)
if result:
self.write('Saved trips data for route '+route_id)
else:
self.set_status(400)
self.write("Some error happened.")
end = time.time()
logmessage("trips POST call took {} seconds.".format(round(end-start,2)))
class stopTimes(tornado.web.RequestHandler):
def get(self):
# API/stopTimes?trip=${trip_id}&route=${route_id}&direction=${direction_id}
start = time.time()
logmessage('\nstopTimes GET call')
trip_id = self.get_argument('trip', default='')
route_id = self.get_argument('route', default='')
direction_id = int(self.get_argument('direction',default=0))
returnMessage = ''
if not ( len(trip_id) and len(route_id) ):
self.set_status(400)
self.write("Error: Invalid trip or route ID given.")
return
tripInTrips = readTableDB('trips', 'trip_id', trip_id)
if not len(tripInTrips):
self.set_status(400)
self.write("Error: Please save this trip to DB in the Trips tab first.")
return
stoptimesDf = readTableDB('stop_times', 'trip_id', trip_id)
# this will simply be empty if the trip doesn't exist yet
stoptimesArray = stoptimesDf.to_dict(orient='records')
if len(stoptimesArray):
returnMessage = 'Loaded timings from stop_times table.'
newFlag = False
else:
returnMessage = 'This trip is new. Loading default sequence, please fill in timings and save to DB.'
newFlag = True
returnJson = {'data':stoptimesArray, 'message':returnMessage, 'newFlag':newFlag }
# let's send back not just the array but even the message to display.
logmessage('returnJson.message:',returnJson['message'])
self.write(json.dumps(returnJson))
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("stopTimes GET call took {} seconds.".format(round(end-start,2)))
def post(self):
# ${APIpath}stopTimes?pw=${pw}&trip=${trip_id}
start = time.time() # time check, from https://stackoverflow.com/a/24878413/4355695
logmessage('\nstopTimes POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
trip_id = self.get_argument('trip', default='')
if not len(trip_id) :
self.set_status(400)
self.write("Error: invalid trip_id.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
timingsData = json.loads( self.request.body.decode('UTF-8') )
# heres where all the action happens:
result = replaceTableDB('stop_times', timingsData, key='trip_id', value=trip_id)
if result:
self.write('Changed timings data for trip '+trip_id)
else:
self.set_status(400)
self.write("Some error happened.")
end = time.time()
logmessage("stopTimes POST call took {} seconds.".format(round(end-start,2)))
class routeIdList(tornado.web.RequestHandler):
def get(self):
# API/routeIdList
start = time.time()
logmessage('\nrouteIdList GET call')
#routesArray = readTableDB('routes')
#route_id_list = [ n['route_id'] for n in routesArray ]
route_id_list = readColumnDB('routes','route_id')
self.write(json.dumps(route_id_list))
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("routeIdList GET call took {} seconds.".format(round(end-start,2)))
class tripIdList(tornado.web.RequestHandler):
def get(self):
# API/tripIdList
start = time.time()
logmessage('\ntripIdList GET call')
trip_id_list = readColumnDB('trips','trip_id')
self.write(json.dumps(trip_id_list))
# db.close()
end = time.time()
logmessage("tripIdList GET call took {} seconds.".format(round(end-start,2)))
class calendar(tornado.web.RequestHandler):
def get(self):
# API/calendar?current=y
start = time.time() # time check
logmessage('\ncalendar GET call')
current = self.get_argument('current',default='')
if current.lower() == 'y':
calendarJson = calendarCurrent().to_json(orient='records', force_ascii=False)
else:
calendarJson = readTableDB('calendar').to_json(orient='records', force_ascii=False)
self.write(calendarJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("calendar GET call took {} seconds.".format(round(end-start,2)))
def post(self):
# API/calendar?pw=${pw}
start = time.time() # time check
logmessage('\ncalendar POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
calendarData = json.loads( self.request.body.decode('UTF-8') )
#csvwriter(calendarData,'calendar.txt')
replaceTableDB('calendar', calendarData)
self.write('Saved Calendar data to DB.')
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("calendar POST call took {} seconds.".format(round(end-start,2)))
class serviceIds(tornado.web.RequestHandler):
def get(self):
# API/serviceIds
start = time.time() # time check
logmessage('\nserviceIds GET call')
service_id_list = serviceIdsFunc()
self.write(json.dumps(service_id_list))
end = time.time()
logmessage("serviceIds GET call took {} seconds.".format(round(end-start,2)))
class stats(tornado.web.RequestHandler):
def get(self):
# API/stats
start = time.time()
logmessage('\nstats GET call')
stats = GTFSstats()
self.write(json.dumps(stats))
end = time.time()
logmessage("stats GET call took {} seconds.".format( round(end-start, 2) ) )
logUse('stats')
class gtfsImportZip(tornado.web.RequestHandler):
def post(self):
# API/gtfsImportZip?pw=${pw}
start = time.time()
logmessage('\ngtfsImportZip GET call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
zipname = uploadaFile( self.request.files['gtfsZipFile'][0] )
if importGTFS(zipname):
self.write(zipname)
else:
self.set_status(400)
self.write("Error: invalid GTFS feed.")
end = time.time()
logmessage("gtfsImportZip POST call took {} seconds.".format( round(end-start,2) ))
logUse('gtfsImportZip')
class commitExport(tornado.web.RequestHandler):
def get(self):
# API/commitExport?commit=${commit}
start = time.time()
logmessage('\ncommitExport GET call')
commit = self.get_argument('commit', default='')
if not len(commit):
self.set_status(400)
self.write("Error: invalid commit name.")
return
commitFolder = exportFolder + '{:%Y-%m-%d-}'.format(datetime.datetime.now()) + commit + '/'
finalmessage = exportGTFS(commitFolder)
# this is the main function. it's in GTFSserverfunctions.py
self.write(finalmessage)
end = time.time()
logmessage("commitExport GET call took {} seconds.".format(round(end-start,2)))
logUse('commitExport')
class pastCommits(tornado.web.RequestHandler):
def get(self):
# API/pastCommits
start = time.time()
logmessage('\npastCommits GET call')
dirnames = []
for root, dirs, files in os.walk(exportFolder):
for folder in dirs:
if os.path.isfile(exportFolder + folder + '/gtfs.zip'):
dirnames.append(folder)
if not len(dirnames):
self.set_status(400)
self.write("No past commits found.")
return
# reversing list, from
dirnames = dirnames[::-1]
writeback = { "commits": dirnames }
self.write(json.dumps(writeback))
end = time.time()
logmessage("pastCommits GET call took {} seconds.".format(round(end-start,2)))
class XMLUpload(tornado.web.RequestHandler):
def post(self):
# `${APIpath}XMLUpload?pw=${pw}&depot=${depot}`,
start = time.time()
logmessage('\nXMLUpload GET call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# pass form file objects to uploadaFile funciton, get filenames in return
weekdayXML = uploadaFile( self.request.files['weekdayXML'][0] )
sundayXML = uploadaFile( self.request.files['sundayXML'][0] )
depot = self.get_argument('depot', default='')
if( depot == 'None' or depot == ''):
depot = None
diagnoseData = diagnoseXMLs(weekdayXML, sundayXML, depot)
# function diagnoseXMLs returns dict having keys: report, weekdaySchedules, sundaySchedules
if diagnoseData is False:
self.set_status(400)
self.write("Error: invalid xml(s).")
return
returnJson = {'weekdayXML':weekdayXML, 'sundayXML':sundayXML }
returnJson.update(diagnoseData)
self.write(json.dumps(returnJson))
end = time.time()
logmessage("XMLUpload POST call took {} seconds.".format(round(end-start,2)))
logUse('XMLUpload')
class XMLDiagnose(tornado.web.RequestHandler):
def get(self):
# `${APIpath}XMLDiagnose?weekdayXML=${weekdayXML}&sundayXML=${sundayXML}&depot=${depot}`
start = time.time()
logmessage('\nXMLDiagnose GET call')
weekdayXML = self.get_argument('weekdayXML', default='')
sundayXML = self.get_argument('sundayXML', default='')
if not ( len(weekdayXML) and len(sundayXML) ):
self.set_status(400)
self.write("Error: invalid xml(s).")
return
depot = self.get_argument('depot', default='')
if( depot == 'None' or depot == ''):
depot = None
diagnoseData = diagnoseXMLs(weekdayXML, sundayXML, depot)
# function diagnoseXMLs returns dict having keys: report, weekdaySchedules, sundaySchedules
if diagnoseData is False:
self.set_status(400)
self.write("Error: invalid xml(s), diagnoseData function failed.")
return
returnJson = {'weekdayXML':weekdayXML, 'sundayXML':sundayXML }
returnJson.update(diagnoseData)
self.write(json.dumps(returnJson))
end = time.time()
logmessage("XMLDiagnose GET call took {} seconds.".format(round(end-start,2)))
logUse('XMLDiagnose')
class stations(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\nstations GET call')
stationsArray = pd.read_csv(xmlFolder + "stations.csv", na_filter=False).to_dict('records')
self.write(json.dumps(stationsArray))
end = time.time()
logmessage("stations GET call took {} seconds.".format(round(end-start,2)))
def post(self):
start = time.time()
logmessage('\nstations POST call')
stationsArray = pd.read_csv(xmlFolder + "stations.csv", na_filter=False).to_dict('records')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
data = json.loads( self.request.body.decode('UTF-8') )
if stationsArray == data :
self.write('No changes to save.')
else:
csvwriter(data, xmlFolder + 'stations.csv')
self.write('Saved changes to stations.csv.')
end = time.time()
logmessage("stations POST call took {} seconds.".format(round(end-start,2)))
class fareChartUpload(tornado.web.RequestHandler):
def post(self):
# `${APIpath}fareChartUpload?pw=${pw}`,
start = time.time()
logmessage('\nfareChartUpload POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# pass form file objects to uploadaFile funciton, get filenames in return
fareChart = uploadaFile( self.request.files['fareChart'][0] )
# idiot-proofing : What if the only column header in the file 'Stations' is named to something else?
# We can do a quick replace of first word before (,) in first line
# from https://stackoverflow.com/a/14947384/4355695
targetfile = uploadFolder + fareChart
from_file = open(targetfile, encoding='utf8')
line = from_file.readline()
lineArray = line.split(',')
if lineArray[0] != 'Stations':
logmessage('Fixing header on ' + fareChart + ' so the unpivot doesn\'t error out.')
lineArray[0] = 'Stations'
line = ','.join(lineArray)
to_file = open(targetfile,mode="w",encoding='utf8')
to_file.write(line)
shutil.copyfileobj(from_file, to_file)
to_file.close()
from_file.close()
try:
fares_array = csvunpivot(uploadFolder + fareChart, ['Stations'], 'destination_id', 'fare_id', ['fare_id','Stations','destination_id']).to_dict('records')
except:
self.set_status(400)
self.write("Error: invalid file.")
return
fare_id_set = set()
fare_id_set.update([ row['fare_id'] for row in fares_array ])
# this set is having a null value, NaN as well.
# need to lose the NaN man
# from https://stackoverflow.com/a/37148508/4355695
faresList = [x for x in fare_id_set if x==x]
faresList.sort()
logmessage(faresList)
report = 'Loaded Fares Chart successfully.'
returnJson = {'report':report, 'faresList':faresList }
self.write(json.dumps(returnJson))
end = time.time()
logmessage("fareChartUpload POST call took {} seconds.".format(round(end-start,2)))
logUse('fareChartUpload')
class xml2GTFS(tornado.web.RequestHandler):
def post(self):
# `${APIpath}xml2GTFS?pw=${pw}`
start = time.time()
logmessage('\nxml2GTFS POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
configdata = json.loads( self.request.body.decode('UTF-8') )
logmessage(configdata)
# and so it begins! Ack lets pass it to a function.
returnMessage = xml2GTFSConvert(configdata)
if not len(returnMessage):
self.set_status(400)
returnMessage = 'Import was unsuccessful, please debug on python side.'
self.write(returnMessage)
end = time.time()
logmessage("xml2GTFS POST call took {} seconds.".format(round(end-start,2)))
logUse('xml2GTFS')
class gtfsBlankSlate(tornado.web.RequestHandler):
def get(self):
# API/gtfsBlankSlate?pw=${pw}
start = time.time()
logmessage('\ngtfsBlankSlate GET call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# take backup first, if we're not in debug mode.
if not debugMode:
backupDB()
finalmessage = '<font color=green size=6>✔</font> Took a backup and cleaned out the DB.'
else:
finalmessage = '<font color=green size=6>✔</font> Cleaned out the DB.'
# outsourced purging DB to a function
purgeDB()
self.write(finalmessage)
end = time.time()
logmessage("gtfsBlankSlate GET call took {} seconds.".format(round(end-start,2)))
logUse('gtfsBlankSlate')
class translations(tornado.web.RequestHandler):
def get(self):
start = time.time()
logmessage('\ntranslations GET call')
translationsJson = readTableDB('translations').to_json(orient='records', force_ascii=False)
self.write(translationsJson)
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("translations GET call took {} seconds.".format(round(end-start,2)))
def post(self):
# API/translations?pw=${pw}
start = time.time() # time check
logmessage('\ntranslations POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
# received text comes as bytestring. Convert to unicode using .decode('UTF-8') from https://stackoverflow.com/a/6273618/4355695
translationsData = json.loads( self.request.body.decode('UTF-8') )
replaceTableDB('translations', translationsData)
self.write('Saved Translations data to DB.')
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("translations POST call took {} seconds.".format(round(end-start,2)))
class shapesList(tornado.web.RequestHandler):
def get(self):
# API/shapesList?route=${route_id}
start = time.time()
logmessage('\nshapesList GET call')
route_id = self.get_argument('route','')
if not len(route_id):
self.set_status(400)
self.write("Error: invalid route.")
return
shapeIDsJson = {}
df = readTableDB('trips', key='route_id', value=route_id)
# since shape_id is an optional column, handle gracefully if column not present.
if 'shape_id' not in df.columns:
shapeIDsJson = { '0':[], '1':[] }
self.write(json.dumps(shapeIDsJson))
del df
gc.collect()
return
# get shape_id's used by that route and direction. Gets rid of blanks and NaNs, gets unique list and outputs as list.
shapeIDsJson['0'] = df[ (df.direction_id == '0') ].shape_id.replace('', pd.np.nan).dropna().unique().tolist()
shapeIDsJson['1'] = df[ (df.direction_id == '1') ].shape_id.replace('', pd.np.nan).dropna().unique().tolist()
self.write(json.dumps(shapeIDsJson))
del df
gc.collect()
# time check, from https://stackoverflow.com/a/24878413/4355695
end = time.time()
logmessage("shapesList GET call took {} seconds.".format(round(end-start,2)))
class shape(tornado.web.RequestHandler):
def post(self):
# ${APIpath}shape?pw=${pw}&route=${route_id}&id=${shape_id}&reverseFlag=${reverseFlag}
start = time.time()
logmessage('\nshape POST call')
pw = self.get_argument('pw',default='')
if not decrypt(pw):
self.set_status(400)
self.write("Error: invalid password.")
return
route_id = self.get_argument('route', default='')
shapePrefix = self.get_argument('id', default='')
reverseFlag = self.get_argument('reverseFlag', default=False) == 'true'
logmessage(route_id)
logmessage(shapePrefix)
logmessage(reverseFlag)
if not ( len(route_id) and len(shapePrefix) ):
self.set_status(400)
self.write("Error: Invalid route or shape id prefix.")
return
shapeArray = False