]> apps.delaporte.us Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
WIP - Pretty print shopping list by zone
[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 - mark item as purchased
35 clear STORE - clear all purchased items for STORE
36 list - re-show list from last used store
37 past STORE - re-show past purchased items from STORE
38 stores - list all stores
39 '''
40 extended_help = '''
41 assign ITEM_ID - assign item to last used store id
42 assign.item.to.store ITEM_ID STORE_ID - plan to shop for at store for item
43 clear.all STORE - clear **all** items for STORE
44 remove ITEM_ID - remove a shopping item from the datatbase
45 remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
46 setup - run one time database setup
47 unsorted - list all items not assigned to any store
48
49 '''
50
51 if match.command('setup'):
52 result = Data().setup()
53 return [result]
54
55 if match.command('stores'):
56 items = Data().get_stores()
57 results = ['List of Stores']
58 for item in items:
59 results.append(str(item))
60 return results
61
62 if match.command('add'):
63 new = ' '.join(match.args()[1:])
64 result = Data().add_item(new)
65 results = [f'Item added with id {result}']
66 for item in Data().get_last_shopping_list():
67 results.append(str(item))
68 return results
69
70 if match.command('add.store'):
71 new = ' '.join(match.args()[1:])
72 result = Data().add_store(new)
73 return [f'Store added with id {result}']
74
75 if match.command('assign.item.to.store'):
76 item_id = int(match.args()[1])
77 store_id = int(match.args()[2])
78 Data().assign_store(item_id, store_id)
79 return ['Success']
80
81 if match.command('assign'):
82 item_id = int(match.args()[1])
83 Data().assign_last_store(item_id)
84
85 # Now print the list for the assigned store
86 items = Data().get_last_shopping_list()
87 results = ['Shopping List']
88 for item in items:
89 results.append(str(item))
90 return results
91
92 if match.command('buy'):
93 items = ' '.join(match.args()[1:])
94 results = Data().purchase_items(items)
95 # results = []
96 # for item in Data().get_last_shopping_list():
97 # results.append(str(item))
98 return results
99
100 if match.command('clear'):
101 store_name = ' '.join(match.args()[1:])
102 results = Data().clear_list(store_name)
103 return results
104
105 if match.command('clear.all'):
106 store_name = ' '.join(match.args()[1:])
107 results = Data().clear_list(store_name, clear_all=True)
108 return results
109
110 if match.command('at'):
111 store_name = ' '.join(match.args()[1:])
112 items = Data().get_shopping_list(store_name)
113 results = ['Shopping List']
114 for item in items:
115 results.append(str(item))
116 return results
117
118 if match.command('list'):
119 items = Data().get_last_shopping_list()
120 results = ['Shopping List']
121 for item in items:
122 results.append(str(item))
123 return results
124
125
126 if match.command('past'):
127 store_name = ' '.join(match.args()[1:])
128 items = Data().list_purchases(store_name)
129 results = ['Past purchases...']
130 for item in items:
131 results.append(str(item))
132 return results
133
134
135 if match.command('unsorted'):
136 items = Data().get_unsorted_list()
137 results = ['Items assigned to no store']
138 for item in items:
139 results.append(str(item))
140 return results
141
142 if match.command('remove'):
143 item_id = ' '.join(match.args()[1:])
144 Data().remove_item(item_id)
145 return ['Success']
146
147 if match.command('remove.store'):
148 store_id = ' '.join(match.args()[1:])
149 Data().remove_store(store_id)
150 return ['Success']
151
152
153 if match.command('extra.help'):
154 return [extended_help]
155
156 return [do_shop.__doc__]
157
158 async def shop_bot(room, message, bot):
159 match = botlib.MessageMatch(room, message, bot, PREFIX)
160
161 if not match.is_not_from_this_bot() and match.prefix():
162 return
163
164 try:
165 if match.prefix():
166 result_lines = await do_shop(match)
167 for line in result_lines:
168 await bot.api.send_text_message(
169 room.room_id, str(line))
170 except Exception as ex:
171 for line in ['You have:', ' no tea', str(ex)]:
172 await bot.api.send_text_message(
173 room.room_id, str(line))
174
175
176