-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddbb.py
56 lines (45 loc) · 1.61 KB
/
ddbb.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
import sqlite3
from threading import Lock
import time
import os
from base64 import b64encode
import json
settings = "/etc/friendspot/settings.json"
if not os.path.isfile(settings):
settings = "settings.json"
with open(settings) as f:
settings = json.loads(f.read())
client_id = settings.get('client_id')
auth = client_id + ":" + settings.get('client_secret')
auth = b64encode(auth.encode('utf-8')).decode('utf-8')
redirect_uri = settings.get('redirect_uri')
db = "/etc/friendspot/friendspot.db"
if not os.path.isfile(db):
db = "friendspot.db"
db = sqlite3.connect(db, check_same_thread=False)
ldb = Lock()
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS user (id integer PRIMARY KEY NOT NULL, username text NOT NULL, name text NOT NULL, url text NOT NULL, img text NOT NULL, session text NOT NULL, access text NOT NULL, refresh text NOT NULL, valid int NOT NULL);")
cursor.execute("CREATE TABLE IF NOT EXISTS friends (user integer NOT NULL, friend integer NOT NULL, FOREIGN KEY (user) REFERENCES user(id), FOREIGN KEY (friend) REFERENCES user(id));")
db.commit()
def query(sql, *param):
with ldb:
cursor = db.cursor()
cursor.execute(sql, param)
result = cursor.fetchall()
db.commit()
return result
def queryone(sql, *param):
with ldb:
cursor = db.cursor()
cursor.execute(sql, param)
result = cursor.fetchone()
db.commit()
return result
def insert(sql, *param):
with ldb:
cursor = db.cursor()
cursor.execute(sql, param)
id = cursor.lastrowid
db.commit()
return id