def _add_datetime(self, stop_schedule, passage, add_direction): new_dt = stop_schedule.date_times.add() # the midnight is calculated from passage.datetime and it keeps the same timezone as passage.datetime midnight = passage.datetime.replace(hour=0, minute=0, second=0, microsecond=0) time = (passage.datetime - midnight).total_seconds() new_dt.time = int(time) new_dt.date = date_to_timestamp(midnight) if passage.is_real_time: new_dt.realtime_level = type_pb2.REALTIME else: new_dt.realtime_level = type_pb2.BASE_SCHEDULE # we also add the direction in the note if add_direction and passage.direction: note = type_pb2.Note() note.note = passage.direction note_uri = hashlib.md5( note.note.encode('utf-8', 'backslashreplace')).hexdigest() note.uri = 'note:{md5}'.format( md5=note_uri ) # the id is a md5 of the direction to factorize them new_dt.properties.notes.extend([note])
def _update_stop_schedule(stop_schedule, next_realtime_passages): """ Update the stopschedule response with the new realtime passages for the moment we remove all base schedule data and replace them with the realtime If next_realtime_passages is None (and not if it's []) it means that the proxy failed, so we use the base schedule """ if next_realtime_passages is None: return logging.getLogger(__name__).debug('next passages: : {}' .format(["dt: {}".format(d.datetime) for d in next_realtime_passages])) # we clean up the old schedule del stop_schedule.date_times[:] for passage in next_realtime_passages: new_dt = stop_schedule.date_times.add() midnight = datetime.datetime.combine(passage.datetime.date(), time=datetime.time(0)) pytz.utc.localize(midnight) midnight = midnight.replace(tzinfo=pytz.UTC) time = (passage.datetime - midnight).total_seconds() new_dt.time = int(time) new_dt.date = date_to_timestamp(midnight) new_dt.realtime_level = type_pb2.REALTIME # we also add the direction in the note if passage.direction: note = type_pb2.Note() note.note = passage.direction note_uri = hashlib.md5(note.note.encode('utf-8')).hexdigest() note.uri = 'note:{md5}'.format(md5=note_uri) # the id is a md5 of the direction to factorize them new_dt.properties.notes.extend([note])
def _update_stop_schedule(self, stop_schedule, next_realtime_passages): """ Update the stopschedule response with the new realtime passages By default we remove all base schedule data and replace them with the realtime Each proxy can define is own way to merge passages If next_realtime_passages is None (and not if it's []) it means that the proxy failed, so we use the base schedule """ if next_realtime_passages is None: return logging.getLogger(__name__).debug('next passages: : {}'.format( ["dt: {}".format(d.datetime) for d in next_realtime_passages])) # we clean up the old schedule pb_del_if(stop_schedule.date_times, self._filter_base_stop_schedule) for passage in next_realtime_passages: new_dt = stop_schedule.date_times.add() # the midnight is calculated from passage.datetime and it keeps the same timezone as passage.datetime midnight = passage.datetime.replace(hour=0, minute=0, second=0, microsecond=0) time = (passage.datetime - midnight).total_seconds() new_dt.time = int(time) new_dt.date = date_to_timestamp(midnight) if passage.is_real_time: new_dt.realtime_level = type_pb2.REALTIME else: new_dt.realtime_level = type_pb2.BASE_SCHEDULE # we also add the direction in the note if passage.direction: note = type_pb2.Note() note.note = passage.direction note_uri = hashlib.md5( note.note.encode('utf-8', 'backslashreplace')).hexdigest() note.uri = 'note:{md5}'.format( md5=note_uri ) # the id is a md5 of the direction to factorize them new_dt.properties.notes.extend([note]) stop_schedule.date_times.sort(key=lambda dt: dt.date + dt.time)