service.py

Thu, 06 Dec 2018 23:39:20 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 06 Dec 2018 23:39:20 +0200
changeset 134
4ac0f2e2ec4e
parent 127
2bc0529d44a5
child 136
27324a81beca
permissions
-rwxr-xr-x

removed uuid mapping

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

mercurial