Mon, 13 Aug 2018 22:13:39 +0300
updates
0 | 1 | #!/usr/bin/env python3 |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
2 | from flask import Flask, render_template, abort, send_from_directory, redirect |
0 | 3 | from datetime import datetime, date, time, timedelta |
87 | 4 | from os import path, listdir, environ |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
5 | from configparser import ConfigParser |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
6 | import locale |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
7 | |
87 | 8 | app = Flask(__name__) |
9 | ||
4 | 10 | from misc import * |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
11 | from busroute import reduce_schedule |
90 | 12 | from busroute import simplify_name |
30 | 13 | import buses |
0 | 14 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
15 | regions = {} |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
16 | suffix_regions = {'naantalin pikatie', 'helsingin valtatie', 'kansanpuisto'} |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
17 | |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
18 | # Varmista ettei järjestelmän kieliasetukset sotke muotoiluja |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
19 | def reset_locale(): |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
20 | locale.setlocale(locale.LC_ALL, locale.getdefaultlocale()) |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
21 | |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
22 | def activate_locale(language = None): |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
23 | language = language or language_for_page() |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
24 | class result: |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
25 | def __enter__(self): |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
26 | locale.setlocale(locale.LC_ALL, tr('locale', 'other', language = language)) |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
27 | def __exit__(self, *args): |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
28 | reset_locale() |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
29 | return result() |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
30 | |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
31 | reset_locale() |
0 | 32 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
33 | # Load translations |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
34 | class Translator: |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
35 | def __init__(self): |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
36 | self.languages = {} |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
37 | def load_language(self, file_path): |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
38 | language_name = path.splitext(path.basename(file_path))[0] |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
39 | ini = ConfigParser() |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
40 | ini.read(path.join(file_path)) |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
41 | self.languages[language_name] = ini |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
42 | def __call__(self, name, *sections, language = None): |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
43 | language = language or language_for_page() |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
44 | for section in sections: |
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
45 | try: |
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
46 | return self.languages[language][section][name] |
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
47 | except KeyError: |
72 | 48 | try: |
49 | return profile['tr:' + language + ':' + section][name] | |
50 | except KeyError: | |
51 | pass | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
52 | else: |
72 | 53 | return name[:1].upper() + name[1:] |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
54 | def load_region(self, region): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
55 | for key, value in region.items(): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
56 | if ':' in key: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
57 | name_type, language = key.split(':', 1) |
90 | 58 | if (name_type.endswith('name') or name_type == 'genitive') and language: |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
59 | section = 'region_' + name_type |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
60 | if section not in self.languages[language]: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
61 | self.languages[language][section] = {} |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
62 | self.languages[language][section][region['name']] = value |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
63 | def load_regions(self, regions): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
64 | for region in regions.values(): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
65 | self.load_region(region) |
5 | 66 | |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
67 | tr = Translator() |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
68 | for file in listdir('tr'): |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
69 | tr.load_language(path.join('tr', file)) |
5 | 70 | |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
71 | def language_for_page(): |
5 | 72 | from flask import request |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
73 | if request.args.get('untranslated') is not None: |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
74 | return None |
5 | 75 | else: |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
76 | for language_name in tr.languages: |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
77 | if request.args.get(language_name) is not None: |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
78 | return language_name |
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
79 | else: |
61
0c9b09f694f3
Kielituki käyttöön. Kauhean monimutkaista koodia tähänkin vaadittiin viime kädessä
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
80 | return request.accept_languages.best_match(tr.languages) |
5 | 81 | |
87 | 82 | def sign_elements(schedule_entry, format = 'medium'): |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
83 | from math import ceil |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
84 | from busroute import simplify_name |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
85 | trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance |
87 | 86 | regions = schedule_entry['trip'].concise_schedule(schedule_entry['stop']) |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
87 | return [ |
90 | 88 | name |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
89 | for name in reduce_schedule( |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
90 | regions, |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
91 | trip_length = trip_length, |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
92 | format = format |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
93 | ) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
94 | ] |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
95 | |
90 | 96 | def genitive(name): |
97 | from busroute import regions | |
98 | region = regions.get(name) | |
99 | if region: | |
100 | return region.get('genitive:fi', simplify_name(name) + 'n') | |
101 | else: | |
102 | return simplify_name(name) + 'n' | |
103 | ||
104 | def via_fi(via): | |
105 | if len(via) > 1: | |
106 | return ', '.join(via[:-1]) + ' ja ' + via[-1] | |
107 | else: | |
108 | return via[0] | |
109 | ||
87 | 110 | def sign(schedule_entry, format = 'medium'): |
111 | sign = sign_elements(schedule_entry, format = format) | |
29 | 112 | if sign: |
90 | 113 | #if language_for_page() == 'fi': |
114 | # if len(sign) > 1: | |
115 | # return simplify_name(sign[-1]) + ' ' + via_fi([genitive(place) for place in sign[:-1]]) + ' kautta' | |
116 | # else: | |
117 | # return simplify_name(sign[0]) | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
118 | # sign_representation = ' - '.join(tr(place, 'region_short_name') for place in sign if place not in suffix_regions) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
119 | # sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
120 | # return sign_representation |
90 | 121 | return ' - '.join(tr(simplify_name(place), 'region_short_name') for place in sign) |
29 | 122 | else: |
123 | return schedule_entry['trip'].schedule[-1].stop.name | |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
124 | |
87 | 125 | def long_form_sign(schedule_entry, format = 'long'): |
68 | 126 | from math import ceil |
127 | trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance | |
87 | 128 | sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, format = format) |
68 | 129 | if sign: |
130 | return { | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
131 | 'destination': tr(sign[-1], 'region_short_name'), |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
132 | 'via': [tr(place, 'region_short_name') for place in sign[:-1]], |
68 | 133 | } |
134 | else: | |
135 | return { | |
136 | 'destination': schedule_entry['trip'].schedule[-1].stop.name, | |
137 | 'via': [], | |
138 | } | |
139 | ||
140 | ||
38
d0ae1996abdd
Lisätty vilkkuvat merkit hetken päästä lähteville
Teemu Piippo <teemu@hecknology.net>
parents:
31
diff
changeset
|
141 | def imminent(schedule_entry): |
d0ae1996abdd
Lisätty vilkkuvat merkit hetken päästä lähteville
Teemu Piippo <teemu@hecknology.net>
parents:
31
diff
changeset
|
142 | return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) |
d0ae1996abdd
Lisätty vilkkuvat merkit hetken päästä lähteville
Teemu Piippo <teemu@hecknology.net>
parents:
31
diff
changeset
|
143 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
144 | def first_halt_in_trip_in_place(trip, place): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
145 | for halt in trip.schedule: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
146 | if halt.stop.region == place: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
147 | return halt |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
148 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
149 | return None |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
150 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
151 | place_abbreviations = ConfigParser() |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
152 | place_abbreviations.read('abbreviations.ini') |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
153 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
154 | def place_abbreviation(place): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
155 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
156 | return place_abbreviations['abbreviations'][place] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
157 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
158 | return place |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
159 | |
94
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
160 | def trip_description(trip): |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
161 | entries = [trip.from_place] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
162 | old_places = None |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
163 | starting_halt = None |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
164 | while len(entries) < 3: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
165 | remaining_length = trip.length |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
166 | if starting_halt: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
167 | remaining_length -= starting_halt.traveled_distance |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
168 | places = reduce_schedule(trip.concise_schedule(starting_stop = starting_halt), trip_length = remaining_length, format = 'short') |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
169 | new_places = set(places) - set(entries) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
170 | if not new_places or places == old_places: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
171 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
172 | for place in places: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
173 | if place in new_places: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
174 | starting_halt = first_halt_in_trip_in_place(trip, place) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
175 | entries += [place] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
176 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
177 | old_places = places |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
178 | if trip.to_place not in entries: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
179 | entries += [trip.to_place] |
94
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
180 | return entries |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
181 | |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
182 | def trip_abbreviation(trip): |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
183 | return trip.route.reference + ':' + '-'.join(map(place_abbreviation, trip_description(trip))) |
68 | 184 | |
77 | 185 | def split_route_ref(route_ref): |
186 | try: | |
187 | return list(parse_route_ref(route_ref)) | |
188 | except ValueError: | |
189 | return ['', route_ref, ''] | |
190 | ||
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
191 | @app.route('/stop/<reference>') |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
192 | def bus_stop_schedule(reference): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
193 | from buses import bus_stops |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
194 | schedule = [] |
3 | 195 | try: |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
196 | bus_stop = bus_stops[reference] |
3 | 197 | except KeyError: |
198 | abort(404) | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
199 | for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True): |
77 | 200 | route_ref = schedule_entry['trip'].route.reference |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
201 | schedule.append({ |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
202 | 'time': time_representation(schedule_entry['time']), |
77 | 203 | 'route': route_ref, |
204 | 'route-splice': split_route_ref(route_ref), | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
205 | 'sign': sign(schedule_entry), |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
206 | 'trip': schedule_entry['stop'].trip.name, |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
207 | 'night': is_night_time(schedule_entry['time']), |
38
d0ae1996abdd
Lisätty vilkkuvat merkit hetken päästä lähteville
Teemu Piippo <teemu@hecknology.net>
parents:
31
diff
changeset
|
208 | 'imminent': imminent(schedule_entry), |
0 | 209 | }) |
5 | 210 | return render_template( |
25
cb423946cf33
Uudelleennimetty templaatit
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
211 | 'stop.html', |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
212 | schedule = schedule, |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
213 | name = bus_stop.code + ' ' + tr(bus_stop.name, 'bus-stops'), |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
214 | link_to_map = bus_stop.location.link_to_map, |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
215 | region = hasattr(bus_stop, 'region_name') and bus_stop.region or None, |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
216 | location = bus_stop.location, |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
217 | cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
218 | tr = tr, |
90 | 219 | typename = bus_stop.typename, |
93 | 220 | service = min(bus_stop.services), |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
221 | ) |
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
222 | |
105 | 223 | def week_schedule(bus_stop, start_date = today(), **kwargs): |
74 | 224 | for i in range(-1, 7): |
90 | 225 | try: |
105 | 226 | yield from bus_stop.schedule_for_day(start_date + timedelta(i), **kwargs) |
90 | 227 | except ValueError: |
228 | # went outside bounds | |
229 | return | |
74 | 230 | |
231 | def route_key(route): | |
232 | match = re.search(r'^([a-zA-Z]*)(\d+)(.*)$', route) | |
233 | if match: | |
234 | groups = match.groups() | |
235 | return (groups[0], int(groups[1]), groups[2]) | |
236 | else: | |
237 | return (route,) | |
238 | ||
77 | 239 | def parse_route_ref(route_ref): |
240 | from re import search | |
241 | match = search(r'^([^0-9]*)([0-9]+)(.*)$', route_ref) | |
242 | try: | |
243 | return match.group(1), int(match.group(2)), match.group(3) | |
244 | except AttributeError: | |
245 | raise ValueError(route_ref) | |
246 | ||
87 | 247 | def condense_route_list(route_list): |
248 | def prepare_range_pool(range_pool): | |
249 | if len(range_pool) < 3: | |
250 | yield from map(str, range_pool) | |
251 | else: | |
252 | yield str(min(range_pool)) + '-' + str(max(range_pool)) | |
253 | range_pool = [] | |
254 | for route in route_list: | |
255 | try: | |
256 | route_int = int(route) | |
257 | except ValueError: | |
258 | yield from prepare_range_pool(range_pool) | |
259 | range_pool = [] | |
260 | yield route | |
261 | else: | |
262 | if not range_pool or route_int - 1 in range_pool: | |
263 | range_pool.append(route_int) | |
264 | else: | |
265 | yield from prepare_range_pool(range_pool) | |
266 | range_pool = [route_int] | |
267 | if range_pool: | |
268 | yield from prepare_range_pool(range_pool) | |
269 | ||
270 | def is_weekend_night(time): | |
271 | from datetime import timedelta | |
272 | adjusted_time = time - timedelta(hours = 4, minutes = 30) | |
273 | return adjusted_time.weekday() in [4, 5] and is_night_time(time) | |
274 | ||
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
275 | #encircled = '\u24b6\u24b7\u24b8\u24b9\u24ba\u24bb\u24bc\u24bd\u24be\u24bf' \ |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
276 | # '\u24c0\u24c1\u24c2\u24c3\u24c4\u24c5\u24c6\u24c7\u24c8\u24c9\u24ca\u24cb' \ |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
277 | # '\u24cc\u24cd\u24ce\u24cf' |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
278 | #encircled = '⒜⒝⒞⒟⒠⒡⒢⒣⒤⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵' |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
279 | encircled = 'ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ' |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
280 | |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
281 | def encircle(char): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
282 | from string import ascii_uppercase |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
283 | try: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
284 | return encircled[ascii_uppercase.index(char.upper())] |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
285 | except IndexError: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
286 | return char |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
287 | |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
288 | def variant_abbreviations(variants): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
289 | '''Makes a mapping of route destination variants to letter abbreviations''' |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
290 | suggestion = [variant[-1][0].upper() for variant in variants] |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
291 | if len(set(suggestion)) != len(suggestion): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
292 | from string import ascii_uppercase |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
293 | suggestion = ascii_uppercase |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
294 | return dict(zip(variants, suggestion)) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
295 | |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
296 | def schedule_entry_hash(schedule_entry): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
297 | return schedule_entry['trip'].name, schedule_entry['time'] |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
298 | |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
299 | def describe(bus_stop, week_schedule): |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
300 | schedule = [] |
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
301 | from collections import defaultdict, Counter |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
302 | from busroute import simplify_name |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
303 | destinations_per_route = defaultdict(Counter) |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
304 | counts_per_variant = {} |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
305 | def route_key(route_ref): |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
306 | from math import log |
77 | 307 | try: |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
308 | return () + parse_route_ref(route_ref) |
77 | 309 | except ValueError: |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
310 | return () |
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
311 | def filter_names(names): |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
312 | if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)): |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
313 | return type(names)() |
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
314 | else: |
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
315 | return names |
74 | 316 | data = [] |
317 | names = [] | |
318 | from collections import defaultdict | |
319 | night_routes = defaultdict(lambda: True) | |
320 | num_leaves = 0 | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
321 | trip_mapping = {} |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
322 | for schedule_entry in week_schedule: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
323 | from busroute import greatly_simplify_name |
87 | 324 | sign_tuple = tuple(sign_elements(schedule_entry, format = 'short')) |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
325 | sign_tuple = tuple(greatly_simplify_name(k) for k in sign_tuple) |
87 | 326 | route = schedule_entry['trip'].route.reference |
327 | night_routes[route] &= is_weekend_night(schedule_entry['time']) | |
328 | destinations_per_route[route][sign_tuple] += 1 | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
329 | trip_mapping[schedule_entry_hash(schedule_entry)] = (route, sign_tuple) |
74 | 330 | num_leaves += 1 |
331 | night_routes = {key for key, value in night_routes.items() if value} | |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
332 | routes_per_destination = defaultdict(set) |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
333 | all_variants = [] |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
334 | variant_names = {} |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
335 | # Collect all variants |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
336 | for route, tally in destinations_per_route.items(): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
337 | variants = variant_abbreviations(tally.keys()) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
338 | winner, count = tally.most_common()[0] |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
339 | for destination, count in tally.items(): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
340 | route_name = route |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
341 | if destination != winner: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
342 | route_name += encircle(variants[destination]) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
343 | if route in night_routes: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
344 | night_routes.add(route_name) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
345 | variant_names[route, destination] = route_name |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
346 | all_variants.append({ |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
347 | 'name': route_name, |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
348 | 'destination': filter_names(destination), |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
349 | 'count': count |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
350 | }) |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
351 | counts_per_variant[route_name] = count |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
352 | all_variants.sort(key = lambda k: k['count']) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
353 | route_variant_count = len(all_variants) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
354 | # Only consider variants so that they cover at least 99% of bus leaves |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
355 | coverage = 0 |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
356 | #while coverage / num_leaves < 0.99: |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
357 | # variant = all_variants.pop() |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
358 | for variant in all_variants: |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
359 | routes_per_destination[variant['destination']].add(variant['name']) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
360 | coverage += variant['count'] |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
361 | for key in routes_per_destination: |
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
362 | routes_per_destination[key] = sorted(routes_per_destination[key], key = route_key) |
87 | 363 | def route_len(route): |
364 | length = 0 | |
365 | for char in route: | |
366 | if char.isdigit(): | |
367 | length += 1 | |
368 | else: | |
369 | break | |
370 | return length or len(route) | |
371 | from math import inf | |
99
0726eb9bc9d1
night routes can be rarer
Teemu Piippo <teemu@hecknology.net>
parents:
98
diff
changeset
|
372 | def route_limit(route): |
101 | 373 | return (route in night_routes) and 6 or 20 |
87 | 374 | def route_key(route): |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
375 | from math import log |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
376 | return ( |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
377 | route in night_routes, |
99
0726eb9bc9d1
night routes can be rarer
Teemu Piippo <teemu@hecknology.net>
parents:
98
diff
changeset
|
378 | counts_per_variant.get(route, 0) < route_limit(route), |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
379 | route_len(route), |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
380 | str(route) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
381 | ) |
87 | 382 | def routes_key(routes): |
383 | return min(route_key(route) for route in routes) | |
384 | result = [] | |
101 | 385 | rare_variants = {variant['name'] for variant in all_variants if variant['count'] < route_limit(variant['name'])} |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
386 | rare_variant_groups = set() |
87 | 387 | for regions, routes in sorted( |
388 | routes_per_destination.items(), | |
389 | key = lambda pair: routes_key(pair[1]) | |
390 | ): | |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
391 | routes_tuple = tuple(condense_route_list(sorted(routes, key = route_key))) |
87 | 392 | result.append(( |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
393 | routes_tuple, |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
394 | ' - '.join(tr(region, 'region_short_name') for region in regions) |
87 | 395 | )) |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
396 | if all(variant in rare_variants for variant in routes): |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
397 | rare_variant_groups.add(routes_tuple) |
87 | 398 | return { |
399 | 'night-routes': night_routes, | |
400 | 'all-night-routes': lambda entry, description: all(route in description['night-routes'] for route in entry[0]), | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
401 | 'simple': route_variant_count <= 1, |
87 | 402 | 'description': result, |
403 | 'wtf': destinations_per_route, | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
404 | 'variant-map': {k:variant_names[v] for k, v in trip_mapping.items()}, |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
405 | 'rare-variants': rare_variants, |
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
406 | 'rare-variant-groups': rare_variant_groups, |
87 | 407 | } |
408 | ||
409 | @app.route('/stop_description/<reference>') | |
410 | def bus_stop_description(reference): | |
411 | from buses import bus_stops | |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
412 | from pprint import pformat |
87 | 413 | try: |
414 | bus_stop = bus_stops[reference] | |
415 | except KeyError: | |
416 | abort(404) | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
417 | return '<pre>' + pformat(describe(bus_stop, week_schedule(bus_stop, arrivals = True))) + '</pre>' |
73
c56b0ef96514
stop_description ja muuta roinaa
Teemu Piippo <teemu@hecknology.net>
parents:
72
diff
changeset
|
418 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
419 | @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>') |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
420 | def describe_destination(trip_reference, stop_reference, index): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
421 | from buses import bus_stops, all_trips |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
422 | from busroute import simplify_name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
423 | from flask import jsonify |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
424 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
425 | trip = all_trips[trip_reference] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
426 | bus_stop = bus_stops[stop] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
427 | schedule_entry = [schedule_entry for schedule_entry in trip.schedule if schedule_entry.stop.reference == stop_reference][index] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
428 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
429 | abort(404) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
430 | except ValueError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
431 | abort(404) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
432 | return jsonify(long_form_sign({'trip': trip, 'stop': schedule_entry})) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
433 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
434 | @app.route('/api/trip_abbreviation/<block_id>/<int:arrival_time_offset>') |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
435 | def api_trip_abbreviation(block_id, arrival_time_offset): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
436 | from buses import trips_by_vehicle_info |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
437 | from flask import jsonify |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
438 | from datetime import timedelta |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
439 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
440 | trip = trips_by_vehicle_info[block_id, timedelta(seconds = arrival_time_offset)] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
441 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
442 | abort(404) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
443 | return jsonify({ |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
444 | 'abbreviation': trip_abbreviation(trip), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
445 | }) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
446 | |
85 | 447 | def current_bus_day(): |
448 | from datetime import date, datetime, timedelta | |
449 | day = date.today() | |
450 | if datetime.now().hour < 5: | |
451 | day -= timedelta(1) | |
452 | return day | |
453 | ||
68 | 454 | @app.route('/stop_display/<reference>') |
455 | def stop_display(reference): | |
456 | from buses import bus_stops | |
457 | schedule = [] | |
458 | try: | |
459 | bus_stop = bus_stops[reference] | |
460 | except KeyError: | |
461 | abort(404) | |
85 | 462 | for i, schedule_entry in enumerate(bus_stop.schedule_for_day(current_bus_day(), arrivals = False)): |
68 | 463 | schedule.append({ |
464 | 'time_data': schedule_entry['time'], | |
465 | 'time': time_representation(schedule_entry['time']), | |
466 | 'route': schedule_entry['trip'].route.reference, | |
87 | 467 | 'sign': long_form_sign(schedule_entry, format = 'medium'), |
68 | 468 | 'trip': schedule_entry['stop'].trip.name, |
469 | 'night': is_night_time(schedule_entry['time']), | |
470 | 'imminent': imminent(schedule_entry), | |
471 | 'index': i, | |
472 | }) | |
473 | from pprint import pprint | |
474 | pprint(schedule) | |
76 | 475 | def destination_key(schedule_entry): |
476 | sign = schedule_entry['sign'] | |
477 | return (sign['destination'],) + tuple(sign['via']) | |
478 | def filter_schedule(schedule, *, key): | |
479 | used = set() | |
480 | for schedule_entry in schedule: | |
481 | key_value = key(schedule_entry) | |
482 | if key_value not in used: | |
483 | used.add(key_value) | |
484 | yield schedule_entry | |
485 | schedule = list(filter_schedule(schedule, key = destination_key))[:6] | |
68 | 486 | if schedule: |
487 | 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)])) | |
488 | else: | |
489 | num_imminent_leaves = 1 | |
490 | return render_template( | |
491 | 'stop_display.html', | |
492 | schedule = schedule, | |
493 | ref = bus_stop.code, | |
494 | name = tr(bus_stop.name, 'bus-stops'), | |
495 | link_to_map = bus_stop.location.link_to_map, | |
496 | region = bus_stop.region, | |
497 | location = bus_stop.location, | |
498 | cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, | |
499 | num_imminent_leaves = num_imminent_leaves, | |
500 | tr = tr, | |
501 | ) | |
502 | ||
503 | @app.route('/test') | |
504 | def test(): | |
505 | from buses import bus_stops | |
506 | bus_stop = bus_stops['16'] | |
507 | schedule = [{'imminent': True, | |
508 | 'index': 0, | |
509 | 'night': False, | |
510 | 'route': '2A', | |
511 | 'sign': {'destination': 'Kohmo', 'via': ['Nummenmäki', 'Kurala']}, | |
512 | 'time': '1m', | |
513 | 'trip': '00012501__3798generatedBlock'}, | |
514 | {'imminent': True, | |
515 | 'index': 1, | |
516 | 'night': False, | |
517 | 'route': '54', | |
518 | 'sign': {'destination': 'Ylioppilaskylä', 'via': []}, | |
519 | 'time': '2m', | |
520 | 'trip': '00014359__5656generatedBlock'}, | |
521 | {'imminent': True, | |
522 | 'index': 2, | |
523 | 'night': False, | |
524 | 'route': '1', | |
525 | 'sign': {'destination': 'Lentoasema ✈', 'via': ['Urusvuori']}, | |
526 | 'time': '3m', | |
527 | 'trip': '00010281__1281generatedBlock'}, | |
528 | {'imminent': False, | |
529 | 'index': 3, | |
530 | 'night': False, | |
531 | 'route': '56', | |
532 | 'sign': {'destination': 'Räntämäki', 'via': ['Nummenmäki', 'Halinen']}, | |
533 | 'time': '8m', | |
534 | 'trip': '00014686__5983generatedBlock'}, | |
535 | {'imminent': False, | |
536 | 'index': 4, | |
537 | 'night': False, | |
538 | 'route': '42', | |
539 | 'sign': {'destination': 'Varissuo', 'via': ['Kupittaa as', 'Itäharju']}, | |
540 | 'time': '18:30', | |
541 | 'trip': '00014010__5307generatedBlock'}, | |
542 | {'imminent': False, | |
543 | 'index': 5, | |
544 | 'night': False, | |
545 | 'route': '2B', | |
546 | 'sign': {'destination': 'Littoinen', | |
547 | 'via': ['Nummenmäki', 'Kurala', 'Kohmo']}, | |
548 | 'time': '18:35', | |
549 | 'trip': '00012629__3926generatedBlock'}] | |
550 | return render_template( | |
551 | 'stop_display.html', | |
552 | schedule = schedule, | |
553 | ref = bus_stop.code, | |
554 | name = tr(bus_stop.name, 'bus-stops'), | |
555 | link_to_map = bus_stop.location.link_to_map, | |
556 | region = bus_stop.region, | |
557 | location = bus_stop.location, | |
558 | cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, | |
559 | num_imminent_leaves = max(1, sum(schedule_entry['imminent'] for schedule_entry in schedule)), | |
560 | tr = tr, | |
561 | ) | |
562 | ||
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
563 | def time_representation(time, relative = True): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
564 | time_difference = time - now() |
42
0b53b7c70875
Näytä myös aikataulut jotka ovat minuutti sitten menneet
teemu
parents:
38
diff
changeset
|
565 | if relative and timedelta(minutes = -1) < time_difference < timedelta(minutes = 1): |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
566 | return tr('right-now', 'misc-text') |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
567 | elif relative and time_difference > timedelta(0) and time_difference < timedelta(minutes = 10): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
568 | return '%dm' % round(time_difference.seconds / 60) |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
569 | elif time.date() == today(): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
570 | return '%d:%02d' % (time.hour, time.minute) |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
571 | elif time_difference < timedelta(7): |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
572 | with activate_locale(): |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
573 | return time.strftime('%-a %H:%M').replace(' ', '\xa0') |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
574 | else: |
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
575 | with activate_locale(): |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
576 | return time.strftime('%-d.%-m. %H:%M').replace(' ', '\xa0') |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
577 | |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
578 | @app.route('/stop_cluster/<cluster_name>') |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
579 | def cluster_schedule(cluster_name): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
580 | from buses import bus_stops, clusters_by_name |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
581 | schedule = [] |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
582 | try: |
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
583 | cluster = clusters_by_name[cluster_name] |
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
584 | except KeyError: |
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
585 | abort(404) |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
586 | for schedule_entry in cluster.schedule(max_amount = 100): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
587 | schedule.append({ |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
588 | 'time': time_representation(schedule_entry['time']), |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
589 | 'route': schedule_entry['trip'].route.reference, |
77 | 590 | 'route-splice': split_route_ref(schedule_entry['trip'].route.reference), |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
591 | 'sign': sign(schedule_entry), |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
592 | 'trip': schedule_entry['stop'].trip.name, |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
593 | 'night': is_night_time(schedule_entry['time']), |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
594 | 'stop_id': schedule_entry['stop'].stop.reference, |
29 | 595 | 'stop_code': schedule_entry['stop'].stop.code, |
72 | 596 | 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus-stops', 'places'), |
38
d0ae1996abdd
Lisätty vilkkuvat merkit hetken päästä lähteville
Teemu Piippo <teemu@hecknology.net>
parents:
31
diff
changeset
|
597 | 'imminent': imminent(schedule_entry), |
90 | 598 | 'typename': schedule_entry['stop'].stop.typename, |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
599 | }) |
27 | 600 | stops_in_cluster = sorted( |
601 | ({ | |
602 | 'id': stop.reference, | |
29 | 603 | 'code': stop.code, |
63 | 604 | 'name': tr(stop.name, 'bus-stops'), |
27 | 605 | } for stop in cluster.stops), |
606 | key = lambda stop: (len(stop['id']), stop['id']) | |
607 | ) | |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
608 | return render_template( |
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
609 | 'cluster.html', |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
610 | schedule = schedule, |
72 | 611 | name = tr(cluster.name, 'places', 'place-clusters', 'bus-stops'), |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
612 | link_to_map = cluster.center.link_to_map, |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
613 | location = cluster.center, |
27 | 614 | stops_in_cluster = stops_in_cluster, |
615 | amount_of_stops_in_cluster = len(stops_in_cluster), | |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
616 | tr = tr, |
93 | 617 | service = min(set.union(*[bus_stop.services for bus_stop in cluster.stops])), |
5 | 618 | ) |
0 | 619 | |
77 | 620 | def day_class(weekday): |
621 | if weekday < 5: | |
622 | return 'working-day' | |
623 | elif weekday == 5: | |
624 | return 'saturday' | |
625 | else: | |
626 | assert weekday == 6 | |
627 | return 'sunday' | |
628 | ||
105 | 629 | class Week: |
630 | def __init__(self, year, weeknumber): | |
631 | self.year, self.weeknumber = year, weeknumber | |
632 | def normalize(self): | |
633 | while self.weeknumber > 52: | |
634 | self.weeknumber -= 52 | |
635 | self.year += 1 | |
636 | while self.weeknumber < 1: | |
637 | self.weeknumber += 52 | |
638 | self.year -= 1 | |
639 | def __repr__(self): | |
640 | return str.format( | |
641 | 'Week({year}, {weeknumber})', | |
642 | year = self.year, | |
643 | weeknumber = self.weeknumber | |
644 | ) | |
645 | def __add__(self, numweeks): | |
646 | newweek = Week(year = self.year, weeknumber = self.weeknumber + numweeks) | |
647 | newweek.normalize() | |
648 | return newweek | |
649 | def __sub__(self, numweeks): | |
650 | newweek = Week(year = self.year, weeknumber = self.weeknumber - numweeks) | |
651 | newweek.normalize() | |
652 | return newweek | |
653 | def monday(self): | |
654 | from datetime import datetime | |
655 | datestring = str.format( | |
656 | '{year}-W{weeknumber}-1', | |
657 | year = self.year, | |
658 | weeknumber = self.weeknumber, | |
659 | ) | |
660 | return datetime.strptime(datestring, "%Y-W%W-%w").date() | |
661 | @staticmethod | |
662 | def fromstring(string): | |
663 | try: | |
664 | year, weeknumber = string.split('W', 1) | |
665 | return Week(year = int(year), weeknumber = int(weeknumber)) | |
666 | except ValueError: | |
667 | raise ValueError('bad week string: ' + repr(string)) | |
668 | @staticmethod | |
669 | def now(): | |
670 | from datetime import date | |
671 | cal = date.today().isocalendar() | |
672 | return Week(year = cal[0], weeknumber = cal[1]) | |
673 | ||
77 | 674 | @app.route('/stop_week/<stop_reference>') |
675 | def stop_week(stop_reference): | |
676 | from buses import bus_stops | |
677 | from flask import request | |
105 | 678 | from datetime import date |
679 | if 'week' in request.args: | |
680 | try: | |
681 | week = Week.fromstring(request.args['week']) | |
682 | except ValueError: | |
683 | abort(400) | |
684 | else: | |
685 | week = Week.now() | |
77 | 686 | if 'routes' in request.args: |
687 | filtered_routes = set(request.args['routes'].split(';')) | |
688 | route_filter = lambda route: route in filtered_routes | |
689 | else: | |
690 | route_filter = lambda route: True | |
691 | if 'dest' in request.args: | |
692 | dests = {bus_stops.get(dest, None) for dest in request.args['dest'].split(';')} | |
693 | dests.discard(None) | |
694 | dest_filter = lambda trip: any(trip.contains_stop(dest) for dest in dests) | |
695 | else: | |
696 | dest_filter = lambda trip: True | |
697 | schedule = [] | |
698 | try: | |
699 | bus_stop = bus_stops[stop_reference] | |
700 | except KeyError: | |
701 | abort(404) | |
702 | week_model = {} | |
105 | 703 | bus_stop_schedule = list(week_schedule(bus_stop, start_date = week.monday(), arrivals = True)) |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
704 | description = describe(bus_stop, bus_stop_schedule) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
705 | for schedule_entry in bus_stop_schedule: |
77 | 706 | route_ref = schedule_entry['trip'].route.reference |
707 | if route_filter(route_ref) and dest_filter(schedule_entry['trip']): | |
708 | time = schedule_entry['time'] | |
709 | date = schedule_entry['date'] | |
710 | if date not in week_model: | |
711 | week_model[date] = {} | |
712 | day_model = week_model[date] | |
713 | if time.hour not in day_model: | |
714 | day_model[time.hour] = [] | |
715 | hour_model = day_model[time.hour] | |
716 | hour_model.append({ | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
717 | 'route': description['variant-map'][schedule_entry_hash(schedule_entry)], |
77 | 718 | 'route-splice': split_route_ref(route_ref), |
719 | 'trip': schedule_entry['stop'].trip.name, | |
720 | 'night': is_night_time(schedule_entry['time']), | |
721 | 'minute': time.minute, | |
722 | }) | |
79
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
723 | for day_offset in range(7): |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
724 | from datetime import date, datetime, timedelta |
105 | 725 | day = week.monday() + timedelta(day_offset) |
79
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
726 | try: |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
727 | day_model = week_model[day] |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
728 | except KeyError: |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
729 | week_model[day] = {} |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
730 | else: |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
731 | def hour_key(x): |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
732 | return (x - 5) % 24 |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
733 | # Fill in missing hours from 5am to last active hour |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
734 | hours = set(day_model.keys()) | {5} |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
735 | sorted_hours = sorted(hours, key = hour_key) |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
736 | start_hour = sorted_hours[0] |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
737 | end_hour = sorted_hours[-1] + 1 |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
738 | for hour in range(start_hour, end_hour): |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
739 | hour_start = datetime(day.year, day.month, day.day, hour, 0) |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
740 | if hour not in day_model and hour_start >= datetime.now(): |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
741 | day_model[hour] = [] |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
742 | # Sort the hours, so that 5am is first and 4am is last. |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
743 | from collections import OrderedDict |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
744 | week_model[day] = OrderedDict( |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
745 | sorted( |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
746 | day_model.items(), |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
747 | key = lambda pair: hour_key(pair[0]), |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
748 | ) |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
749 | ) |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
750 | week_model = [ |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
751 | { |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
752 | 'day': day, |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
753 | 'schedule': schedule, |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
754 | 'day-class': day_class(day.weekday()) |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
755 | } for day, schedule in week_model.items() |
ba854da8c424
stop_week: järjestä tunnit ja paikkaa tyhjät tunnit
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
756 | ] |
77 | 757 | week_model = sorted(week_model, key = lambda day: day['day']) |
758 | return render_template( | |
759 | 'stop_week.html', | |
760 | ref = bus_stop.code, | |
761 | name = tr(bus_stop.name, 'bus-stops'), | |
762 | tr = tr, | |
763 | week = week_model, | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
90
diff
changeset
|
764 | description = description, |
90 | 765 | typename = bus_stop.typename, |
105 | 766 | curweek = week, |
77 | 767 | ) |
768 | ||
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
769 | @app.route('/trip/<trip_reference>') |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
770 | def trip(trip_reference): |
2 | 771 | from flask import request |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
772 | from buses import all_trips |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
773 | from busroute import simplify_name |
3 | 774 | try: |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
775 | trip = all_trips[trip_reference] |
3 | 776 | except KeyError: |
777 | abort(404) | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
778 | schedule = [] |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
779 | region = '' |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
780 | for halt in trip.schedule: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
781 | stop_time = datetime.combine(today(), time()) + halt.arrival_time |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
782 | formatted_time = time_representation(stop_time) |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
783 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
784 | if halt.stop.region != region and not (region and not halt.stop.region): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
785 | if len(schedule) and not schedule[-1]['name']: |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
786 | schedule[-1]['name'] = tr(halt.stop.region or '', 'region_name') |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
787 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
788 | schedule.append({ |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
789 | 'name': tr(halt.stop.region or '', 'region_name'), |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
790 | 'time': formatted_time, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
791 | 'stops': [], |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
792 | 'index': len(schedule), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
793 | }) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
794 | region = halt.stop.region |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
795 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
796 | schedule.append({ |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
797 | 'name': tr(halt.stop.name or '', 'bus-stops'), |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
798 | 'time': formatted_time, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
799 | 'stops': [], |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
800 | 'index': len(schedule), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
801 | }) |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
802 | schedule[-1]['stops'].append({ |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
803 | 'time': formatted_time, |
29 | 804 | 'id': halt.stop.reference, |
805 | 'code': halt.stop.code, | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
806 | 'name': tr(halt.stop.name, 'bus-stops'), |
90 | 807 | 'typename': halt.stop.typename, |
0 | 808 | }) |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
809 | sign = trip.concise_schedule() |
29 | 810 | try: |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
811 | sign = [sign[0], sign[-1]] |
29 | 812 | except IndexError: |
813 | sign = [trip.schedule[0].stop.name, trip.schedule[-1].stop.name] | |
25
cb423946cf33
Uudelleennimetty templaatit
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
814 | return render_template('trip.html', |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
815 | schedule = schedule, |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
816 | trip_reference = trip_reference, |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
817 | route = trip.route.reference, |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
818 | description = ' - '.join(tr(place, 'region_name') for place in sign), |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
819 | night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time), |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
820 | tr = tr, |
27 | 821 | length = trip.length / 1000, |
93 | 822 | service = trip.route.service, |
27 | 823 | ) |
824 | ||
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
825 | @app.route('/route/<name>') |
27 | 826 | def route_page(name): |
827 | from buses import routes | |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
828 | from collections import defaultdict |
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
829 | from busroute import greatly_simplify_name |
27 | 830 | route = routes[name.upper()] |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
831 | schedules = defaultdict(list) |
27 | 832 | for trip in route.trips: |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
833 | if trip.is_served_at(today()): |
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
834 | schedules[trip.schedule[0].stop].append({ |
27 | 835 | 'name': trip.reference, |
836 | 'from': trip.from_place, | |
837 | 'to': trip.to_place, | |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
838 | 'description': ' - '.join( |
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
839 | greatly_simplify_name(place) |
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
840 | for place in trip_description(trip) |
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
841 | ), |
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
842 | 'gone': datetime.combine(today(), time()) + trip.schedule[-1].arrival_time < now(), |
27 | 843 | 'time': time_representation(datetime.combine(today(), time()) + trip.schedule[0].departure_time), |
844 | }) | |
845 | return render_template('route.html', | |
846 | name = route.reference + ' ' + route.description, | |
847 | tr = tr, | |
98
c07a77c8a070
fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents:
94
diff
changeset
|
848 | schedules = schedules, |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
849 | ) |
0 | 850 | |
94
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
851 | @app.route('/interesting') |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
852 | def interesting(): |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
853 | from buses import all_trips, services_for_day |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
854 | from busroute import simplify_name |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
855 | from collections import Counter, defaultdict |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
856 | from datetime import datetime |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
857 | trip_counts = Counter() |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
858 | interesting_trips = defaultdict(list) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
859 | for trip_reference, trip in all_trips.items(): |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
860 | if len(trip.service.dates) <= 3: |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
861 | for date in trip.service.dates: |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
862 | moment = trip.schedule[0].departure_datetime(date) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
863 | if moment > datetime.now(): |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
864 | interesting_trips[date].append((trip, moment)) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
865 | for date in interesting_trips: |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
866 | interesting_trips[date].sort(key = lambda tuple: tuple[1]) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
867 | interesting_trips = [(date, interesting_trips[date]) for date in sorted(interesting_trips.keys())] |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
868 | data = [ |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
869 | { |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
870 | 'date': date, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
871 | 'leaves': [{ |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
872 | 'route': trip.route.reference, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
873 | 'departure': moment, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
874 | 'trip': trip.reference, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
875 | 'stop': trip.schedule[0].stop, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
876 | 'description': ' - '.join( |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
877 | simplify_name(place, replace = True) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
878 | for place in trip_description(trip) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
879 | ), |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
880 | } for trip, moment in leaves] |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
881 | } |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
882 | for date, leaves in interesting_trips |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
883 | ] |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
884 | return render_template( |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
885 | 'interesting.html', |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
886 | data = data, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
887 | tr = tr, |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
888 | ) |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
889 | |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
890 | @app.route('/') |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
891 | def index(): |
49 | 892 | return redirect('stop_cluster/kauppatori') |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
893 | |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
894 | @app.route('/pysäkki/<reference>') |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
895 | def redirect_pysäkki(reference): |
50 | 896 | return redirect('stop/' + str(reference)) |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
897 | |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
898 | @app.route('/pysäkkiryhmä/<reference>') |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
899 | def redirect_pysäkkiryhmä(reference): |
50 | 900 | return redirect('stop_cluster/' + str(reference)) |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
901 | |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
902 | @app.route('/ajovuoro/<reference>') |
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
903 | def redirect_ajovuoro(reference): |
50 | 904 | return redirect('trip/' + str(reference)) |
48
428cfdf64ccc
Lisätty uudelleenohjauksia
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
905 | |
11
e18ad36fbbf5
Lisätty favicon, korjattu tr()-funktio joka ei olettunut suomen kieleen (vaan kääntämättömään)
Teemu Piippo <teemu@hecknology.net>
parents:
10
diff
changeset
|
906 | @app.route('/static/<path:path>') |
e18ad36fbbf5
Lisätty favicon, korjattu tr()-funktio joka ei olettunut suomen kieleen (vaan kääntämättömään)
Teemu Piippo <teemu@hecknology.net>
parents:
10
diff
changeset
|
907 | def static_file(path): |
e18ad36fbbf5
Lisätty favicon, korjattu tr()-funktio joka ei olettunut suomen kieleen (vaan kääntämättömään)
Teemu Piippo <teemu@hecknology.net>
parents:
10
diff
changeset
|
908 | return send_from_directory(path.join('static', path)) |
e18ad36fbbf5
Lisätty favicon, korjattu tr()-funktio joka ei olettunut suomen kieleen (vaan kääntämättömään)
Teemu Piippo <teemu@hecknology.net>
parents:
10
diff
changeset
|
909 | |
30 | 910 | from argparse import ArgumentParser |
911 | parser = ArgumentParser() | |
912 | parser.add_argument('gtfs_zip_path') | |
913 | parser.add_argument('profile_path') | |
914 | ||
0 | 915 | if __name__ == '__main__': |
2 | 916 | parser.add_argument('-p', '--port', type = int, default = 5000) |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
917 | parser.add_argument('-d', '--debug', action = 'store_true') |
30 | 918 | |
919 | args = parser.parse_args() | |
920 | profile.read(args.profile_path) | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
921 | if profile['regions']['use-regions']: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
922 | from regions import parse_regions |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
923 | regions = parse_regions(profile['regions']['osm-path']) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
924 | tr.load_regions(regions) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
925 | import busroute |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
926 | busroute.regions = regions |
93 | 927 | buses.load_buses(args.gtfs_zip_path) |
30 | 928 | |
929 | if __name__ == '__main__': | |
10
843c6ee340e7
Refaktoroitu käännä-funktio, kaikki kielet luetaan automaattisesti
Teemu Piippo <teemu@hecknology.net>
parents:
9
diff
changeset
|
930 | app.run(debug = args.debug, port = args.port) |