DB functions working - slow to load

This commit is contained in:
warwickh
2021-09-11 10:55:43 +10:00
parent f97b9e1de9
commit 54a071a9c7
6 changed files with 208 additions and 34 deletions

7
lib/dbutils/__init__.py Normal file
View File

@ -0,0 +1,7 @@
"""
Databse utilities for plugin.audio.subsonic
"""
from .dbutils import *
__version__ = '0.0.1'

80
lib/dbutils/dbutils.py Normal file
View File

@ -0,0 +1,80 @@
import sqlite3 as sql
tbl_artist_info_ddl = str('CREATE TABLE artist_info ('
'artist_id TEXT NOT NULL PRIMARY KEY,'
'artist_info TEXT,'
'last_update TEXT);')
class SQLiteDatabase(object):
def __init__(self, db_filename):
print("Init %s"%db_filename)
self.db_filename = db_filename
self.conn = None
self.connect()
def connect(self):
try:
#xbmc.log("Trying connection to the database %s"%self.db_filename, xbmc.LOGINFO)
print("Trying connection to the database %s"%self.db_filename)
self.conn = sql.connect(self.db_filename)
cursor = self.conn.cursor()
cursor.execute(str('SELECT SQLITE_VERSION()'))
#xbmc.log("Connection %s was successful %s"%(self.db_filename, cursor.fetchone()[0]), xbmc.LOGINFO)
print("Connection %s was successful %s"%(self.db_filename, cursor.fetchone()[0]))
cursor.row_factory = lambda cursor, row: row[0]
cursor.execute(str('SELECT name FROM sqlite_master WHERE type=\'table\' ''AND name NOT LIKE \'sqlite_%\''))
list_tables = cursor.fetchall()
if not list_tables:
# If no tables exist create a new one
#xbmc.log("Creating Subsonic local DB", xbmc.LOGINFO)
print("Creating Subsonic local DB")
cursor.execute(tbl_artist_info_ddl)
except sql.Error as e:
#xbmc.log("SQLite error %s"%e.args[0], xbmc.LOGINFO)
print("SQLite error %s"%e.args[0])
def get_cursor(self):
return self.conn.cursor()
def run_query(self, query, params=None, cursor=None):
print("Processing query %s params %s"%(str(query),str(params)))
try:
if cursor is None:
cursor = self.get_cursor()
if params is not None:
cursor.execute(query, params)
else:
cursor.execute(query)
return cursor
except sql.Error as e:
print("SQLite error %s"%e.args[0])
except ValueError:
print("Error query %s"%str(query))
print("Error query type %s"%type(query))
print("Error params %s"%str(params))
print("Error params type %s"%type(params))
def update_artist(self, artist_id, artist_info, update_time):
success = False
query = 'INSERT or REPLACE INTO artist_info VALUES (?, ?, ?)'
params = (str(artist_id), str(artist_info), str(update_time))
cursor = self.run_query(query, params)
try:
self.conn.commit()
success = True
except Exception as e:
print("Exception %s"%e)
pass
return success
def get_artist_info(self, artist_id):
query = 'SELECT * FROM artist_info WHERE artist_id = ?'
params = [str(artist_id)]
cursor = self.run_query(query, params)
return cursor.fetchall()
def close(self):
if self.conn:
self.conn.close()

View File

@ -228,11 +228,13 @@ class Connection(object):
"""
methodName = 'ping'
viewName = '%s.view' % methodName
print("test")
req = self._getRequest(viewName)
xbmc.log("Pinging %s"%str(req.full_url),xbmc.LOGDEBUG)
print("Pinging %s"%str(req.full_url))#,#xbmc.logDEBUG)
#xbmc.log("Pinging %s"%str(req.full_url),#xbmc.logDEBUG)
try:
res = self._doInfoReq(req)
print(res)
except Exception as e:
print("Ping failed %s"%e)
return False
@ -896,11 +898,11 @@ class Connection(object):
'converted': converted})
req = self._getRequest(viewName, q)
#xbmc.log("Requesting %s"%str(req.full_url),xbmc.LOGDEBUG)
##xbmc.log("Requesting %s"%str(req.full_url),#xbmc.logDEBUG)
return_url = req.full_url
if self._insecure:
return_url += '|verifypeer=false'
xbmc.log("Request is insecure %s"%return_url,level=xbmc.LOGDEBUG)
#xbmc.log("Request is insecure %s"%return_url,level=#xbmc.logDEBUG)
return return_url
@ -945,11 +947,11 @@ class Connection(object):
q = self._getQueryDict({'id': aid, 'size': size})
req = self._getRequest(viewName, q)
#xbmc.log("Requesting %s"%str(req.full_url),xbmc.LOGDEBUG)
##xbmc.log("Requesting %s"%str(req.full_url),#xbmc.logDEBUG)
return_url = req.full_url
if self._insecure:
return_url += '|verifypeer=false'
xbmc.log("Request is insecure %s"%return_url,level=xbmc.LOGDEBUG)
#xbmc.log("Request is insecure %s"%return_url,level=#xbmc.logDEBUG)
return return_url
@ -1997,7 +1999,7 @@ class Connection(object):
q['musicFolderId'] = musicFolderId
req = self._getRequest(viewName, q)
xbmc.log("Requesting %s"%str(req.full_url),xbmc.LOGDEBUG)
#xbmc.log("Requesting %s"%str(req.full_url),#xbmc.logDEBUG)
res = self._doInfoReq(req)
self._checkStatus(res)
return res
@ -2809,12 +2811,13 @@ class Connection(object):
qdict.update(query)
url = '%s:%d/%s/%s' % (self._baseUrl, self._port, self._serverPath,
viewName)
#xbmc.log("Standard URL %s"%url,level=xbmc.LOGDEBUG)
#xbmc.log("Qdict %s"%str(qdict),level=xbmc.LOGDEBUG)
#xbmc.log("Standard URL %s"%url,level=#xbmc.logDEBUG)
#xbmc.log("Qdict %s"%str(qdict),level=#xbmc.logDEBUG)
req = urllib.request.Request(url, urlencode(qdict).encode('utf-8'))
if(self._useGET or ('getCoverArt' in viewName) or ('stream' in viewName)):
url += '?%s' % urlencode(qdict)
#xbmc.log("UseGET URL %s"%(url),xbmc.LOGDEBUG)
#xbmc.log("UseGET URL %s"%(url), xbmc.logDEBUG)
print(url)
req = urllib.request.Request(url)
return req