Refresh info by alpha index to avoid blocking

This commit is contained in:
warwickh
2021-10-04 12:46:10 +11:00
parent 607bb52158
commit f587d3d4d6
4 changed files with 50 additions and 35 deletions

View File

@ -1,5 +1,6 @@
import sqlite3 as sql import sqlite3 as sql
import time import time
import xbmc
tbl_artist_info_ddl = str('CREATE TABLE artist_info (' tbl_artist_info_ddl = str('CREATE TABLE artist_info ('
'artist_id TEXT NOT NULL PRIMARY KEY,' 'artist_id TEXT NOT NULL PRIMARY KEY,'
@ -23,24 +24,24 @@ class SQLiteDatabase(object):
def connect(self): def connect(self):
try: try:
#xbmc.log("Trying connection to the database %s"%self.db_filename, xbmc.LOGINFO) xbmc.log("Trying connection to the database %s"%self.db_filename, xbmc.LOGINFO)
print("Trying connection to the database %s"%self.db_filename) #print("Trying connection to the database %s"%self.db_filename)
self.conn = sql.connect(self.db_filename) self.conn = sql.connect(self.db_filename)
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute(str('SELECT SQLITE_VERSION()')) cursor.execute(str('SELECT SQLITE_VERSION()'))
#xbmc.log("Connection %s was successful %s"%(self.db_filename, cursor.fetchone()[0]), xbmc.LOGINFO) 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])) #print("Connection %s was successful %s"%(self.db_filename, cursor.fetchone()[0]))
cursor.row_factory = lambda cursor, row: row[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_%\'')) cursor.execute(str('SELECT name FROM sqlite_master WHERE type=\'table\' ''AND name NOT LIKE \'sqlite_%\''))
list_tables = cursor.fetchall() list_tables = cursor.fetchall()
if not list_tables: if not list_tables:
# If no tables exist create a new one # If no tables exist create a new one
#xbmc.log("Creating Subsonic local DB", xbmc.LOGINFO) xbmc.log("Creating Subsonic local DB", xbmc.LOGINFO)
print("Creating Subsonic local DB") #print("Creating Subsonic local DB")
cursor.execute(tbl_artist_info_ddl) cursor.execute(tbl_artist_info_ddl)
except sql.Error as e: except sql.Error as e:
#xbmc.log("SQLite error %s"%e.args[0], xbmc.LOGINFO) xbmc.log("SQLite error %s"%e.args[0], xbmc.LOGINFO)
print("SQLite error %s"%e.args[0]) #print("SQLite error %s"%e.args[0])
def get_cursor(self): def get_cursor(self):
return self.conn.cursor() return self.conn.cursor()

View File

@ -2816,7 +2816,7 @@ class Connection(object):
req = urllib.request.Request(url, urlencode(qdict).encode('utf-8')) req = urllib.request.Request(url, urlencode(qdict).encode('utf-8'))
if(self._useGET or ('getCoverArt' in viewName) or ('stream' in viewName)): if(self._useGET or ('getCoverArt' in viewName) or ('stream' in viewName)):
url += '?%s' % urlencode(qdict) url += '?%s' % urlencode(qdict)
#xbmc.log("UseGET URL %s"%(url), xbmc.LOGDEBUG) xbmc.log("UseGET URL %s"%(url), xbmc.LOGDEBUG)
#print(url) #print(url)
req = urllib.request.Request(url) req = urllib.request.Request(url)
return req return req

20
main.py
View File

@ -63,6 +63,7 @@ def get_connection():
insecure=Addon().get_setting('insecure'), insecure=Addon().get_setting('insecure'),
legacyAuth=Addon().get_setting('legacyauth'), legacyAuth=Addon().get_setting('legacyauth'),
useGET=Addon().get_setting('useget'), useGET=Addon().get_setting('useget'),
appName="Kodi-Subsonic",
) )
connected = connection.ping() connected = connection.ping()
except Exception as e: except Exception as e:
@ -764,13 +765,16 @@ def get_image(item):
if Addon().get_setting('enhanced_info'): if Addon().get_setting('enhanced_info'):
image = db.get_value(item.get('id'), 'wikipedia_image')[0][0] image = db.get_value(item.get('id'), 'wikipedia_image')[0][0]
#print("Checking image type %s %s %s"%(item.get('id'), image, type(image))) #print("Checking image type %s %s %s"%(item.get('id'), image, type(image)))
if (image is None) or (image =='') or (image =='None'): except IndexError:
connection = get_connection() print("Wiki image not available for artist %s" % item.get('name'))
#print("Using coverart tag from item %s is it same as %s ?"%(item.get('coverArt'),item.get('id'))) except Exception as e:
image = connection.getCoverArtUrl(item.get('coverArt')) print("Error getting image for %s %s"%(item.get('name'),e))
return image
except:
return image return image
if (image is None) or (image =='') or (image =='None'):
connection = get_connection()
#print("Using coverart tag from item %s is it same as %s ?"%(item.get('coverArt'),item.get('id')))
image = connection.getCoverArtUrl(item.get('coverArt'))
return image
def get_artist_info(artist_id, forced=False): def get_artist_info(artist_id, forced=False):
db = get_db() db = get_db()
@ -789,8 +793,10 @@ def get_artist_info(artist_id, forced=False):
if(artist_info is None): if(artist_info is None):
print("artist_info is None making empty string") print("artist_info is None making empty string")
artist_info = "" artist_info = ""
except IndexError:
print("Enhanced info not available for artist %s" % artist_id)
except Exception as e: except Exception as e:
print("Error get info from DB %s"%e) print("Error getting artist info from DB %s"%e)
return artist_info return artist_info
def get_entry_artist(item,params): def get_entry_artist(item,params):

