-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.py
51 lines (45 loc) · 1.26 KB
/
connection.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
import MySQLdb
import base64
import orjson as json
import os
from dotenv import load_dotenv
from walrus import Walrus
BASEDIR = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(BASEDIR, '.env'))
def redis_conn():
db = Walrus(
host=os.getenv("REDIS_HOST", "localhost"),
port=os.getenv("REDIS_PORT", 6379),
db=os.getenv("REDIS_DATABASE", 0),
)
return db
def redis_cache():
db = redis_conn()
return db
def mysql_conn():
conn = MySQLdb.connect(
host=os.getenv("MYSQL_HOST"),
port=int(os.getenv("MYSQL_PORT", 3306)),
user=os.getenv("MYSQL_USER"),
passwd=os.getenv("MYSQL_PASSWORD"),
db=os.getenv("MYSQL_DATABASE"),
charset="utf8"
)
conn.autocommit(True)
return conn
def mysql_query(sql, nick):
cursor = mysql_conn().cursor()
q = "%s%%" % nick
cursor.execute(sql, [q, ])
row = cursor.fetchall()
for myresult in row:
return myresult
def mysql_json(sql, nick):
cursor = mysql_conn().cursor()
q = "%s%%" % nick
cursor.execute(sql, [q, ])
row = cursor.fetchall()
for myresult in row:
json_data = base64.urlsafe_b64decode(myresult[0])
json_object = json.loads(json_data)
return json_object