59 if request.args.get(language_name) is not None: |
59 if request.args.get(language_name) is not None: |
60 return language_name |
60 return language_name |
61 else: |
61 else: |
62 return request.accept_languages.best_match(tr.languages) |
62 return request.accept_languages.best_match(tr.languages) |
63 |
63 |
64 def sign(schedule_entry): |
64 def sign(schedule_entry, long = False): |
65 from math import ceil |
65 from math import ceil |
66 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance |
66 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance |
67 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length) |
67 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = long) |
68 if sign: |
68 if sign: |
69 sign_representation = ' - '.join(tr(place, 'paikat') for place in sign if place not in suffix_regions) |
69 sign_representation = ' - '.join(tr(place, 'paikat') for place in sign if place not in suffix_regions) |
70 sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) |
70 sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) |
71 #if len(sign_representation) > 25: |
|
72 # k = ceil(len(sign) / 2) |
|
73 # sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:]) |
|
74 return sign_representation |
71 return sign_representation |
75 else: |
72 else: |
76 return schedule_entry['trip'].schedule[-1].stop.name |
73 return schedule_entry['trip'].schedule[-1].stop.name |
|
74 |
|
75 def long_form_sign(schedule_entry): |
|
76 from math import ceil |
|
77 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance |
|
78 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = True) |
|
79 if sign: |
|
80 return { |
|
81 'destination': tr(sign[-1], 'paikat'), |
|
82 'via': [tr(place, 'paikat') for place in sign[:-1]], |
|
83 } |
|
84 else: |
|
85 return { |
|
86 'destination': schedule_entry['trip'].schedule[-1].stop.name, |
|
87 'via': [], |
|
88 } |
|
89 |
77 |
90 |
78 def imminent(schedule_entry): |
91 def imminent(schedule_entry): |
79 return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) |
92 return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) |
|
93 |
80 |
94 |
81 @app.route('/stop/<reference>') |
95 @app.route('/stop/<reference>') |
82 def bus_stop_schedule(reference): |
96 def bus_stop_schedule(reference): |
83 from buses import bus_stops |
97 from buses import bus_stops |
84 schedule = [] |
98 schedule = [] |
101 name = bus_stop.code + ' ' + tr(bus_stop.name, 'bus-stops'), |
115 name = bus_stop.code + ' ' + tr(bus_stop.name, 'bus-stops'), |
102 link_to_map = bus_stop.location.link_to_map, |
116 link_to_map = bus_stop.location.link_to_map, |
103 region = bus_stop.region, |
117 region = bus_stop.region, |
104 location = bus_stop.location, |
118 location = bus_stop.location, |
105 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, |
119 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, |
|
120 tr = tr, |
|
121 ) |
|
122 |
|
123 @app.route('/stop_display/<reference>') |
|
124 def stop_display(reference): |
|
125 from buses import bus_stops |
|
126 schedule = [] |
|
127 try: |
|
128 bus_stop = bus_stops[reference] |
|
129 except KeyError: |
|
130 abort(404) |
|
131 for i, schedule_entry in enumerate(bus_stop.schedule(max_amount = 6, arrivals = False)): |
|
132 schedule.append({ |
|
133 'time_data': schedule_entry['time'], |
|
134 'time': time_representation(schedule_entry['time']), |
|
135 'route': schedule_entry['trip'].route.reference, |
|
136 'sign': long_form_sign(schedule_entry), |
|
137 'trip': schedule_entry['stop'].trip.name, |
|
138 'night': is_night_time(schedule_entry['time']), |
|
139 'imminent': imminent(schedule_entry), |
|
140 'index': i, |
|
141 }) |
|
142 from pprint import pprint |
|
143 pprint(schedule) |
|
144 if schedule: |
|
145 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)])) |
|
146 else: |
|
147 num_imminent_leaves = 1 |
|
148 return render_template( |
|
149 'stop_display.html', |
|
150 schedule = schedule, |
|
151 ref = bus_stop.code, |
|
152 name = tr(bus_stop.name, 'bus-stops'), |
|
153 link_to_map = bus_stop.location.link_to_map, |
|
154 region = bus_stop.region, |
|
155 location = bus_stop.location, |
|
156 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, |
|
157 num_imminent_leaves = num_imminent_leaves, |
|
158 tr = tr, |
|
159 ) |
|
160 |
|
161 @app.route('/test') |
|
162 def test(): |
|
163 from buses import bus_stops |
|
164 bus_stop = bus_stops['16'] |
|
165 schedule = [{'imminent': True, |
|
166 'index': 0, |
|
167 'night': False, |
|
168 'route': '2A', |
|
169 'sign': {'destination': 'Kohmo', 'via': ['Nummenmäki', 'Kurala']}, |
|
170 'time': '1m', |
|
171 'trip': '00012501__3798generatedBlock'}, |
|
172 {'imminent': True, |
|
173 'index': 1, |
|
174 'night': False, |
|
175 'route': '54', |
|
176 'sign': {'destination': 'Ylioppilaskylä', 'via': []}, |
|
177 'time': '2m', |
|
178 'trip': '00014359__5656generatedBlock'}, |
|
179 {'imminent': True, |
|
180 'index': 2, |
|
181 'night': False, |
|
182 'route': '1', |
|
183 'sign': {'destination': 'Lentoasema ✈', 'via': ['Urusvuori']}, |
|
184 'time': '3m', |
|
185 'trip': '00010281__1281generatedBlock'}, |
|
186 {'imminent': False, |
|
187 'index': 3, |
|
188 'night': False, |
|
189 'route': '56', |
|
190 'sign': {'destination': 'Räntämäki', 'via': ['Nummenmäki', 'Halinen']}, |
|
191 'time': '8m', |
|
192 'trip': '00014686__5983generatedBlock'}, |
|
193 {'imminent': False, |
|
194 'index': 4, |
|
195 'night': False, |
|
196 'route': '42', |
|
197 'sign': {'destination': 'Varissuo', 'via': ['Kupittaa as', 'Itäharju']}, |
|
198 'time': '18:30', |
|
199 'trip': '00014010__5307generatedBlock'}, |
|
200 {'imminent': False, |
|
201 'index': 5, |
|
202 'night': False, |
|
203 'route': '2B', |
|
204 'sign': {'destination': 'Littoinen', |
|
205 'via': ['Nummenmäki', 'Kurala', 'Kohmo']}, |
|
206 'time': '18:35', |
|
207 'trip': '00012629__3926generatedBlock'}] |
|
208 return render_template( |
|
209 'stop_display.html', |
|
210 schedule = schedule, |
|
211 ref = bus_stop.code, |
|
212 name = tr(bus_stop.name, 'bus-stops'), |
|
213 link_to_map = bus_stop.location.link_to_map, |
|
214 region = bus_stop.region, |
|
215 location = bus_stop.location, |
|
216 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, |
|
217 num_imminent_leaves = max(1, sum(schedule_entry['imminent'] for schedule_entry in schedule)), |
106 tr = tr, |
218 tr = tr, |
107 ) |
219 ) |
108 |
220 |
109 def time_representation(time, relative = True): |
221 def time_representation(time, relative = True): |
110 time_difference = time - now() |
222 time_difference = time - now() |