-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdazeus-noms.py
executable file
·125 lines (102 loc) · 4.21 KB
/
dazeus-noms.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
import argparse
from datetime import datetime, timedelta
from dazeus import DaZeus
from suppliers import DeFest, FNWI, HetGerecht, RadboudUMC, Refter
from suppliers.utils import DateFormatter
class DaZeusNoms(object):
suppliers = {
'fest': DeFest,
'fnwi': FNWI,
'gerecht': HetGerecht,
'refter': Refter,
'umc': RadboudUMC,
}
def __init__(self, address, verbose = False):
self.dazeus = DaZeus(address, verbose)
self.dazeus.subscribe_command("noms", self.handleMessage)
def listen(self):
self.dazeus.listen()
def handleMessage(self, event, reply):
# Ready available suppliers
suppliers = DaZeusNoms.suppliers.keys()
params = event['params']
# Ensure a restaurant is set.
if len(params) < 6:
reply('Where would you like to eat? I know the menu for: ' + ', '.join(suppliers))
return
# Ensure it is one we know.
supplier = params[5].lower()
if supplier not in suppliers:
reply('I don\'t have a menu for that place... How about one of these: ' + ', '.join(suppliers))
return
# Did we say when we'd like to eat?
now = datetime.now();
if len(params) >= 7:
when = params[6]
# Today?
if when in ['today', 'vandaag', 'nu', 'vanmiddag', 'vanavond']:
dt = now
# Tomorrow?
elif when in ['tomorrow', 'morgen']:
dt = now + timedelta(days=1)
dt = dt.replace(hour=12)
# Day of the week?
else:
when = when[0:2]
print('Looking for ' + when)
if when in DateFormatter.dutchDaysShort:
when = DateFormatter.dutchDaysShort.index(when)
elif when in DateFormatter.englishDaysShort:
when = DateFormatter.englishDaysShort.index(when)
else:
when = None
dt = now
if when >= 0:
offset = when - dt.weekday()
dt += timedelta(days=offset)
dt = dt.replace(hour=12)
# Fall back to today
else:
dt = now
# So, what day is this, again?
concerningDay = 'today' if dt == now else DateFormatter.englishDays[dt.weekday()]
# Dial our supplier...
supplier = DaZeusNoms.suppliers[supplier]
if not (supplier.isOpen(dt) or supplier.willOpen(dt)):
excuse = 'Sorry, ' + supplier.friendly_name
if dt == now:
excuse += ' has closed for the day...'
else:
excuse += ' is not open on ' + concerningDay + '...'
reply(excuse)
return
# Fetch the menu!
menu = supplier.getMenu(dt)
if not menu:
excuse = 'Sorry, I was unable to get a menu for ' + supplier.friendly_name + ' for ' + concerningDay
reply(excuse)
return
# Announce it properly...
reply('Here\'s the menu for ' + supplier.friendly_name + ' for ' + concerningDay + ':')
# Now, pass it on to our faithful user.
for line in menu:
reply(line)
# Finally... can we get this now?
if dt == now:
# Are we open for lunch?
if supplier.isOpenForLunch():
reply(supplier.friendly_name + ' is open for lunch right now.')
# What about dinner?
elif supplier.isOpenForDinner():
reply(supplier.friendly_name + ' is open for dinner right now.')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DaZeus noms plugin.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-a', '--address', required=True, help='Address of the DaZeus instance to connect to. ' +
'Use either `unix:/path/to/file` or `tcp:host:port`.')
parser.add_argument('-v', '--verbose', default=False, help='Increase output verbosity', action='store_true')
# Fetch command line arguments.
args = parser.parse_args()
# Run the bot.
bot = DaZeusNoms(args.address, args.verbose)
bot.listen()