forked from BeepBoopHQ/starter-python-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opening.py
62 lines (51 loc) · 1.58 KB
/
opening.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import re
from osm_time.opening_hours import OpeningHours
DAYS = {"Montag": "mo",
"Dienstag": "tu",
"Mittwoch": "we",
"Donnerstag": "th",
"Freitag": "fr",
"Samstag": "sa",
"Sonntag": "su"}
def main():
offen = match_text("Welcher Markt hat am Montag um 14:00 auf?")
print offen
def match_text(text):
data = get_data()
markets = make_openings(data)
pattern = re.compile(r".* (?P<weekday>\w+) um (?P<time>\d+:\d+).*")
match = pattern.match(text)
if match is None:
print "Did not match"
return None
match_dict = match.groupdict()
day = match_dict.get("weekday")
time = match_dict.get("time")
weekday = DAYS.get(day)
if not weekday or not time:
return
return open_at(markets, weekday, time)
def open_at(markets, day, time):
open_markets = []
for market in markets:
if market['opening_hours'].is_open(day, time):
open_markets.append(market['name'])
return open_markets
def make_openings(data):
markets = []
for market in data:
opening_string = market["properties"].get("opening_hours")
if opening_string is None:
continue
opening_hours = OpeningHours(opening_string)
markets.append({'opening_hours': opening_hours,
'name': market["properties"].get("title")
})
return markets
def get_data():
with open("../data/berlin.json") as infile:
data = json.load(infile)
return data["features"]
if __name__ == "__main__":
main()