-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
80 lines (60 loc) · 2.18 KB
/
Copy pathdatabase.py
File metadata and controls
80 lines (60 loc) · 2.18 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
import sqlite3
import config
import random
import base64
import api
from flask import g
from passlib.hash import pbkdf2_sha256
def MakeKey():
return base64.b32encode(random.getrandbits(256).to_bytes(32, "big")).strip(b"=").decode("utf8")
def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(config.db, isolation_level=None)
db.row_factory = make_dicts
return db
def Initialize():
_local = sqlite3.connect(config.db)
_local.row_factory = make_dicts
_local.execute("PRAGMA journal_mode = WAL;")
_local.execute(
"CREATE TABLE IF NOT EXISTS users (`username` STRING, `password` STRING, `title` STRING, `key` STRING, `hidden` INTEGER)")
_local.execute("CREATE TABLE IF NOT EXISTS invites (`token` TEXT)")
def GetAccount(username):
q = get_db().execute("SELECT * FROM `users` WHERE username LIKE ?", (username,))
if not q:
return None
else:
fetch = q.fetchone()
return fetch
def CreateAccount(username, password):
key = MakeKey()
get_db().execute("INSERT INTO `users` VALUES (?, ?, ?, ?, ?)",
(username,
pbkdf2_sha256.hash(password),
f"{username}'s stream",
key,
0))
api.AddPath(username, key)
def GetByKey(key):
query = get_db().execute("SELECT * FROM `users` WHERE `key` = ?", (key, ))
return query.fetchone()
def UpdateAccount(username, title, visibility):
get_db().execute("UPDATE `users` SET `title` = ?, `hidden` = ? WHERE username = ?",
(
title, visibility, username
))
def HasToken(token):
q = get_db().execute("SELECT * FROM invites WHERE token = ?",
(
token,
))
return q.fetchone()
def RemoveToken(token):
get_db().execute("DELETE FROM `invites` WHERE `token` = ?",
(
token,
))