Complete backend redesign to using SQLite #4
2 changed files with 31 additions and 25 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
||||||
server/sound/
|
server/sound/
|
||||||
server/songDatabase.json
|
server/songDatabase.json
|
||||||
|
*.db
|
||||||
start.bat
|
start.bat
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
from mutagen.easyid3 import EasyID3
|
from mutagen.easyid3 import EasyID3
|
||||||
from mutagen.mp3 import MP3
|
from mutagen.mp3 import MP3
|
||||||
|
import sqlite3 as sql
|
||||||
import requests, ast, time, math, argparse, json
|
import requests, ast, time, math, argparse, json
|
||||||
|
|
||||||
loading = ["-","\\","|","/"]
|
loading = ["-","\\","|","/"]
|
||||||
|
|
@ -22,32 +23,35 @@ else:
|
||||||
# apikeylastfm = "KeyHere"
|
# apikeylastfm = "KeyHere"
|
||||||
# soundLocation = "directoryHere"
|
# soundLocation = "directoryHere"
|
||||||
songFiles = os.listdir(soundLocation)
|
songFiles = os.listdir(soundLocation)
|
||||||
|
fileOfDB = sql.connect("songDatabase.db")
|
||||||
|
songDatabase = fileOfDB.cursor()
|
||||||
|
# setting song directory
|
||||||
|
songDatabase.execute("CREATE TABLE IF NOT EXISTS meta (id TEXT PRIMARY KEY, data TEXT);")
|
||||||
|
try:
|
||||||
|
songDatabase.execute("INSERT INTO meta (id, data) VALUES (?,?)",("songDirectory",soundLocation))
|
||||||
|
except:
|
||||||
|
songDatabase.execute("UPDATE meta SET data = ? WHERE id = 'songDirectory'", (soundLocation,))
|
||||||
if args.mode.lower() == "update":
|
if args.mode.lower() == "update":
|
||||||
try:
|
#Create if not exists
|
||||||
with open('songDatabase.json', 'r') as handle:
|
songDatabase.execute("CREATE TABLE IF NOT EXISTS songs (filename TEXT PRIMARY KEY, title TEXT, artist TEXT, art TEXT, length INTEGER);")
|
||||||
songDatabaseList = json.load(handle)
|
songDatabase.execute("SELECT filename FROM songs;")
|
||||||
except:
|
dBfilelist = songDatabase.fetchall()
|
||||||
songDatabaseList={"songDirectory":soundLocation,'songData':{}}
|
dBfilelistSet = set()
|
||||||
deleteySongs = []
|
for i in dBfilelist:
|
||||||
for i in songDatabaseList["songData"]:
|
dBfilelistSet.add(i[0])
|
||||||
try:
|
# Delete nonexistant files
|
||||||
if songFiles.index(i) == -1:
|
deleteySongs = list(dBfilelistSet - set(songFiles))
|
||||||
deleteySongs.append(i)
|
songDatabase.executemany("DELETE FROM songs WHERE filename = ?", [(item,) for item in deleteySongs]) # in this line it turns the list of strings into a list of tuples of strings
|
||||||
except:
|
print("Deleted: " + ", ".join(deleteySongs)+ " from database")
|
||||||
deleteySongs.append(i)
|
# only include new files in list to be used
|
||||||
if deleteySongs:
|
songFiles = list(set(songFiles) - dBfilelistSet)
|
||||||
print("deleted: " + ", ".join(deleteySongs)+ " from database")
|
|
||||||
for i in deleteySongs:
|
|
||||||
songDatabaseList["songData"].pop(i)
|
|
||||||
for i in songDatabaseList["songData"]:
|
|
||||||
songFiles.remove(i)
|
|
||||||
# This prints everything in the directory, including non mp3s
|
|
||||||
# theres not agood way to fix this without looping again.
|
|
||||||
print("new songs: " + ", ".join(songFiles))
|
print("new songs: " + ", ".join(songFiles))
|
||||||
elif args.mode.lower()=="new":
|
elif args.mode.lower()=="new":
|
||||||
songDatabaseList={"songDirectory":soundLocation,'songData':{}}
|
songDatabase.execute("DROP TABLE IF EXISTS songs;")
|
||||||
|
songDatabase.execute("CREATE TABLE songs (filename TEXT PRIMARY KEY, title TEXT, artist TEXT, art TEXT, length INTEGER);")
|
||||||
else:
|
else:
|
||||||
raise ValueError("Must be \"new\" or \"update\"")
|
raise ValueError("Must be \"new\" or \"update\"")
|
||||||
|
|
||||||
if args.art.lower() == "true" and not(args.apikey == ""):
|
if args.art.lower() == "true" and not(args.apikey == ""):
|
||||||
x = len(songFiles)*0.25
|
x = len(songFiles)*0.25
|
||||||
if x > 60:
|
if x > 60:
|
||||||
|
|
@ -95,7 +99,8 @@ for i in songFiles:
|
||||||
index = (songFiles.index(i))%4
|
index = (songFiles.index(i))%4
|
||||||
print("\r" + str(loading[index] + str(math.floor((songFiles.index(i)/(len(songFiles)-1))*100))+ "%"), end='', flush=True)
|
print("\r" + str(loading[index] + str(math.floor((songFiles.index(i)/(len(songFiles)-1))*100))+ "%"), end='', flush=True)
|
||||||
# each "song" is stored as a dictionary/JSON entry following the format seen in the readME
|
# each "song" is stored as a dictionary/JSON entry following the format seen in the readME
|
||||||
songDatabaseList["songData"][i] = ({"title":title,"artist":artist,"art":image,"length":length})
|
songDatabase.execute(f"INSERT INTO songs (filename, title, artist, art, length) VALUES (?,?,?,?,?)",(i,title,artist,image,length))
|
||||||
|
|
||||||
with open('songDatabase.json', 'w') as handle:
|
|
||||||
json.dump(songDatabaseList, handle)
|
|
||||||
|
fileOfDB.commit()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue