diff -r c2fc50748efd -r d2e19670b772 service.py --- a/service.py Sat Oct 07 01:52:15 2017 +0300 +++ b/service.py Thu Oct 26 16:54:29 2017 +0300 @@ -91,6 +91,43 @@ def imminent(schedule_entry): return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) +def first_halt_in_trip_in_place(trip, place): + for halt in trip.schedule: + if halt.stop.region == place: + return halt + else: + return None + +place_abbreviations = ConfigParser() +place_abbreviations.read('abbreviations.ini') + +def place_abbreviation(place): + try: + return place_abbreviations['abbreviations'][place] + except KeyError: + return place + +def trip_abbreviation(trip): + entries = [trip.from_place] + old_places = None + starting_halt = None + while True: + remaining_length = trip.length + if starting_halt: + remaining_length -= starting_halt.traveled_distance + places = reduce_schedule(trip.concise_schedule(starting_stop = starting_halt), trip_length = remaining_length, long = False) + new_places = set(places) - set(entries) + if not new_places or places == old_places: + break + for place in places: + if place in new_places: + starting_halt = first_halt_in_trip_in_place(trip, place) + entries += [place] + break + old_places = places + if trip.to_place not in entries: + entries += [trip.to_place] + return trip.route.reference + ':' + '-'.join(map(place_abbreviation, entries)) @app.route('/stop/') def bus_stop_schedule(reference): @@ -120,6 +157,34 @@ tr = tr, ) +@app.route('/api/describe_destination///') +def describe_destination(trip_reference, stop_reference, index): + from buses import bus_stops, all_trips + from busroute import simplify_name + from flask import jsonify + try: + trip = all_trips[trip_reference] + bus_stop = bus_stops[stop] + schedule_entry = [schedule_entry for schedule_entry in trip.schedule if schedule_entry.stop.reference == stop_reference][index] + except KeyError: + abort(404) + except ValueError: + abort(404) + return jsonify(long_form_sign({'trip': trip, 'stop': schedule_entry})) + +@app.route('/api/trip_abbreviation//') +def api_trip_abbreviation(block_id, arrival_time_offset): + from buses import trips_by_vehicle_info + from flask import jsonify + from datetime import timedelta + try: + trip = trips_by_vehicle_info[block_id, timedelta(seconds = arrival_time_offset)] + except KeyError: + abort(404) + return jsonify({ + 'abbreviation': trip_abbreviation(trip), + }) + @app.route('/stop_display/') def stop_display(reference): from buses import bus_stops @@ -286,17 +351,25 @@ for halt in trip.schedule: stop_time = datetime.combine(today(), time()) + halt.arrival_time formatted_time = time_representation(stop_time) - if halt.stop.region != region and not (region and not halt.stop.region): - if len(schedule) and not schedule[-1]['name']: - schedule[-1]['name'] = tr(halt.stop.region or '', 'paikat') - else: - schedule.append({ - 'name': tr(halt.stop.region or '', 'paikat'), - 'time': formatted_time, - 'stops': [], - 'index': len(schedule), - }) - region = halt.stop.region + if profile['regions']['use-regions']: + if halt.stop.region != region and not (region and not halt.stop.region): + if len(schedule) and not schedule[-1]['name']: + schedule[-1]['name'] = tr(halt.stop.region or '', 'paikat') + else: + schedule.append({ + 'name': tr(halt.stop.region or '', 'paikat'), + 'time': formatted_time, + 'stops': [], + 'index': len(schedule), + }) + region = halt.stop.region + else: + schedule.append({ + 'name': tr(halt.stop.name or '', 'bus-stops'), + 'time': formatted_time, + 'stops': [], + 'index': len(schedule), + }) schedule[-1]['stops'].append({ 'time': formatted_time, 'id': halt.stop.reference, @@ -367,9 +440,8 @@ parser.add_argument('-d', '--debug', action = 'store_true') args = parser.parse_args() -profile = ConfigParser() profile.read(args.profile_path) -buses.load_buses(args.gtfs_zip_path, profile = profile) +buses.load_buses(args.gtfs_zip_path) if __name__ == '__main__': app.run(debug = args.debug, port = args.port)