forked from leeyujeong/lunchbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
executable file
·155 lines (133 loc) · 4.31 KB
/
storage.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# coding: utf-8
import contextlib
import importlib
import json
import sqlite3
import sys
@contextlib.contextmanager
def conn(path):
db = sqlite3.connect(path)
try:
yield db
finally:
db.close()
def setup_db(path):
with conn(path) as db:
db.execute(u'''
CREATE TABLE IF NOT EXISTS menu (
name TEXT PRIMARY KEY,
price INT,
restaurant TEXT
)''')
db.execute(u'''
CREATE TABLE IF NOT EXISTS order_record (
id INTEGER PRIMARY KEY AUTOINCREMENT,
handle TEXT,
fullname TEXT,
items TEXT,
total INT,
timestamp NUM
)''')
db.commit()
db.close()
return lambda: conn(path)
class Connect(object):
def __init__(self, connect):
self.connect = connect
class Menu(Connect):
def populate(self, restaurant, menus):
with self.connect() as db:
db.execute(
u'DELETE FROM menu WHERE restaurant = ?',
[restaurant])
for name, price in menus:
ok = True
try:
db.execute(
u'INSERT INTO menu(name, price, restaurant) '
u'VALUES(?, ?, ?)',
(name.replace(' ', ''), price, restaurant))
except sqlite3.Error as e:
ok = False
print name, price
if not ok:
print '!!!!', e
db.commit()
db.close()
def get(self, name):
with self.connect() as db:
rows = list(db.execute(
u'''SELECT name, price, restaurant
FROM menu WHERE name = ?''',
[name]))
if len(rows) == 1:
return rows[0]
rows = list(db.execute(
u'''SELECT name, price, restaurant
FROM menu WHERE name LIKE ?''',
[u'%{}%'.format(name)]))
if len(rows) == 1:
return rows[0]
def getall(self, restaurant=None):
with self.connect() as db:
if restaurant:
where = 'WHERE restaurant = ?'
args = (restaurant,)
else:
where = ''
args = ()
sql = (
u'SELECT name, price FROM menu {} ORDER BY price DESC'
.format(where)
)
return list(db.execute(sql, args))
def getrandombyprice(self, min=1000, max=5000):
with self.connect() as db:
rows = list(db.execute(
u'SELECT name, price FROM menu WHERE price between ? and ? '
u'ORDER BY RANDOM() LIMIT 1', [min, max]))
return rows[0]
class OrderRecord(Connect):
def add(self, handle, fullname, items, total, timestamp):
with self.connect() as db:
sql = u'''
INSERT INTO order_record
(handle, fullname, items, total, timestamp)
VALUES (?, ?, ?, ?, ?)
'''
db.execute(sql, [
handle,
fullname,
json.dumps(items, ensure_ascii=False),
total,
timestamp
])
db.commit()
def get_last_order(self, handle, offset):
with self.connect() as db:
sql = u'''
SELECT items FROM order_record WHERE handle = ?
ORDER BY id DESC LIMIT 1 OFFSET ?
'''
rows = list(db.execute(sql, [handle, offset]))
return rows[0][0] if rows else None
def get_recent_orders(self, handle):
with self.connect() as db:
sql = u'''
SELECT items, total, timestamp
FROM order_record WHERE handle = ?
ORDER BY id DESC LIMIT 20
'''
return list(db.execute(sql, [handle]))
db = setup_db('lunch.sqlite')
menu = Menu(db)
order_record = OrderRecord(db)
if __name__ == '__main__':
if len(sys.argv) > 1:
module_names = sys.argv[1:]
else:
module_names = ['hsd', 'mcdonalds', 'burgerking']
for module_name in module_names:
mod = importlib.import_module(module_name)
menu.populate(module_name, mod.get_menus())