View File

@ -19,12 +19,13 @@ mb = None
serviceEnabled = True serviceEnabled = True
refresh_age = 86400 #multiple of random to age info records - needs some validation refresh_age = 3600 #multiple of random to age info records - needs some validation
check_freq = 300#3600 #How often to run a refresh cycle - needs some validation check_freq = 30 #How often to run a refresh cycle - needs some validation
db_filename = "subsonic_sqlite.db" db_filename = "subsonic_sqlite.db"
last_db_check = 0 last_db_check = 0
current_artist_index = 0
from simpleplugin import Plugin from simpleplugin import Plugin
from simpleplugin import Addon from simpleplugin import Addon
@ -73,6 +74,7 @@ def get_connection():
insecure=Addon().get_setting('insecure'), insecure=Addon().get_setting('insecure'),
legacyAuth=Addon().get_setting('legacyauth'), legacyAuth=Addon().get_setting('legacyauth'),
useGET=Addon().get_setting('useget'), useGET=Addon().get_setting('useget'),
appName="Kodi-Subsonic",
) )
connected = connection.ping() connected = connection.ping()
except: except:
@ -104,6 +106,7 @@ def refresh_artist(artist_id):
connection = get_connection() connection = get_connection()
artist = connection.getArtist(artist_id)#['subsonic-response'] artist = connection.getArtist(artist_id)#['subsonic-response']
artist_name = artist['artist']['name'] artist_name = artist['artist']['name']
db.update_value(artist_id, 'artist_name', artist_name)
artist_info = connection.getArtistInfo2(artist_id) artist_info = connection.getArtistInfo2(artist_id)
try: try:
artist_info = artist_info['artistInfo2']['biography'] artist_info = artist_info['artistInfo2']['biography']
@ -112,14 +115,13 @@ def refresh_artist(artist_id):
plugin.log("subbed: %s"%artist_info) plugin.log("subbed: %s"%artist_info)
except: except:
artist_info = "" artist_info = ""
if enhancedInfo: if(enhancedInfo):
mb = get_mb() mb = get_mb()
mb_artist_id = mb.get_artist_id(artist_name) mb_artist_id = mb.get_artist_id(artist_name)
artist_image_url = mb.get_artist_image(mb_artist_id) artist_image_url = mb.get_artist_image(mb_artist_id)
wikipedia_url = mb.get_artist_wikpedia(mb_artist_id) wikipedia_url = mb.get_artist_wikpedia(mb_artist_id)
artist_wiki_extract = mb.get_wiki_extract(wikipedia_url) artist_wiki_extract = mb.get_wiki_extract(wikipedia_url)
wikipedia_image = mb.get_wiki_image(wikipedia_url) wikipedia_image = mb.get_wiki_image(wikipedia_url)
db.update_value(artist_id, 'artist_name', artist_name)
db.update_value(artist_id, 'artist_info', artist_info) db.update_value(artist_id, 'artist_info', artist_info)
db.update_value(artist_id, 'mb_artist_id', mb_artist_id) db.update_value(artist_id, 'mb_artist_id', mb_artist_id)
db.update_value(artist_id, 'image_url', artist_image_url) db.update_value(artist_id, 'image_url', artist_image_url)
@ -127,32 +129,37 @@ def refresh_artist(artist_id):
db.update_value(artist_id, 'wikipedia_extract', artist_wiki_extract) db.update_value(artist_id, 'wikipedia_extract', artist_wiki_extract)
db.update_value(artist_id, 'wikipedia_image', wikipedia_image) db.update_value(artist_id, 'wikipedia_image', wikipedia_image)
def check_db_status(forced=False): def check_db_status():
global last_db_check global last_db_check
refresh_single_flag = False global current_artist_index
try: try:
if(time.time()-check_freq > last_db_check) or forced: if(time.time()-check_freq > last_db_check): #Won't check on every run uses check_freq
#popup("DB Check Starting")
plugin.log("DB check starting %s %s" % (time.time(), last_db_check)) plugin.log("DB check starting %s %s" % (time.time(), last_db_check))
db = get_db() db = get_db()
connection = get_connection() connection = get_connection()
response = connection.getArtists() response = connection.getArtists()
for index in response["artists"]["index"]: current_index_content = response["artists"]["index"][current_artist_index] #Completes refresh for alpha index
for artist in index["artist"]: plugin.log("Starting info load for index %s"%current_index_content['name'])
artist_id = artist['id'] for artist in current_index_content["artist"]:
record_age = db.get_record_age(artist_id) artist_id = artist['id']
if(forced or not record_age or (record_age > (random.randint(1,111)*refresh_age))) and not refresh_single_flag: record_age = db.get_record_age(artist_id)
#plugin.log("Record age %s vs %s for %s"%(record_age, (random.randint(1,111)*refresh_age), artist_id)) plugin.log("Record age %s vs %s for %s"%(record_age, (random.randint(1,111)*refresh_age), artist_id))
#popup("Refreshing %s" % artist_id) if(not record_age or (record_age > (random.randint(1,111)*refresh_age))):
refresh_artist(artist_id) plugin.log("Record age %s vs %s for %s"%(record_age, (random.randint(1,111)*refresh_age), artist_id))
if(record_age>0):refresh_single_flag = True plugin.log("Refreshing %s" % (artist_id))
refresh_artist(artist_id)
plugin.log("Refresh complete for %s" % (artist_id))
plugin.log("Finished info loading for index %s"%current_index_content['name'])
current_artist_index+=1
if(current_artist_index>=len(response["artists"]["index"])):current_artist_index=0
last_db_check = time.time() last_db_check = time.time()
except Exception as e: except Exception as e:
plugin.log("DB status check failed %s"%e) plugin.log("Refresh check failed %s"%e)
return return
def check_player_status(): def check_player_status():
global scrobbled
if (scrobbleEnabled and xbmc.getCondVisibility("Player.HasMedia")): if (scrobbleEnabled and xbmc.getCondVisibility("Player.HasMedia")):
try: try:
currentFileName = xbmc.getInfoLabel("Player.Filenameandpath") currentFileName = xbmc.getInfoLabel("Player.Filenameandpath")
@ -187,6 +194,7 @@ def scrobble_track(track_id):
return True return True
else: else:
popup("Scrobble failed") popup("Scrobble failed")
xbmc.log("Scrobble failed", xbmc.LOGERROR)
return False return False