-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
631 lines (507 loc) · 16.1 KB
/
db.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import sqlite3
import config
import time
from threading import Lock
conn = sqlite3.connect(config.database, check_same_thread=False, timeout=1)
cursor = conn.cursor()
lock = Lock()
def get_id_by_refcode(refcode):
try:
lock.acquire()
sql = 'select ID from USERS where REFERAL_CODE=?'
cursor.execute(sql, (refcode,))
fetch = cursor.fetchone()
if fetch is not None:
return fetch[0]
else:
return None
finally:
lock.release()
def add_user(user_id, refcode, time_s, ref_parent=None, ref_parent_2=None, ref_parent_3=None):
try:
lock.acquire()
if ref_parent is None:
sql = 'insert into USERS (ID, REFERAL_CODE, REGISTRED_TIME) values(?, ?, ?)'
cursor.execute(sql, (str(user_id), str(time_s), refcode))
else:
sql = '''
insert into USERS (ID, REFERAL_CODE, REFERAL_PARENT, REFERAL_PARENT_2, REFERAL_PARENT_3, REGISTRED_TIME)
values(?, ?, ?, ?, ?, ?)'''
cursor.execute(sql,
(str(user_id), refcode, ref_parent, ref_parent_2, ref_parent_3, str(time_s)))
conn.commit()
finally:
lock.release()
def is_new_user(user_id):
try:
lock.acquire()
cursor.execute('select * from USERS where ID=?', (str(user_id),))
return cursor.fetchone() is None
finally:
lock.release()
def is_trained(user_id):
try:
lock.acquire()
cursor.execute('select IS_TRAINED from USERS where ID=?', (str(user_id),))
return cursor.fetchone()[0] == 1
finally:
lock.release()
def is_registred(user_id):
try:
lock.acquire()
cursor.execute('select EMAIL from USERS where ID=?', (str(user_id),))
return cursor.fetchone()[0] is not None
finally:
lock.release()
def get_state(user_id):
try:
lock.acquire()
cursor.execute('select STATE from USERS where ID=?', (str(user_id),))
return cursor.fetchone()[0]
finally:
lock.release()
def get_amount(user_id):
try:
lock.acquire()
cursor.execute('select AMOUNT from USERS where ID=?', (str(user_id),))
return cursor.fetchone()[0]
finally:
lock.release()
def set_state(user_id, state):
try:
lock.acquire()
cursor.execute('update USERS set STATE = ? where ID = ?', (state, str(user_id)))
conn.commit()
finally:
lock.release()
def set_name(user_id, name):
try:
lock.acquire()
cursor.execute('update USERS set NAME = ? where ID = ?', (name, str(user_id)))
conn.commit()
finally:
lock.release()
def set_phone(user_id, phone):
try:
lock.acquire()
cursor.execute('update USERS set PHONE = ? where ID = ?', (phone, str(user_id)))
conn.commit()
finally:
lock.release()
def set_email(user_id, email):
try:
lock.acquire()
cursor.execute('update USERS set EMAIL = ? where ID = ?', (email, str(user_id)))
conn.commit()
finally:
lock.release()
def set_paysign(user_id, code):
try:
lock.acquire()
cursor.execute('update USERS set PAY_SIGN = ? where ID = ?', (code, str(user_id)))
conn.commit()
finally:
lock.release()
def set_balance(user_id, balance):
try:
lock.acquire()
cursor.execute('update USERS set BALANCE = ? where ID = ?', (balance, str(user_id)))
conn.commit()
finally:
lock.release()
def set_salary(user_id, salary):
try:
cursor.execute('update USERS set MONTH_SALARY = ? where ID = ?', (salary, str(user_id)))
conn.commit()
finally:
lock.release()
def set_amount(user_id, amount):
try:
lock.acquire()
cursor.execute('update USERS set AMOUNT = ? where ID = ?', (amount, str(user_id)))
conn.commit()
finally:
lock.release()
def set_expire(user_id, exp):
try:
lock.acquire()
cursor.execute('update USERS set EXPIRATION_TIME = ? where ID = ?', (exp, str(user_id)))
conn.commit()
finally:
lock.release()
def trained(user_id):
try:
lock.acquire()
cursor.execute('update USERS set IS_TRAINED = 1 where ID = ?', (str(user_id),))
conn.commit()
finally:
lock.release()
def get_paysign(user_id):
try:
lock.acquire()
cursor.execute('select PAY_SIGN from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_name_by_refcode(refcode):
try:
lock.acquire()
cursor.execute('select NAME from USERS where REFERAL_CODE=?', (refcode,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_phone_by_refparent(refparent):
try:
lock.acquire()
cursor.execute('select PHONE from USERS where REFERAL_PARENT=?', (str(refparent),))
return cursor.fetchone()[0]
finally:
lock.release()
def get_registred_time(user_id):
try:
lock.acquire()
cursor.execute('select REGISTRED_TIME from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_balance(user_id):
try:
lock.acquire()
cursor.execute('select BALANCE from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_salary(user_id):
try:
lock.acquire()
cursor.execute('select MONTH_SALARY from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_expiration_time(user_id):
try:
lock.acquire()
cursor.execute('select EXPIRATION_TIME from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_refcode_by_id(user_id):
try:
lock.acquire()
cursor.execute('select REFERAL_CODE from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_name_by_id(user_id):
try:
lock.acquire()
cursor.execute('select NAME from USERS where ID=?', (user_id,))
fetch = cursor.fetchone()
if fetch is not None:
return fetch[0]
else:
return None
finally:
lock.release()
def get_parent_by_id(user_id):
try:
lock.acquire()
cursor.execute('select REFERAL_PARENT from USERS where ID=?', (user_id,))
fetch = cursor.fetchone()
if fetch is not None:
return fetch[0]
else:
return None
finally:
lock.release()
def get_phone_by_id(user_id):
try:
lock.acquire()
cursor.execute('select PHONE from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_parents_by_id(user_id):
try:
lock.acquire()
cursor.execute('select REFERAL_PARENT, REFERAL_PARENT_2, REFERAL_PARENT_3 from USERS where ID=?', (user_id,))
fetch = cursor.fetchone()
if fetch is not None:
return fetch
else:
return None
finally:
lock.release()
def get_lines_by_id(user_id):
try:
lock.acquire()
cursor.execute('select ID from USERS where REFERAL_PARENT=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchone()
if fetch is not None:
line_1 = fetch
else:
return None, None, None
cursor.execute('select ID from USERS where REFERAL_PARENT_2=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchone()
if fetch is not None:
line_2 = fetch
else:
return line_1, None, None
cursor.execute('select ID from USERS where REFERAL_PARENT_3=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchone()
if fetch is not None:
line_3 = fetch
return line_1, line_2, line_3
else:
return line_1, line_2, None
finally:
lock.release()
def get_line_1_by_id(user_id):
try:
lock.acquire()
cursor.execute('select ID from USERS where REFERAL_PARENT=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchall()
if fetch is not None:
return fetch
else:
return None
finally:
lock.release()
def get_line_2_by_id(user_id):
cursor.execute('select ID from USERS where REFERAL_PARENT_2=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchall()
if fetch is not None:
return fetch
else:
return None
def get_line_3_by_id(user_id):
try:
lock.acquire()
cursor.execute('select ID from USERS where REFERAL_PARENT_3=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchall()
if fetch is not None:
return fetch
else:
return None
finally:
lock.release()
def select_subs():
try:
lock.acquire()
cursor.execute('select ID from USERS where EXPIRATION_TIME > ? OR FREE_ACCESS=1', (time.time(),))
fetch = cursor.fetchall()
if fetch is not None:
return fetch
else:
return None
finally:
lock.release()
def get_lines_len_by_id(user_id):
try:
lock.acquire()
cursor.execute('select ID from USERS where REFERAL_PARENT=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchall()
line_1_len = len(fetch)
cursor.execute('select ID from USERS where REFERAL_PARENT_2=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchall()
line_2_len = len(fetch)
cursor.execute('select ID from USERS where REFERAL_PARENT_3=? and EXPIRATION_TIME != -1', (user_id,))
fetch = cursor.fetchall()
line_3_len = len(fetch)
return line_1_len, line_2_len, line_3_len
finally:
lock.release()
def get_api_code_by_id(user_id):
try:
lock.acquire()
cursor.execute('select PAY_WALLET from USERS where ID=?', (user_id,))
wallet = cursor.fetchone()[0]
# print(wallet)
cursor.execute('select CODE from MONEY where WALLET=?', (wallet,))
code = cursor.fetchone()[0]
# print(code)
return code
finally:
lock.release()
def new_month():
try:
lock.acquire()
cursor.execute('update USERS set MONTH_SALARY = 0')
conn.commit()
finally:
lock.release()
def update_links(xbet_link, xbet_mobile_link):
try:
lock.acquire()
cursor.execute('update UTILS set XBET_LINK = ?, XBET_MOBILE_LINK = ?', (xbet_link, xbet_mobile_link))
conn.commit()
finally:
lock.release()
def get_links():
try:
lock.acquire()
cursor.execute('select XBET_LINK, XBET_MOBILE_LINK from UTILS')
return cursor.fetchone()
finally:
lock.release()
def select_users():
try:
lock.acquire()
cursor.execute('select ID from USERS')
return cursor.fetchall()
finally:
lock.release()
def set_at_id(user_id, at_id):
try:
lock.acquire()
cursor.execute('update USERS set AT_ID = ? where ID = ?', (str(at_id), user_id))
conn.commit()
finally:
lock.release()
def get_at_id(user_id):
try:
lock.acquire()
cursor.execute('select AT_ID from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def increment_line(line_n, user_id):
try:
lock.acquire()
requests_1 = {
0: 'select LINE_1 from USERS where ID=?',
1: 'select LINE_2 from USERS where ID=?',
2: 'select LINE_3 from USERS where ID=?'
}
requests_2 = {
0: 'update USERS set LINE_1 = ? where ID = ?',
1: 'update USERS set LINE_2 = ? where ID = ?',
2: 'update USERS set LINE_3 = ? where ID = ?'
}
cursor.execute(requests_1[line_n], (user_id,))
line_c = int(cursor.fetchone()[0]) + 1
cursor.execute(requests_2[line_n], (line_c, user_id))
conn.commit()
return line_c
finally:
lock.release()
def get_email(user_id):
try:
lock.acquire()
cursor.execute('select EMAIL from USERS where ID=?', (user_id,))
return cursor.fetchone()[0]
finally:
lock.release()
def get_user_by_pay_sign(pay_sign):
try:
lock.acquire()
cursor.execute('select ID from USERS where PAY_SIGN=?', (pay_sign,))
fetch = cursor.fetchone()
if fetch is not None:
return fetch[0]
else:
return None
finally:
lock.release()
def is_first_pay(user_id):
try:
lock.acquire()
cursor.execute('select EXPIRATION_TIME from USERS where ID=?', (user_id,))
return cursor.fetchone()[0] == -1
finally:
lock.release()
def is_blocked(user_id):
try:
lock.acquire()
cursor.execute('select BLOCKED from USERS where ID=?', (user_id,))
return cursor.fetchone()[0] == 1
finally:
lock.release()
def is_have_free_access(user_id):
try:
lock.acquire()
cursor.execute('select FREE_ACCESS from USERS where ID=?', (user_id,))
return cursor.fetchone()[0] == 1
finally:
lock.release()
def invert_block(user_id):
try:
lock.acquire()
cursor.execute('update users set BLOCKED=BLOCKED*-1 where ID=?', (user_id,))
conn.commit()
finally:
lock.release()
def invert_access(user_id):
try:
lock.acquire()
cursor.execute('update users set FREE_ACCESS=FREE_ACCESS*-1 where ID=?', (user_id,))
conn.commit()
finally:
lock.release()
def update_msgs(xbet_link, xbet_mobile_link, video, faq, channels):
try:
lock.acquire()
cursor.execute('update utils set XBET_LINK = ?, XBET_MOBILE_LINK = ?, VIDEO = ?, FAQ = ?, CHANNELS = ?',
(xbet_link, xbet_mobile_link, video, faq, channels))
conn.commit()
finally:
lock.release()
def get_video():
try:
lock.acquire()
cursor.execute('select VIDEO from UTILS')
return cursor.fetchone()[0]
finally:
lock.release()
def get_faq():
try:
lock.acquire()
cursor.execute('select FAQ from UTILS')
return cursor.fetchone()[0]
finally:
lock.release()
def get_channels():
try:
lock.acquire()
cursor.execute('select CHANNELS from UTILS')
return cursor.fetchone()[0]
finally:
lock.release()
def file_in(at_id):
try:
lock.acquire()
cursor.execute('select PLACE from FILES where AT_ID=?', (at_id,))
return cursor.fetchone() is not None
finally:
lock.release()
def update_file(file_id, caption, place, at_id):
try:
lock.acquire()
cursor.execute('update FILES set ID=?, CAPTION=?, PLACE=? where AT_ID=?', (file_id, caption, place, at_id))
conn.commit()
finally:
lock.release()
def add_file(file_id, caption, place, at_id):
try:
lock.acquire()
sql = 'insert into FILES (ID, CAPTION, PLACE, AT_ID) values(?, ?, ?, ?)'
cursor.execute(sql, (str(file_id), str(caption), str(place), str(at_id)))
conn.commit()
finally:
lock.release()
def get_franchise_files():
cursor.execute("select ID, CAPTION from FILES where PLACE='Обучение по франшизе'")
for file_id, caption in cursor.fetchall():
yield (file_id, caption)
def get_signal_files():
cursor.execute("select ID, CAPTION from FILES where PLACE='Обучение по сигналам'")
for file_id, caption in cursor.fetchall():
yield (file_id, caption)
def get_presentation_files():
cursor.execute("select ID, CAPTION from FILES where PLACE='Презентация'")
for file_id, caption in cursor.fetchall():
yield (file_id, caption)
def get_termsofuse_files():
cursor.execute("select ID, CAPTION from FILES where PLACE='Условия использования'")
for file_id, caption in cursor.fetchall():
yield (file_id, caption)