]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
Fix name
[matrix-shop-bot.git] / matrix_shop_bot / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from .db import Data
8
9 MODULE='shop'
10
11 DATE_FORMAT = '%Y-%m-%d'
12
13 def today():
14 return dt.now().strftime(DATE_FORMAT)
15
16 PREFIX = f'{MODULE}'
17 logger = logging.getLogger(__name__)
18 logging.basicConfig(filename=f'{MODULE}.log', encoding='utf-8', level=logging.DEBUG)
19 logging.getLogger('nio').setLevel(logging.WARNING)
20
21 async def do_shop(match):
22 '''
23 Prefix: shop
24
25 Synax:
26 shop COMMAND ARGS
27
28 Commands:
29 help - show this help
30 extra.help - show extended help
31 add - add an item to shop for
32 add.store - add a store
33 at STORE_ID - use this store for future commands
34 buy ID ID ID - mark items as purchased
35 clear STORE - clear all purchased items for STORE
36 list - re-show list from last used store
37 past STORE - show purchased items from STORE in last 90 days
38 stores - list all stores
39
40 Examples:
41 shop past costco
42 shop at costco
43
44 shop add sour cream
45 shop add milk
46
47 shop list
48
49 shop buy 1 2 5 7
50
51 '''
52 extended_help = '''
53 assign ITEM_ID - assign item to last used store id
54 assign.item.to.store ITEM_ID STORE_ID - plan to shop for at store for item
55 clear.all STORE - clear **all** items for STORE, including un-purchased
56 rename OLD,NEW - rename an item
57 rename.store OLD,NEW - rename a store
58 remove ITEM_ID - remove a shopping item from the datatbase
59 remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
60 setup - run one time database setup
61 unsorted - list all items not assigned to any store
62 zone ZONE ITEM - list ITEM under given ZONE - ZONE may not contain spaces
63
64 Example:
65 shop zone dairy sour cream
66 '''
67
68 if match.command('setup'):
69 result = Data().setup()
70 return [result]
71
72 if match.command('stores'):
73 items = Data().get_stores()
74 results = ['List of Stores']
75 for item in items:
76 results.append(str(item))
77 return results
78
79 if match.command('add'):
80 new = ' '.join(match.args()[1:])
81 result = Data().add_item(new)
82 results = [f'Item added with id {result}']
83 for item in Data().get_last_shopping_list():
84 results.append(str(item))
85 return results
86
87 if match.command('add.store'):
88 new = ' '.join(match.args()[1:])
89 result = Data().add_store(new)
90 return [f'Store added with id {result}']
91
92 if match.command('assign.item.to.store'):
93 item_id = int(match.args()[1])
94 store_id = int(match.args()[2])
95 Data().assign_store(item_id, store_id)
96 return ['Success']
97
98 if match.command('zone'):
99 zone = match.args()[1]
100 item = ' '.join(match.args()[2:])
101 Data().assign_zone(item, zone)
102 return ['Success']
103
104 if match.command('assign'):
105 item_id = int(match.args()[1])
106 Data().assign_last_store(item_id)
107
108 # Now print the list for the assigned store
109 items = Data().get_last_shopping_list()
110 results = ['Shopping List']
111 for item in items:
112 results.append(str(item))
113 return results
114
115 if match.command('buy'):
116 items = ' '.join(match.args()[1:])
117 results = Data().purchase_items(items)
118 # results = []
119 # for item in Data().get_last_shopping_list():
120 # results.append(str(item))
121 return results
122
123 if match.command('clear'):
124 store_name = ' '.join(match.args()[1:])
125 results = Data().clear_list(store_name)
126 return results
127
128 if match.command('clear.all'):
129 store_name = ' '.join(match.args()[1:])
130 results = Data().clear_list(store_name, clear_all=True)
131 return results
132
133 if match.command('at'):
134 store_name = ' '.join(match.args()[1:])
135 items = Data().get_shopping_list(store_name)
136 results = ['Shopping List']
137 for item in items:
138 results.append(str(item))
139 return results
140
141 if match.command('list'):
142 items = Data().get_last_shopping_list()
143 results = ['Shopping List']
144 for item in items:
145 results.append(str(item))
146 return results
147
148
149 if match.command('past'):
150 store_name = ' '.join(match.args()[1:])
151 items = Data().list_purchases(store_name)
152 results = [f'# Previously purchased at {store_name}']
153 for item in items:
154 results.append(str(item))
155 return results
156
157
158 if match.command('unsorted'):
159 items = Data().get_unsorted_list()
160 results = ['Items assigned to no store']
161 for item in items:
162 results.append(str(item))
163 return results
164
165 if match.command('rename'):
166 old,new = ' '.join(match.args()[1:]).split(',')
167 Data().rename_item(old,new)
168 return ['Success']
169
170 if match.command('rename.store'):
171 old,new = ' '.join(match.args()[1:]).split(',')
172 Data().rename_store(old,new)
173 return ['Success']
174
175 if match.command('remove'):
176 item_id = ' '.join(match.args()[1:])
177 Data().remove_item(item_id)
178 return ['Success']
179
180 if match.command('remove.store'):
181 store_id = ' '.join(match.args()[1:])
182 Data().remove_store(store_id)
183 return ['Success']
184
185
186 if match.command('extra.help'):
187 return [extended_help]
188
189 return [do_shop.__doc__]
190
191 async def shop_bot(room, message, bot):
192 match = botlib.MessageMatch(room, message, bot, PREFIX)
193
194 if not match.is_not_from_this_bot() and match.prefix():
195 return
196
197 try:
198 if match.prefix():
199 result_lines = await do_shop(match)
200
201 message_lines = []
202 for line in result_lines:
203 message_lines.append(str(line))
204
205 message = '\n'.join(message_lines)
206
207 await bot.api.send_text_message(
208 room.room_id, message)
209
210 except Exception as ex:
211 for line in ['You have:', ' no tea', str(ex)]:
212 await bot.api.send_text_message(
213 room.room_id, str(line))
214
215
216