# HG changeset patch # User Teemu Piippo # Date 1506325583 -10800 # Node ID 382dd85b83a2b3067e2ac0a1d501a150abaa7be4 # Parent 336c750b7b5da325aaead497d45a220bdcfbb056 Asioita diff -r 336c750b7b5d -r 382dd85b83a2 busroute.py --- a/busroute.py Sat Sep 23 01:23:55 2017 +0300 +++ b/busroute.py Mon Sep 25 10:46:23 2017 +0300 @@ -45,7 +45,7 @@ name = abbreviations.get(name, name) return name -def reduce_schedule(reitti, trip_length, whole = False): +def reduce_schedule(reitti, trip_length, whole = False, long = False): length = ((trip_length / 600) * 3 + len(reitti) * 2) / 5 if not reitti: return '' @@ -62,12 +62,12 @@ priorities = { 'ihala': 150, 'kauppakeskus mylly': 200, - 'kuninkoja': 80, + 'kuninkoja': 150, 'moikoinen': 80, 'raision keskusta': 150, 'kukola': 50, 'kaistarniemi': 50, - 'papinsaari': 50, + 'papinsaari': 80, 'oriniemi': 50, 'pikisaari': 50, 'häppilä': 20, @@ -76,8 +76,8 @@ 'halinen': 70, 'kakskerta': 200, 'pansio': 50, - 'räntämäki': 10, - 'verkahovi': 20, + 'räntämäki': 25, + 'verkahovi': 25, 'skanssi': 200, 'vaala': 20, 'varissuo': 60, @@ -110,7 +110,7 @@ 'marttila': 100, 'koski tl': 100, 'katariina': 50, - 'länsikeskus': 50, + 'länsikeskus': 30, 'kaanaa': 100, 'satava': 50, 'suikkila': 50, @@ -118,7 +118,6 @@ 'räntämäki': 50, 'moisio': 25, 'pääskyvuori': 100, - #'rautatieasema': 50, 'logomo': 100, 'rymättylä': 50, "sauvo": 500, @@ -132,8 +131,35 @@ 'merimasku': 50, 'turkuhalli': 100, 'messukeskus': 100, - 'naantalin pikatie': 150, - 'helsingin valtatie': 500, + 'naantalin pikatie': 25, + 'helsingin valtatie': 25, + 'lauste': 25, + 'pompo': 25, + 'loukinainen': 25, + 'tuorla': 25, + 'satakunnantie': 25, + 'hepokulta': 25, + 'nättinummi': 25, + 'nummenmäki': 25, + 'kurala': 25, + 'itäharju': 25, + 'vasaramäki': 25, + 'pohjola': 25, + 'iso-heikkilä': 25, + 'patterinhaka': 25, + 'illoinen': 50, + 'luolavuori': 25, + 'mäntymäki': 25, + 'kurjenmäki': 25, + 'kuusisto': 25, + 'rautatieasema': 25, + 'majakkaranta': 25, + 'itäranta': 25, + 'martti': 25, + 'vähä-heikkilä': 25, + 'särkilahti': 25, + 'urusvuori': 25, + 'port arthur': 25, } if 'kauppatori' not in reitti: @@ -164,19 +190,22 @@ for i, stop in enumerate(reitti) \ if reitti_arvot[stop] >= 1 ], key = lambda stop: -stop[1]) - # enintään neljä tulee kylttiin - weights = weights[:3] - # jos kolmas kylttiarvo ei ole tarpeeksi merkittävä suhteessa reitin pituuteen niin otetaan se pois - try: - if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): - del weights[2] - except IndexError: - pass - try: - if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): - del weights[1] - except IndexError: - pass + if long: + weights = weights[:4] + else: + # enintään neljä tulee kylttiin + weights = weights[:3] + # jos kolmas kylttiarvo ei ole tarpeeksi merkittävä suhteessa reitin pituuteen niin otetaan se pois + try: + if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): + del weights[2] + except IndexError: + pass + try: + if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): + del weights[1] + except IndexError: + pass # lajitellaan painoarvot uudestaan reittijärjestykseen jotta sign tulee oikeinpäin weights = sorted(weights, key = lambda weight_data: weight_data[2]) # muodostetaan sign.. diff -r 336c750b7b5d -r 382dd85b83a2 service.py --- a/service.py Sat Sep 23 01:23:55 2017 +0300 +++ b/service.py Mon Sep 25 10:46:23 2017 +0300 @@ -61,23 +61,37 @@ else: return request.accept_languages.best_match(tr.languages) -def sign(schedule_entry): +def sign(schedule_entry, long = False): from math import ceil trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance - sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length) + sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = long) if sign: sign_representation = ' - '.join(tr(place, 'paikat') for place in sign if place not in suffix_regions) sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) - #if len(sign_representation) > 25: - # k = ceil(len(sign) / 2) - # sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:]) return sign_representation else: return schedule_entry['trip'].schedule[-1].stop.name +def long_form_sign(schedule_entry): + from math import ceil + trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance + sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = True) + if sign: + return { + 'destination': tr(sign[-1], 'paikat'), + 'via': [tr(place, 'paikat') for place in sign[:-1]], + } + else: + return { + 'destination': schedule_entry['trip'].schedule[-1].stop.name, + 'via': [], + } + + def imminent(schedule_entry): return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) + @app.route('/stop/') def bus_stop_schedule(reference): from buses import bus_stops @@ -106,6 +120,104 @@ tr = tr, ) +@app.route('/stop_display/') +def stop_display(reference): + from buses import bus_stops + schedule = [] + try: + bus_stop = bus_stops[reference] + except KeyError: + abort(404) + for i, schedule_entry in enumerate(bus_stop.schedule(max_amount = 6, arrivals = False)): + schedule.append({ + 'time_data': schedule_entry['time'], + 'time': time_representation(schedule_entry['time']), + 'route': schedule_entry['trip'].route.reference, + 'sign': long_form_sign(schedule_entry), + 'trip': schedule_entry['stop'].trip.name, + 'night': is_night_time(schedule_entry['time']), + 'imminent': imminent(schedule_entry), + 'index': i, + }) + from pprint import pprint + pprint(schedule) + if schedule: + num_imminent_leaves = max(1, len([schedule_entry for schedule_entry in schedule if schedule_entry['time_data'] - schedule[0]['time_data'] < timedelta(minutes = 3)])) + else: + num_imminent_leaves = 1 + return render_template( + 'stop_display.html', + schedule = schedule, + ref = bus_stop.code, + name = tr(bus_stop.name, 'bus-stops'), + link_to_map = bus_stop.location.link_to_map, + region = bus_stop.region, + location = bus_stop.location, + cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, + num_imminent_leaves = num_imminent_leaves, + tr = tr, + ) + +@app.route('/test') +def test(): + from buses import bus_stops + bus_stop = bus_stops['16'] + schedule = [{'imminent': True, + 'index': 0, + 'night': False, + 'route': '2A', + 'sign': {'destination': 'Kohmo', 'via': ['Nummenmäki', 'Kurala']}, + 'time': '1m', + 'trip': '00012501__3798generatedBlock'}, + {'imminent': True, + 'index': 1, + 'night': False, + 'route': '54', + 'sign': {'destination': 'Ylioppilaskylä', 'via': []}, + 'time': '2m', + 'trip': '00014359__5656generatedBlock'}, + {'imminent': True, + 'index': 2, + 'night': False, + 'route': '1', + 'sign': {'destination': 'Lentoasema ✈', 'via': ['Urusvuori']}, + 'time': '3m', + 'trip': '00010281__1281generatedBlock'}, + {'imminent': False, + 'index': 3, + 'night': False, + 'route': '56', + 'sign': {'destination': 'Räntämäki', 'via': ['Nummenmäki', 'Halinen']}, + 'time': '8m', + 'trip': '00014686__5983generatedBlock'}, + {'imminent': False, + 'index': 4, + 'night': False, + 'route': '42', + 'sign': {'destination': 'Varissuo', 'via': ['Kupittaa as', 'Itäharju']}, + 'time': '18:30', + 'trip': '00014010__5307generatedBlock'}, + {'imminent': False, + 'index': 5, + 'night': False, + 'route': '2B', + 'sign': {'destination': 'Littoinen', + 'via': ['Nummenmäki', 'Kurala', 'Kohmo']}, + 'time': '18:35', + 'trip': '00012629__3926generatedBlock'}] + return render_template( + 'stop_display.html', + schedule = schedule, + ref = bus_stop.code, + name = tr(bus_stop.name, 'bus-stops'), + link_to_map = bus_stop.location.link_to_map, + region = bus_stop.region, + location = bus_stop.location, + cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, + num_imminent_leaves = max(1, sum(schedule_entry['imminent'] for schedule_entry in schedule)), + tr = tr, + ) + def time_representation(time, relative = True): time_difference = time - now() if relative and timedelta(minutes = -1) < time_difference < timedelta(minutes = 1): diff -r 336c750b7b5d -r 382dd85b83a2 static/style.css --- a/static/style.css Sat Sep 23 01:23:55 2017 +0300 +++ b/static/style.css Mon Sep 25 10:46:23 2017 +0300 @@ -159,7 +159,6 @@ { color: #C0660B; vertical-align: middle; - animation: blinker steps(1) 500ms infinite alternate; opacity: 0; animation: blinking 2s linear infinite; } diff -r 336c750b7b5d -r 382dd85b83a2 templates/stop_display.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/stop_display.html Mon Sep 25 10:46:23 2017 +0300 @@ -0,0 +1,265 @@ + + + + + + {{name}} + + + + + + +

{{ref}} {{name}}

+
+ + {% for schedule_entry in schedule[:num_imminent_leaves] %} + + + + + + + {% endfor %} +
+ {% if schedule_entry['imminent'] %} + {{schedule_entry['time']}}​ + {% else %} + {{schedule_entry['time']}}​ + {% endif %} + {{schedule_entry['route']}}{{schedule_entry['sign']['destination']}} +
+ {% if num_imminent_leaves > 1 and schedule_entry['sign']['via'][0] and schedule_entry['sign']['via'][1] %} + {{schedule_entry['sign']['via'][0]}} - {{schedule_entry['sign']['via'][1]}} + {% for entry in schedule_entry['sign']['via'][2:] %}
{{entry}}{% endfor %}
+ {% else %} + {% for entry in schedule_entry['sign']['via'] %}{{entry}}
{% endfor %} + {% endif %} + +
+
+ {% if schedule[num_imminent_leaves] %} +
+ + {% for halt in schedule[num_imminent_leaves:] %} + + + + + + {% endfor %} +
+
+ {% endif %} + +