-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.py
103 lines (88 loc) · 3.42 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
import sqlite3
results_cons = {}
def init_users():
with sqlite3.connect('users.db') as users_con:
users_con.execute("""
CREATE TABLE IF NOT EXISTS USERS (
handle TEXT PRIMARY KEY,
targeturl TEXT,
chat_id TEXT
)
""")
def get_all_handles():
with sqlite3.connect('users.db') as users_con:
user_exists_result = users_con.execute('SELECT handle FROM USERS')
result = user_exists_result.fetchall()
return result if len(result) > 0 else None
def clear_users():
with sqlite3.connect('users.db') as users_con:
users_con.execute('DELETE FROM USERS')
def user_exists(handle):
with sqlite3.connect('users.db') as users_con:
user_exists_result = users_con.execute('SELECT EXISTS(SELECT 1 FROM USERS WHERE handle = ?)', (handle,))
result = user_exists_result.fetchall()
return result[0][0] == 1
def get_userURL(handle):
with sqlite3.connect('users.db') as users_con:
user_target_url = users_con.execute('SELECT targeturl FROM USERS WHERE handle = ?', (handle,))
result = user_target_url.fetchall()
return result[0][0]
def get_userID(handle):
with sqlite3.connect('users.db') as users_con:
user_target_url = users_con.execute('SELECT chat_id FROM USERS WHERE handle = ?', (handle,))
result = user_target_url.fetchall()
return result[0][0]
def add_userURL(handle, url, chat_id):
with sqlite3.connect('users.db') as users_con:
users_con.execute('INSERT INTO USERS (handle, targeturl, chat_id) values(?, ?, ?)', (handle, url, chat_id))
def init_user_results(handle):
with sqlite3.connect('users.db') as users_con:
query = '''
CREATE TABLE IF NOT EXISTS {0} (
url TEXT PRIMARY KEY,
img_url TEXT,
title TEXT,
epoch int,
price TEXT,
distance TEXT
)
'''.format(handle)
users_con.execute(query)
def delete_user_results(handle):
with sqlite3.connect('users.db') as users_con:
query = '''
DELETE FROM {0}
'''.format(handle)
users_con.execute(query)
def print_user_results(handle):
with sqlite3.connect('users.db') as users_con:
query = '''
SELECT * FROM {0}
'''.format(handle)
user_results = users_con.execute(query).fetchall()
def add_user_result(handle, result):
with sqlite3.connect('users.db') as users_con:
query = '''
INSERT OR IGNORE INTO {0} VALUES(?, ?, ?, ?, ?, ?)
'''.format(
handle
)
users_con.execute(query, (result.url, result.img_url, result.title,
result.epoch, result.price, result.distance))
def get_latest_result_epoch(handle):
with sqlite3.connect('users.db') as users_con:
query = '''
SELECT MAX(epoch) FROM {0}
'''.format(
handle
)
result = users_con.execute(query).fetchall()
return result[0][0] if result[0][0] else 0
def update_userURL(handle, url):
with sqlite3.connect('users.db') as users_con:
users_con.execute('UPDATE USERS SET targeturl = ? WHERE handle = ?', (url, handle))
def get_users():
with sqlite3.connect('users.db') as users_con:
user_target_url = users_con.execute('SELECT * FROM USERS')
result = user_target_url.fetchall()
return result