forked from IDN-C-X/IDN-GDrive-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gDriveDB.py
50 lines (35 loc) · 1.24 KB
/
gDriveDB.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
import pickle
import threading
from sqlalchemy import Column, Integer, String, LargeBinary
from sqlalchemy.sql.sqltypes import BigInteger
from gdrive.helpers.sql_helper import BASE, SESSION
class gDriveCreds(BASE):
__tablename__ = "gDrive"
chat_id = Column(BigInteger, primary_key=True)
credential_string = Column(LargeBinary)
def __init__(self, chat_id):
self.chat_id = chat_id
gDriveCreds.__table__.create(checkfirst=True)
INSERTION_LOCK = threading.RLock()
def _set(chat_id, credential_string):
with INSERTION_LOCK:
saved_cred = SESSION.query(gDriveCreds).get(chat_id)
if not saved_cred:
saved_cred = gDriveCreds(chat_id)
saved_cred.credential_string = pickle.dumps(credential_string)
SESSION.add(saved_cred)
SESSION.commit()
def search(chat_id):
with INSERTION_LOCK:
saved_cred = SESSION.query(gDriveCreds).get(chat_id)
return (
pickle.loads(saved_cred.credential_string)
if saved_cred is not None
else None
)
def _clear(chat_id):
with INSERTION_LOCK:
saved_cred = SESSION.query(gDriveCreds).get(chat_id)
if saved_cred:
SESSION.delete(saved_cred)
SESSION.commit()