-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_tools.py
More file actions
155 lines (132 loc) · 4.64 KB
/
db_tools.py
File metadata and controls
155 lines (132 loc) · 4.64 KB
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
import os
import sqlite3
from datetime import datetime, timedelta
from os import getenv
from sqlite3 import DatabaseError, OperationalError
from dotenv import load_dotenv
from xui import update_xui_client
load_dotenv(".env")
FS_USER = getenv("FS_USER")
def check_subscription_end(user_id, is_proxy=0, is_vray=0):
"""
Checks if user's subscription has ended
"""
db_conn = SQLUtils()
subscription_end = db_conn.query(
f"select subscription_end from users where user_id={user_id} "
f"and is_proxy={is_proxy} and is_vray={is_vray};"
)
return subscription_end
def get_all_users():
"""
Gets all users from the database
"""
db_conn = SQLUtils()
all_users = db_conn.query("select user_id from users;")
return all_users
def check_all_subscriptions():
"""
Checks all subscriptions
"""
db_conn = SQLUtils()
subscriptions_end_vray = db_conn.query(
"select obfuscated_user from users where subscription_end <= date('now') and is_vray=1;"
)
subscriptions_ends_tomorrow_users_vray = db_conn.query(
"select user_id from users where subscription_end <= date('now', '+2 day') and is_vray=1;"
)
return (
subscriptions_end_vray,
subscriptions_ends_tomorrow_users_vray,
)
def get_obfuscated_user(user_id):
"""
Gets obfuscated user from the database
"""
db_conn = SQLUtils()
obfuscated_user = db_conn.query(
f"select obfuscated_user from users where user_id={user_id};"
)
if not obfuscated_user:
return None
return f"{obfuscated_user}"
def delete_user_subscription(user_id, is_proxy=0, is_vray=0):
"""
Deletes user subscription from the database
"""
db_conn = SQLUtils()
db_conn.mutate(
f"delete from users where obfuscated_user={user_id} "
f"and is_proxy={is_proxy} and is_vray={is_vray};"
)
def need_to_update_user(user_id, obfuscated_user, invoice_payload):
"""
Returns True if user exists in the database and False if not
and updates user's subscription end date if exists,
otherwise inserts new user with subscription end date
"""
db_conn = SQLUtils()
cur_datetime = datetime.now()
subscription_type = invoice_payload.split("_")[0]
prolongation = int(invoice_payload.split("_")[1])
is_proxy = 1 if subscription_type == "proxy" else 0
is_vray = 1 if subscription_type == "vray" else 0
user_exist = db_conn.query(
f"select count(*) from users where user_id={user_id} "
f"and is_proxy={is_proxy} and is_vray={is_vray};"
)
if user_exist:
end_of_period = datetime.fromisoformat(
check_subscription_end(user_id, is_proxy=is_proxy, is_vray=is_vray)
) + timedelta(days=prolongation)
db_conn.mutate(
f"""update users set subscription_end='{end_of_period}'
where user_id={user_id} and is_proxy={is_proxy} and is_vray={is_vray};"""
)
update_xui_client(f"{obfuscated_user}@vray", prolongation)
return True
end_of_period = cur_datetime + timedelta(days=prolongation)
db_conn.mutate(f"""insert into users
(id, user_id, obfuscated_user, subscription_start, subscription_end, is_proxy, is_vray)
values ((select max(id)+1 from users), '{user_id}', '{obfuscated_user}',
'{cur_datetime}', '{end_of_period}', {is_proxy}, {is_vray});""")
return False
class SQLUtils:
"""
Class for working with SQLite database
"""
conn = None
def connect(self):
"""Connects to the database"""
self.conn = sqlite3.connect(
f'/{FS_USER}/vray_mirror_bot/{os.getenv("DB_NAME")}.db'
)
def query(self, request):
"""Executes query"""
try:
cursor = self.conn.cursor()
cursor.execute(request)
except (AttributeError, DatabaseError, OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(request)
fetched = cursor.fetchall()
if len(fetched) == 1:
if len(fetched[0]) == 1:
return fetched[0][0]
return fetched[0]
if len(fetched) > 1 and len(fetched[0]) == 1:
return [x[0] for x in fetched]
return fetched
def mutate(self, request):
"""Executes mutation"""
try:
cursor = self.conn.cursor()
cursor.execute(request)
self.conn.commit()
except (AttributeError, DatabaseError, OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(request)
self.conn.commit()
return cursor