From ec83af3a3ac1982f21009839e9b4917b8cbd0940 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 2 Jun 2023 00:47:24 +0100 Subject: [PATCH] Fix timetable update handling --- src/mongo.py | 1 + src/timetable.py | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/mongo.py b/src/mongo.py index a8225bd..e375cf1 100644 --- a/src/mongo.py +++ b/src/mongo.py @@ -152,6 +152,7 @@ def dropCollection(collection): def deleteTimetableData(query): collection = "timetable" col = db[collection] + log.out(f"mongo.deleteTimetableData: Deleting entry matching {query}") res = col.delete_one(query) def getMetaHash(target): diff --git a/src/timetable.py b/src/timetable.py index dbbdb9d..06d4d72 100644 --- a/src/timetable.py +++ b/src/timetable.py @@ -57,7 +57,9 @@ def getTimetable(full :bool = False): response = requests.get(downloadUrl, auth=(CORPUS_USER, CORPUS_PASS)) mongo.incrementCounter("schedule_api") log.out(f"timetable.getTimetable: Fetch (Full:{full}) response: {response.status_code}", "DBUG") - return zlib.decompress(response.content, 16+zlib.MAX_WBITS) + decompressed = zlib.decompress(response.content, 16+zlib.MAX_WBITS) + #print(decompressed) + return decompressed def loopTimetable(data): listify = data.splitlines() @@ -71,10 +73,10 @@ def loopTimetable(data): # Do something with this data here # Check if timestamp and sequence are correct, if not trigger a full download elif ('TiplocV1' in dic): - print("Disregarding TIPLOC Data") + pass # Not used as TIPLOCs etc. are sourced from CORPUS elif ('JsonAssociationV1' in dic): - print("JsonAssociationData") + pass # Associates trains with eachother - not planning to use yet. elif ('JsonScheduleV1' in dic): document = insertSchedule(dic) @@ -108,18 +110,22 @@ def insertSchedule(sch_record): else: now = datetime.now() scheduleStart = now.replace(hour=0,minute=0,second=0,microsecond=0) + if ('schedule_end_date' in schedule): + scheduleEnd = _helpParseDate(schedule['schedule_end_date'], "end") + else: + scheduleEnd = "null" document = { - 'transactionType': schedule['transaction_type'], - 'stpIndicator': schedule['CIF_stp_indicator'], + 'transactionType': schedule.get('transaction_type'), + 'stpIndicator': schedule.get('CIF_stp_indicator'), 'trainUid': scheduleId, - 'headcode': schedule['schedule_segment']['signalling_id'], - 'powerType': schedule['schedule_segment']['CIF_power_type'], - 'planSpeed': schedule['schedule_segment']['CIF_speed'], + 'headcode': schedule.get('schedule_segment', {}).get('signalling_id'), + 'powerType': schedule.get('schedule_segment', {}).get('CIF_power_type'), + 'planSpeed': schedule.get('schedule_segment', {}).get('CIF_speed'), 'scheduleStartDate': scheduleStart, - 'scheduleEndDate': _helpParseDate(schedule['schedule_end_date'], "end"), - 'daysRun': _helpParseDays(schedule['schedule_days_runs']) + 'scheduleEndDate': scheduleEnd, + 'daysRun': _helpParseDays(schedule.get('schedule_days_runs', '0000000')) } - if ('schedule_location' in schedule['schedule_segment']): + if ('schedule_location' in schedule.get('schedule_segment', {})): stops = _helpParseStops(schedule['schedule_segment']['schedule_location']) else: stops = [] @@ -137,7 +143,7 @@ def _insertToDb(data :list, type :str): singleList = [item] mongo.putTimetable(singleList) elif item['transactionType'] == "Delete": - mongo.deleteTimetableData({'trainUid': item.trainUid}) + mongo.deleteTimetableData({'trainUid': item['trainUid']}) return True #If Successful else False def _helpParseStops(schedule_segment):