-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.py
323 lines (273 loc) · 9.85 KB
/
store.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import sqlite3
from reagent import Reagent
class Store:
def __init__(self, database):
self.database = database
self.conn = sqlite3.connect(self.database)
self.__auctions = AuctionsTable(self)
self.__downloads = DownloadsTable(self)
self.__recipes = RecipesTable(self)
self.__reagents = ReagentsTable(self)
self.__quantities = QuantitiesTable(self)
self.__credentials = CredentialsTable(self)
self.__cache = CacheTable(self)
self.__initialize()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.conn.close()
def __initialize(self):
self.__auctions.create_if_exists()
self.__downloads.create_if_exists()
self.__recipes.create_if_exists()
self.__reagents.create_if_exists()
self.__quantities.create_if_exists()
self.__credentials.create_if_exists()
self.__cache.create_if_exists()
def add_auctions(self, listings, datetime):
self.__auctions.insert(listings)
self.__downloads.insert(datetime)
def get_last_download(self):
cur = self.conn.execute(
'SELECT datetime FROM downloads ORDER BY datetime DESC LIMIT 1')
datetime = cur.fetchone()
return datetime[0] if datetime is not None else None
def get_reagents_price(self, recipe_id):
datetime = self.get_last_download()
if datetime is None:
print('download from auction house first before searching.')
return None
cur = self.conn.execute('''
SELECT q.item_id, r.name, r.craftable, q.quantity, a.price
FROM quantities q
INNER JOIN reagents r
ON q.item_id = r.item_id
AND recipe_id = ?
INNER JOIN (
SELECT * FROM auctions
WHERE datetime = ?
) a ON q.item_id = a.item_id
''', (recipe_id, datetime))
return cur.fetchall()
def get_price(self, item_id):
datetime = self.get_last_download()
if datetime is None:
print('download from auction house first before searching.')
return None
cur = self.conn.execute('''
SELECT price
FROM auctions
WHERE item_id = ? and datetime = ?
''', (item_id, datetime))
return cur.fetchone()
def get_price_by_name(self, item_name):
cur = self.conn.execute('''
SELECT item_id
FROM reagents
WHERE name = ?
''', (item_name,))
item_id = cur.fetchone()
if item_id is None:
return None
return self.get_price(item_id[0])
def get_price_history(self, item_name):
cur = self.conn.execute('''
SELECT item_id
FROM reagents
WHERE name = ?
''', (item_name,))
item_id = cur.fetchone()
if item_id is None:
return None
cur = self.conn.execute('''
SELECT *
FROM auctions
WHERE item_id = ?
''', (item_id[0],))
return cur.fetchall()
def add_recipes(self, recipes):
self.__recipes.insert(recipes)
for recipe in recipes:
self.__reagents.upsert(recipe.item_id, recipe.item_name, 1)
self.__reagents.insert_list(recipe.reagents)
self.__quantities.insert(recipe.id, recipe.reagents)
def get_recipe(self, id):
recipe = None
cur = self.conn.execute('''
SELECT r.*, i.name FROM (SELECT *
FROM recipes
WHERE item_id = ?) r
INNER JOIN reagents AS i
ON r.item_id = i.item_id
''', (id,))
recipe = cur.fetchone()
if recipe is None:
cur = self.conn.execute('''
SELECT r.*, i.name FROM (SELECT *
FROM recipes
WHERE name = ?) r
INNER JOIN reagents AS i
ON r.item_id = i.item_id
''', (id,))
recipe = cur.fetchone()
return recipe
def get_recipe_count(self, profession, skill_tier):
cur = self.conn.execute(
'SELECT COUNT(*) FROM recipes WHERE profession = ? AND skilltier = ?', (profession, skill_tier))
return cur.fetchone()[0]
def get_all_reagent_ids(self):
cur = self.conn.execute('SELECT item_id FROM reagents')
return [i[0] for i in cur.fetchall()]
def get_all_reagents(self):
cur = self.conn.execute('SELECT * FROM reagents')
return cur.fetchall()
def get_credentials(self):
return self.__credentials.get()
def add_or_replace_credentials(self, client_id, client_secret, datetime):
creds = self.__credentials.get()
if creds is not None:
self.__credentials.clear()
self.__credentials.insert(client_id, client_secret, datetime)
def clear_credentials(self):
self.__credentials.clear()
def get_cache(self):
return self.__cache.get()
def update_cache(self, realm_name):
self.__cache.update(realm_name)
class Table:
def __init__(self, store):
self.store = store
def create_if_exists(self):
pass
class AuctionsTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS auctions
(
item_id TEXT NOT NULL,
price INTEGER NOT NULL,
quantity INTEGER NOT NULL,
datetime TEXT NOT NULL
)
''')
self.store.conn.commit()
def insert(self, auctions):
auctions_insert = [(a.id, a.price, a.quantity, a.datetime)
for a in auctions]
self.store.conn.executemany(
'INSERT INTO auctions VALUES (?, ?, ?, ?)', auctions_insert)
self.store.conn.commit()
class DownloadsTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS downloads
(
datetime TEXT NOT NULL
)
''')
self.store.conn.commit()
def insert(self, datetime):
self.store.conn.execute(
'INSERT INTO downloads VALUES (?)', (datetime,))
self.store.conn.commit()
class RecipesTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS recipes
(
recipe_id INTEGER NOT NULL PRIMARY KEY,
profession INTEGER NOT NULL,
skilltier INTEGER NOT NULL,
name TEXT NOT NULL,
item_id TEXT NOT NULL,
crafted_quantity INTEGER NOT NULL
)
''')
self.store.conn.commit()
def insert(self, recipes):
recipes_insert = [(r.id, r.profession, r.skill_tier, r.name,
r.item_id, r.crafted_quantity) for r in recipes]
self.store.conn.executemany(
'INSERT INTO recipes VALUES (?, ?, ?, ?, ?, ?)', recipes_insert)
self.store.conn.commit()
class ReagentsTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS reagents
(
item_id TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
craftable INTEGER NOT NULL
)
''')
self.store.conn.commit()
def upsert(self, item_id, name, craftable):
self.store.conn.execute('''
INSERT INTO reagents
VALUES (?, ?, ?)
ON CONFLICT (item_id)
DO UPDATE SET craftable = ?
''', (item_id, name, craftable, craftable))
self.store.conn.commit()
def insert_list(self, items):
items_insert = [(i.id, i.name, 0) for i in items]
self.store.conn.executemany(
'INSERT OR IGNORE INTO reagents VALUES (?, ?, ?)', items_insert)
self.store.conn.commit()
class QuantitiesTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS quantities
(
item_id TEXT NOT NULL,
recipe_id INTEGER NOT NULL,
quantity INTEGER NOT NULL
)
''')
self.store.conn.commit()
def get(self, recipe_id):
cur = self.store.conn.execute(
'SELECT * FROM quantities WHERE recipe_id = ?', recipe_id)
return cur.fetchall()
def insert(self, recipe_id, items):
quantities_insert = [(i.id, recipe_id, i.quantity) for i in items]
self.store.conn.executemany(
'INSERT OR IGNORE INTO quantities VALUES (?, ?, ?)', quantities_insert)
self.store.conn.commit()
class CredentialsTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS credentials
(
client_id TEXT NOT NULL,
client_secret TEXT NOT NULL,
datetime TEXT NOT NULL
)
''')
self.store.conn.commit()
def get(self):
cur = self.store.conn.execute('SELECT * FROM credentials')
return cur.fetchone()
def insert(self, client_id, client_secret, datetime):
self.store.conn.execute(
'INSERT INTO credentials VALUES (?, ?, ?)', (client_id, client_secret, datetime))
self.store.conn.commit()
def clear(self):
self.store.conn.execute('DELETE FROM credentials')
self.store.conn.commit()
class CacheTable(Table):
def create_if_exists(self):
self.store.conn.execute('''
CREATE TABLE IF NOT EXISTS cache
(
realm_name INTEGER NOT NULL
)
''')
self.store.conn.commit()
def get(self):
cur = self.store.conn.execute('SELECT * FROM cache')
return cur.fetchone()
def update(self, realm_name):
self.store.conn.execute('DELETE FROM cache')
self.store.conn.execute('INSERT INTO cache VALUES (?)', (realm_name,))
self.store.conn.commit()