Spaces:
No application file
No application file
Upload bot_db.py
Browse files
bot_db.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gzip
|
2 |
+
import pickle
|
3 |
+
import re
|
4 |
+
import json
|
5 |
+
|
6 |
+
import aiohttp
|
7 |
+
import cryptography.fernet as fernet
|
8 |
+
from gino import Gino
|
9 |
+
|
10 |
+
db = Gino()
|
11 |
+
|
12 |
+
ENCRYPTION_KEY = None
|
13 |
+
|
14 |
+
|
15 |
+
def _generate_key():
|
16 |
+
encryption_key = fernet.Fernet.generate_key()
|
17 |
+
with open('bot_config.py', 'r+') as f:
|
18 |
+
contents = f.read()
|
19 |
+
pattern = r"COOKIE_ENCRYPTION_KEY\s*=\s*None"
|
20 |
+
new_contents = re.sub(
|
21 |
+
pattern, f'COOKIE_ENCRYPTION_KEY = {encryption_key}', contents)
|
22 |
+
f.seek(0)
|
23 |
+
f.write(new_contents)
|
24 |
+
f.truncate()
|
25 |
+
print("Here is your cookie encryption key. The key has been saved in bot_config.py:",
|
26 |
+
encryption_key, "", sep="\n\n")
|
27 |
+
return encryption_key
|
28 |
+
|
29 |
+
|
30 |
+
def _cookies_save(cookie_jar):
|
31 |
+
f = fernet.Fernet(ENCRYPTION_KEY)
|
32 |
+
data = pickle.dumps(cookie_jar._cookies, pickle.HIGHEST_PROTOCOL)
|
33 |
+
compressed_data = gzip.compress(data)
|
34 |
+
return f.encrypt(compressed_data)
|
35 |
+
|
36 |
+
|
37 |
+
def _cookies_load(cookie_jar):
|
38 |
+
f = fernet.Fernet(ENCRYPTION_KEY)
|
39 |
+
compressed_data = f.decrypt(cookie_jar)
|
40 |
+
data = gzip.decompress(compressed_data)
|
41 |
+
cookies = aiohttp.CookieJar()
|
42 |
+
cookies.clear()
|
43 |
+
cookies._cookies = pickle.loads(data)
|
44 |
+
return cookies
|
45 |
+
|
46 |
+
|
47 |
+
class User(db.Model):
|
48 |
+
__tablename__ = 'usersb'
|
49 |
+
id = db.Column(db.BigInteger(), primary_key=True)
|
50 |
+
cookies = db.Column(db.LargeBinary())
|
51 |
+
style = db.Column(db.Integer())
|
52 |
+
chat = db.Column(db.BigInteger())
|
53 |
+
captions = db.Column(db.Boolean())
|
54 |
+
replies = db.Column(db.Boolean())
|
55 |
+
|
56 |
+
|
57 |
+
USERS = {}
|
58 |
+
|
59 |
+
|
60 |
+
async def init(dbstring, encryption_key):
|
61 |
+
global ENCRYPTION_KEY
|
62 |
+
global USERS, ENCRYPTION_KEY
|
63 |
+
ENCRYPTION_KEY = encryption_key or _generate_key()
|
64 |
+
await db.set_bind(dbstring)
|
65 |
+
await db.gino.create_all()
|
66 |
+
|
67 |
+
all_users = await db.all(User.query)
|
68 |
+
USERS = {u.id: {'cookies': _cookies_load(u.cookies) if u.cookies else None,
|
69 |
+
'style': u.style, 'chat': u.chat,
|
70 |
+
'captions': u.captions,
|
71 |
+
'replies': u.replies,
|
72 |
+
'id': u.id} for u in all_users}
|
73 |
+
return USERS
|
74 |
+
|
75 |
+
|
76 |
+
async def get_user(userID=None, chatID=None):
|
77 |
+
user = None
|
78 |
+
if userID:
|
79 |
+
user = USERS[userID] if userID in USERS.keys() else None
|
80 |
+
if chatID and USERS:
|
81 |
+
for k, v in USERS.items():
|
82 |
+
if v['chat'] == chatID:
|
83 |
+
user = v
|
84 |
+
break
|
85 |
+
return user
|
86 |
+
|
87 |
+
|
88 |
+
async def insert_user(userID, cookies=None, chat=None, style=None, keep_cookies=True, captions=True, replies=True):
|
89 |
+
global USERS
|
90 |
+
if userID not in USERS.keys():
|
91 |
+
USERS[userID] = {'cookies': None, 'Style': None,
|
92 |
+
'chat': chat, 'captions': captions, 'replies': replies}
|
93 |
+
USERS[userID]['cookies'] = cookies
|
94 |
+
USERS[userID]['chat'] = chat
|
95 |
+
USERS[userID]['style'] = style
|
96 |
+
USERS[userID]['id'] = userID
|
97 |
+
USERS[userID]['captions'] = captions
|
98 |
+
USERS[userID]['replies'] = replies
|
99 |
+
user = await User.get(userID)
|
100 |
+
if not user:
|
101 |
+
return await User.create(id=userID, cookies=_cookies_save(cookies) if keep_cookies else None, chat=chat, style=style)
|
102 |
+
await user.update(cookies=_cookies_save(USERS[userID]['cookies']) if keep_cookies else None,
|
103 |
+
chat=USERS[userID]['chat'], style=USERS[userID]['style'], captions=USERS[userID]['captions'], replies=USERS[userID]['replies']).apply()
|
104 |
+
return USERS[userID]
|
105 |
+
|
106 |
+
|
107 |
+
async def remove_user(userID):
|
108 |
+
global USERS
|
109 |
+
if userID in USERS.keys():
|
110 |
+
del USERS[userID]
|
111 |
+
user = await User.get(userID)
|
112 |
+
if user:
|
113 |
+
return await User.delete.where(User.id == userID).gino.status()
|
114 |
+
return None
|
115 |
+
|
116 |
+
|
117 |
+
async def retrieve_data(userId):
|
118 |
+
if userId in USERS.keys():
|
119 |
+
result = USERS[userId]
|
120 |
+
if result['cookies']:
|
121 |
+
cookies = [str(cookie).split(None, 1)[-1]
|
122 |
+
for cookie in result['cookies']]
|
123 |
+
result['cookies'] = cookies
|
124 |
+
return json.dumps(USERS[userId])
|
125 |
+
return None
|