Better canceled trains display
This commit is contained in:
@ -27,12 +27,17 @@ class Command(BaseCommand):
|
||||
feed_message = FeedMessage()
|
||||
feed_message.ParseFromString(requests.get(feed_url).content)
|
||||
|
||||
with open("toto.txt", "w") as f:
|
||||
f.write(str(feed_message))
|
||||
|
||||
stop_times_updates = []
|
||||
|
||||
for entity in feed_message.entity:
|
||||
if entity.HasField("trip_update"):
|
||||
trip_update = entity.trip_update
|
||||
trip_id = trip_update.trip.trip_id
|
||||
if feed_type in ["TGV", "IC", "TER"]:
|
||||
trip_id = trip_id.split(":", 1)[0]
|
||||
start_date = date(year=int(trip_update.trip.start_date[:4]),
|
||||
month=int(trip_update.trip.start_date[4:6]),
|
||||
day=int(trip_update.trip.start_date[6:]))
|
||||
@ -43,10 +48,13 @@ class Command(BaseCommand):
|
||||
trip_qs = Trip.objects.all()
|
||||
trip_ids = trip_qs.values_list('id', flat=True)
|
||||
|
||||
first_stop_queryset = StopTime.objects.filter(
|
||||
stop__parent_station_id=trip_update.stop_time_update[0].stop_id,
|
||||
).values('trip_id')
|
||||
last_stop_queryset = StopTime.objects.filter(
|
||||
stop__parent_station_id=trip_update.stop_time_update[-1].stop_id,
|
||||
).values('trip_id')
|
||||
trip_ids = trip_ids.intersection(last_stop_queryset)
|
||||
).values('trip_id')
|
||||
trip_ids = trip_ids.intersection(first_stop_queryset).intersection(last_stop_queryset)
|
||||
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
|
||||
stop_id = stop_time_update.stop_id
|
||||
st_queryset = StopTime.objects.filter(stop__parent_station_id=stop_id)
|
||||
@ -91,7 +99,6 @@ class Command(BaseCommand):
|
||||
"service_id": f"{feed_type}-new-{headsign}",
|
||||
"date": trip_update.trip.start_date,
|
||||
"exception_type": 1,
|
||||
"transport_type": feed_type,
|
||||
}
|
||||
)
|
||||
Trip.objects.update_or_create(
|
||||
@ -104,12 +111,13 @@ class Command(BaseCommand):
|
||||
}
|
||||
)
|
||||
|
||||
sample_trip = Trip.objects.filter(id__in=trip_ids).first()
|
||||
sample_trip = Trip.objects.filter(id__in=trip_ids, route_id=route_id).first()
|
||||
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
|
||||
stop_id = stop_time_update.stop_id
|
||||
stop = Stop.objects.get(id=stop_id)
|
||||
if stop.location_type == 1:
|
||||
if not StopTime.objects.filter(trip_id=trip_id).exists():
|
||||
print(trip_id, sample_trip.id)
|
||||
stop = StopTime.objects.get(trip_id=sample_trip.id,
|
||||
stop__parent_station_id=stop_id).stop
|
||||
elif StopTime.objects.filter(trip_id=trip_id, stop__parent_station_id=stop_id).exists():
|
||||
@ -120,18 +128,26 @@ class Command(BaseCommand):
|
||||
if s.stop_type in s2.stop.stop_type
|
||||
or s2.stop.stop_type in s.stop_type)
|
||||
stop_id = stop.id
|
||||
|
||||
arr_time = datetime.fromtimestamp(stop_time_update.arrival.time,
|
||||
tz=ZoneInfo("Europe/Paris")) - start_dt
|
||||
dep_time = datetime.fromtimestamp(stop_time_update.departure.time,
|
||||
tz=ZoneInfo("Europe/Paris")) - start_dt
|
||||
|
||||
pickup_type = 0 if stop_time_update.departure.time and stop_sequence > 0 else 1
|
||||
drop_off_type = 0 if stop_time_update.arrival.time \
|
||||
and stop_sequence < len(trip_update.stop_time_update) - 1 else 1
|
||||
|
||||
StopTime.objects.update_or_create(
|
||||
id=f"{trip_id}-{stop_id}",
|
||||
trip_id=trip_id,
|
||||
defaults={
|
||||
"stop_id": stop_id,
|
||||
"stop_sequence": stop_sequence,
|
||||
"arrival_time": datetime.fromtimestamp(stop_time_update.arrival.time,
|
||||
tz=ZoneInfo("Europe/Paris")) - start_dt,
|
||||
"departure_time": datetime.fromtimestamp(stop_time_update.departure.time,
|
||||
tz=ZoneInfo("Europe/Paris")) - start_dt,
|
||||
"pickup_type": 0 if stop_time_update.departure.time else 1,
|
||||
"drop_off_type": 0 if stop_time_update.arrival.time else 1,
|
||||
"arrival_time": arr_time,
|
||||
"departure_time": dep_time,
|
||||
"pickup_type": pickup_type,
|
||||
"drop_off_type": drop_off_type,
|
||||
}
|
||||
)
|
||||
|
||||
@ -139,11 +155,13 @@ class Command(BaseCommand):
|
||||
self.stdout.write(f"Trip {trip_id} does not exist in the GTFS feed.")
|
||||
continue
|
||||
|
||||
tu, _created = TripUpdate.objects.get_or_create(
|
||||
tu, _created = TripUpdate.objects.update_or_create(
|
||||
trip_id=trip_id,
|
||||
start_date=trip_update.trip.start_date,
|
||||
start_time=trip_update.trip.start_time,
|
||||
schedule_relationship=trip_update.trip.schedule_relationship,
|
||||
defaults=dict(
|
||||
schedule_relationship=trip_update.trip.schedule_relationship,
|
||||
)
|
||||
)
|
||||
|
||||
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
|
||||
@ -174,9 +192,9 @@ class Command(BaseCommand):
|
||||
elif stop_time_update.schedule_relationship == 1:
|
||||
st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),
|
||||
trip_id=trip_id)
|
||||
if st.pickup_type != 0 or st.drop_off_type != 0:
|
||||
st.pickup_type = 0
|
||||
st.drop_off_type = 0
|
||||
if st.pickup_type != 1 or st.drop_off_type != 1:
|
||||
st.pickup_type = 1
|
||||
st.drop_off_type = 1
|
||||
st.save()
|
||||
else:
|
||||
st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),
|
||||
|
Reference in New Issue
Block a user