Files
Membarr/app/bot/helper/db.py
Yoruio d2e184144b feat: Adds database entries for Jellyfin
- Adds database columns and expands database commands to acccomodate
 jellyfin

task: none
2022-07-09 15:25:07 -06:00

145 lines
4.2 KiB
Python

import sqlite3
DB_URL = 'app/config/app.db'
def create_connection(db_file):
""" create a database connection to a SQLite database """
conn = None
try:
conn = sqlite3.connect(db_file)
print("Connected to db")
except Error as e:
print("error in connecting to db")
finally:
if conn:
return conn
def checkTableExists(dbcon, tablename):
dbcur = dbcon.cursor()
dbcur.execute("""SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{0}';""".format(tablename.replace('\'', '\'\'')))
if dbcur.fetchone()[0] == 1:
dbcur.close()
return True
dbcur.close()
return False
conn = create_connection(DB_URL)
# Checking if table exists
if checkTableExists(conn, 'clients'):
print('Table exists.')
else:
conn.execute(
'''CREATE TABLE "clients" (
"id" INTEGER NOT NULL UNIQUE,
"discord_username" TEXT NOT NULL UNIQUE,
"email" TEXT,
"jellyfin_username" TEXT,
PRIMARY KEY("id" AUTOINCREMENT)
);''')
def save_user_email(username, email):
if username and email:
conn.execute(f"""
INSERT OR REPLACE INTO clients(discord.username, email)
VALUES('{username}', '{email}')
""")
conn.commit()
print("User added to db.")
else:
return "Username and email cannot be empty"
def save_user(username):
if username:
conn.execute("INSERT INTO clients (discord_username) VALUES ('"+ username +"')")
conn.commit()
print("User added to db.")
else:
return "Username cannot be empty"
def save_user_jellyfin(username, jellyfin_username):
if username and jellyfin_username:
conn.execute(f"""
INSERT OR REPLACE INTO clients(discord_username, jellyfin_username)
VALUES('{username}', '{jellyfin_username}')
""")
conn.commit()
print("User added to db.")
else:
return "Discord and Jellyfin usernames cannot be empty"
def save_user_all(username, email, jellyfin_username):
if username and email and jellyfin_username:
conn.execute(f"""
INSERT OR REPLACE INTO clients(discord_username, email, jellyfin_username)
VALUES('{username}', '{email}', '{jellyfin_username}')
""")
conn.commit()
print("User added to db.")
elif username and email:
save_user_email(username, email)
elif username and jellyfin_username:
save_user_jellyfin(username, jellyfin_username)
elif username:
save_user(username)
else:
return "Discord username must all be provided"
def get_useremail(username):
if username:
try:
cursor = conn.execute('SELECT discord_username, email from clients where discord_username="{}";'.format(username))
for row in cursor:
email = row[1]
if email:
return email
else:
return "No users found"
except:
return "error in fetching from db"
else:
return "username cannot be empty"
def get_jellyfin_username(username):
"""
Get jellyfin username of user based on discord username
param username: discord username
return jellyfin username
"""
if username:
try:
cursor = conn.execute('SELECT discord_username, jellyfin_username from clients where discord_username="{}";'.format(username))
for row in cursor:
jellyfin_username = row[1]
if jellyfin_username:
return jellyfin_username
else:
return "No users found"
except:
return "error in fetching from db"
else:
return "username cannot be empty"
def delete_user(username):
if username:
try:
conn.execute('DELETE from clients where discord_username="{}";'.format(username))
conn.commit()
return True
except:
return False
else:
return "username cannot be empty"
def read_useremail():
cur = conn.cursor()
cur.execute("SELECT * FROM clients")
rows = cur.fetchall()
all = []
for row in rows:
#print(row[1]+' '+row[2])
all.append(row)
return all