-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
369 lines (311 loc) · 12.6 KB
/
database.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# database.py
import sqlite3
import json
from contextlib import contextmanager
PREDICTIONS_DB_PATH = "predictions.db"
ORDERS_DB_PATH = "woo_orders.db"
PUB_LEAGUE_DB_PATH = "pub_league.db"
def get_latest_order_id():
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute("SELECT order_id FROM woo_orders ORDER BY order_id DESC LIMIT 1")
result = c.fetchone()
return result[0] if result else None
def count_orders_for_multiple_subgroups(subgroups):
counts = {}
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
for subgroup in subgroups:
query = """
SELECT COUNT(DISTINCT order_id) FROM woo_orders
WHERE order_data LIKE ?
"""
wildcard_subgroup_name = (
f'%"name": "Subgroup designation", "value": "{subgroup}"%'
)
c.execute(query, (wildcard_subgroup_name,))
result = c.fetchone()
counts[subgroup] = result[0] if result else 0
return counts
def get_members_for_subgroup(subgroup):
members = []
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
query = """
SELECT DISTINCT order_data FROM woo_orders
WHERE order_data LIKE ?
"""
wildcard_subgroup_name = (
f'%"name": "Subgroup designation", "value": "{subgroup}"%'
)
c.execute(query, (wildcard_subgroup_name,))
for row in c.fetchall():
order_data = json.loads(row[0])
billing_info = order_data.get("billing", {})
member = {
"first_name": billing_info.get("first_name", ""),
"last_name": billing_info.get("last_name", ""),
"email": billing_info.get("email", ""),
}
if member not in members:
members.append(member)
return members
@contextmanager
def get_db_connection(db_path):
conn = sqlite3.connect(db_path)
try:
yield conn
finally:
conn.close()
def initialize_predictions_db():
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"""CREATE TABLE IF NOT EXISTS predictions
(match_id TEXT, user_id TEXT, prediction TEXT, timestamp DATETIME)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS match_threads
(thread_id TEXT, match_id TEXT)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS match_schedule (
match_id TEXT,
opponent TEXT,
date_time DATETIME,
venue TEXT,
is_home_game BOOLEAN,
match_summary_link TEXT,
match_stats_link TEXT,
match_commentary_link TEXT,
thread_created INTEGER DEFAULT 0,
live_updates_active INTEGER DEFAULT 0
)"""
)
c.execute("PRAGMA table_info(match_schedule)")
columns = [row[1] for row in c.fetchall()]
if 'competition' not in columns:
c.execute("ALTER TABLE match_schedule ADD COLUMN competition TEXT DEFAULT 'usa.1'")
conn.commit()
def insert_match_schedule(match_id, opponent, date_time, is_home_game, summary_link, stats_link, commentary_link, venue, competition):
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"INSERT INTO match_schedule (match_id, opponent, date_time, is_home_game, match_summary_link, match_stats_link, match_commentary_link, venue, competition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
(match_id, opponent, date_time, is_home_game, summary_link, stats_link, commentary_link, venue, competition)
)
conn.commit()
def insert_match_thread(thread_id, match_id):
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute("INSERT INTO match_threads VALUES (?, ?)", (thread_id, match_id))
conn.commit()
def insert_prediction(match_id, user_id, prediction):
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"SELECT * FROM predictions WHERE match_id=? AND user_id=?",
(match_id, user_id),
)
if c.fetchone():
return False
c.execute(
"INSERT INTO predictions VALUES (?, ?, ?, CURRENT_TIMESTAMP)",
(match_id, user_id, prediction),
)
conn.commit()
return True
def get_predictions(match_id):
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"SELECT prediction, COUNT(*) FROM predictions WHERE match_id=? GROUP BY prediction",
(match_id,),
)
return c.fetchall()
def load_match_threads():
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute("SELECT * FROM match_threads")
return {thread_id: match_id for thread_id, match_id in c.fetchall()}
def reset_woo_orders_db():
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"""DELETE FROM order_extract WHERE 1=1"""
)
c.execute(
"""DELETE FROM woo_orders WHERE 1=1"""
)
c.execute("""
UPDATE latest_order_info
SET latest_order_id = '0'
WHERE id = 1
""")
conn.commit()
def initialize_woo_orders_db():
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"""CREATE TABLE IF NOT EXISTS woo_orders
(order_id TEXT PRIMARY KEY, order_data TEXT)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS order_extract
(order_id TEXT PRIMARY KEY,
product_name TEXT,
first_name TEXT,
last_name TEXT,
email_address TEXT,
order_date TEXT,
item_qty INTEGER,
item_price TEXT,
order_status TEXT,
order_note TEXT,
product_variation TEXT,
billing_address TEXT)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS latest_order_info
(id INTEGER PRIMARY KEY,
latest_order_id TEXT)"""
)
c.execute("""
INSERT INTO latest_order_info (id, latest_order_id)
VALUES (1, '0')
ON CONFLICT(id) DO NOTHING
""")
conn.commit()
def update_latest_order_id(order_id):
try:
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute("""
INSERT INTO latest_order_info (id, latest_order_id)
VALUES (1, ?)
ON CONFLICT(id) DO UPDATE SET latest_order_id = excluded.latest_order_id
""", (order_id,))
conn.commit()
except Exception as e:
print(f"Error updating latest order ID: {e}")
def get_latest_order_id():
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute("SELECT latest_order_id FROM latest_order_info WHERE id = 1")
result = c.fetchone()
return result[0] if result else None
def insert_order_extract(order_id, product_name, first_name, last_name, email_address, order_date, item_qty, item_price, order_status, order_note, product_variation, billing_address):
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"INSERT OR REPLACE INTO order_extract (order_id, product_name, first_name, last_name, email_address, order_date, item_qty, item_price, order_status, order_note, product_variation, billing_address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(order_id, product_name, first_name, last_name, email_address, order_date, item_qty, item_price, order_status, order_note, product_variation, billing_address),
)
conn.commit()
def get_order_extract(product_title):
with get_db_connection(ORDERS_DB_PATH) as conn:
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("SELECT * FROM order_extract WHERE product_name = ? ORDER BY email_address, order_id", (product_title,))
rows = c.fetchall()
return [dict(row) for row in rows]
def prep_order_extract():
with get_db_connection(ORDERS_DB_PATH) as conn:
conn.execute(
"DELETE FROM order_extract"
)
conn.commit()
def update_woo_orders(order_id, order_data):
with get_db_connection(ORDERS_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"INSERT OR REPLACE INTO woo_orders (order_id, order_data) VALUES (?, ?)",
(order_id, order_data),
)
conn.commit()
def load_existing_dates():
with get_db_connection(PREDICTIONS_DB_PATH) as conn:
c = conn.cursor()
c.execute("SELECT date_time, competition FROM match_schedule")
existing_dates = [(row[0], row[1]) for row in c.fetchall()]
return existing_dates
def initialize_pub_league_db():
with get_db_connection(PUB_LEAGUE_DB_PATH) as conn:
c = conn.cursor()
c.execute(
"""CREATE TABLE IF NOT EXISTS leagues (
league_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE
)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS teams (
team_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
league_id INTEGER,
FOREIGN KEY (league_id) REFERENCES leagues (league_id)
)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS schedules (
schedule_id INTEGER PRIMARY KEY AUTOINCREMENT,
week INTEGER,
date TEXT,
time TEXT,
opponent TEXT,
team_id INTEGER,
FOREIGN KEY (team_id) REFERENCES teams (team_id)
)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS coaches (
coach_id INTEGER PRIMARY KEY AUTOINCREMENT,
discord_tag TEXT,
team_id INTEGER,
FOREIGN KEY (team_id) REFERENCES teams (team_id)
)"""
)
c.execute(
"""CREATE TABLE IF NOT EXISTS members (
member_id INTEGER PRIMARY KEY AUTOINCREMENT,
discord_tag TEXT,
team_id INTEGER,
FOREIGN KEY (team_id) REFERENCES teams (team_id)
)"""
)
conn.commit()
def load_league_data(json_file):
with open(json_file, 'r') as f:
data = json.load(f)
with get_db_connection(PUB_LEAGUE_DB_PATH) as conn:
c = conn.cursor()
for league_name, league_info in data['leagues'].items():
# Insert league
c.execute("INSERT OR IGNORE INTO leagues (name) VALUES (?)", (league_name,))
c.execute("SELECT league_id FROM leagues WHERE name=?", (league_name,))
league_id = c.fetchone()[0]
for team_info in league_info['teams']:
team_name = team_info['name']
# Insert team
c.execute("INSERT OR IGNORE INTO teams (name, league_id) VALUES (?, ?)", (team_name, league_id))
c.execute("SELECT team_id FROM teams WHERE name=?", (team_name,))
team_id = c.fetchone()[0]
for match_info in team_info['schedule']:
week = match_info['week']
for match in match_info['matches']:
date = match['date']
time = match['time']
opponent = match['opponent']
# Insert schedule
c.execute("INSERT INTO schedules (week, date, time, opponent, team_id) VALUES (?, ?, ?, ?, ?)", (week, date, time, opponent, team_id))
conn.commit()
def insert_coach(discord_tag, team_id):
with get_db_connection(PUB_LEAGUE_DB_PATH) as conn:
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO coaches (discord_tag, team_id) VALUES (?, ?)", (discord_tag, team_id))
conn.commit()
def insert_member(discord_tag, team_id):
with get_db_connection(PUB_LEAGUE_DB_PATH) as conn:
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO members (discord_tag, team_id) VALUES (?, ?)", (discord_tag, team_id))
conn.commit()