Improved settings handling - fulltime service

This commit is contained in:
warwickh
2021-09-28 14:52:43 +10:00
parent cefe9d617f
commit 3a71d3c107
3 changed files with 52 additions and 68 deletions

View File

@ -70,6 +70,8 @@ class SQLiteDatabase(object):
last_update = self.get_value(artist_id, 'last_update') last_update = self.get_value(artist_id, 'last_update')
record_age = round(time.time())-round(float(last_update[0][0])) record_age = round(time.time())-round(float(last_update[0][0]))
return record_age return record_age
except IndexError:
print("No existing record for artist %s" % artist_id)
except Exception as e: except Exception as e:
print("get_record_age failed %s" % e) print("get_record_age failed %s" % e)
return 0 return 0

10
main.py
View File

@ -31,6 +31,10 @@ connection = None
db = None db = None
cachetime = int(Addon().get_setting('cachetime')) cachetime = int(Addon().get_setting('cachetime'))
try:
enhancedInfo = Addon().get_setting('enhanced_info')
except:
enhancedInfo = False
db_filename = "subsonic_sqlite.db" db_filename = "subsonic_sqlite.db"
@ -775,13 +779,9 @@ def get_artist_info(artist_id, forced=False):
print("Retreiving artist info for id: %s"%(artist_id)) print("Retreiving artist info for id: %s"%(artist_id))
#popup("Updating artist info\nplease wait") #popup("Updating artist info\nplease wait")
try: try:
if Addon().get_setting('enhanced_info'): if enhancedInfo:
artist_info = db.get_value(artist_id, 'artist_info')[0][0] artist_info = db.get_value(artist_id, 'artist_info')[0][0]
artist_wiki = db.get_value(artist_id, 'wikipedia_extract')[0][0] artist_wiki = db.get_value(artist_id, 'wikipedia_extract')[0][0]
#plugin.log("Artist info: %s"%artist_info)
#plugin.log("Artist wiki: %s"%artist_wiki)
#plugin.log("Len Artist info: %s"%len(artist_info))
#plugin.log("Len Artist wiki: %s"%len(artist_wiki))
if(len(artist_info)<10): if(len(artist_info)<10):
print("Using wiki data") print("Using wiki data")
artist_info = artist_wiki artist_info = artist_wiki

View File

@ -17,6 +17,11 @@ connection = None
db = None db = None
mb = None mb = None
serviceEnabled = True
refresh_age = 86400 #multiple of random to age info records - needs some validation
check_freq = 300#3600 #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
@ -42,14 +47,12 @@ scrobbled = False
def popup(text, time=5000, image=None): def popup(text, time=5000, image=None):
title = plugin.addon.getAddonInfo('name') title = plugin.addon.getAddonInfo('name')
icon = plugin.addon.getAddonInfo('icon') icon = plugin.addon.getAddonInfo('icon')
xbmc.executebuiltin('Notification(%s, %s, %d, %s)' % (title, text, xbmc.executebuiltin('Notification(%s, %s, %d, %s)' % (title, text, time, icon))
time, icon))
def get_connection(): def get_connection():
global connection global connection
if connection==None: if connection==None:
connected = False connected = False
# Create connection
try: try:
connection = libsonic.Connection( connection = libsonic.Connection(
baseUrl=Addon().get_setting('subsonic_url'), baseUrl=Addon().get_setting('subsonic_url'),
@ -72,13 +75,10 @@ def get_connection():
return connection return connection
def get_mb(): def get_mb():
global mb
mb = musicbrainz.MBConnection() mb = musicbrainz.MBConnection()
return mb return mb
def get_db(): def get_db():
global db
global db_filename
db_path = os.path.join(plugin.profile_dir, db_filename) db_path = os.path.join(plugin.profile_dir, db_filename)
plugin.log("Getting DB %s"%db_path) plugin.log("Getting DB %s"%db_path)
try: try:
@ -87,23 +87,8 @@ def get_db():
plugin.log("Connecting to DB failed: %s"%e) plugin.log("Connecting to DB failed: %s"%e)
return db return db
def check_artist_info():
db = get_db()
connection = get_connection()
response = connection.getArtists()
for index in response["artists"]["index"]:
for artist in index["artist"]:
artist_id = artist['id']
artist_name = db.get_value(artist_id, 'artist_name')
artist_info = db.get_value(artist_id, 'artist_info')
artist_wiki = db.get_value(artist_id, 'wikipedia_extract')
plugin.log("Name %s"%(artist_name))
plugin.log("LastFM %s"%(artist_info))
plugin.log("Wiki %s"%(artist_wiki))
def refresh_artist(artist_id): def refresh_artist(artist_id):
db = get_db() db = get_db()
mb = get_mb()
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']
@ -115,44 +100,43 @@ def refresh_artist(artist_id):
plugin.log("subbed: %s"%artist_info) plugin.log("subbed: %s"%artist_info)
except: except:
artist_info = "" artist_info = ""
mb_artist_id = mb.get_artist_id(artist_name) if enhancedInfo:
artist_image_url = mb.get_artist_image(mb_artist_id) mb = get_mb()
wikipedia_url = mb.get_artist_wikpedia(mb_artist_id) mb_artist_id = mb.get_artist_id(artist_name)
artist_wiki_extract = mb.get_wiki_extract(wikipedia_url) artist_image_url = mb.get_artist_image(mb_artist_id)
wikipedia_image = mb.get_wiki_image(wikipedia_url) wikipedia_url = mb.get_artist_wikpedia(mb_artist_id)
db.update_value(artist_id, 'artist_name', artist_name) artist_wiki_extract = mb.get_wiki_extract(wikipedia_url)
db.update_value(artist_id, 'artist_info', artist_info) wikipedia_image = mb.get_wiki_image(wikipedia_url)
db.update_value(artist_id, 'mb_artist_id', mb_artist_id) db.update_value(artist_id, 'artist_name', artist_name)
db.update_value(artist_id, 'image_url', artist_image_url) db.update_value(artist_id, 'artist_info', artist_info)
db.update_value(artist_id, 'wikipedia_url', wikipedia_url) db.update_value(artist_id, 'mb_artist_id', mb_artist_id)
db.update_value(artist_id, 'wikipedia_extract', artist_wiki_extract) db.update_value(artist_id, 'image_url', artist_image_url)
db.update_value(artist_id, 'wikipedia_image', wikipedia_image) db.update_value(artist_id, 'wikipedia_url', wikipedia_url)
db.update_value(artist_id, 'wikipedia_extract', artist_wiki_extract)
db.update_value(artist_id, 'wikipedia_image', wikipedia_image)
def check_db_status(forced=False): def check_db_status(forced=False):
global last_db_check global last_db_check
refresh_age = 86400
check_freq = 300#3600
refresh_single_flag = False refresh_single_flag = False
if(enhancedInfo): try:
try: if(time.time()-check_freq > last_db_check) or forced:
if(time.time()-check_freq > last_db_check) or forced: #popup("DB Check Starting")
#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"]:
for index in response["artists"]["index"]: for artist in index["artist"]:
for artist in index["artist"]: artist_id = artist['id']
artist_id = artist['id'] record_age = db.get_record_age(artist_id)
record_age = db.get_record_age(artist_id) if(forced or not record_age or (record_age > (random.randint(1,111)*refresh_age))) and not refresh_single_flag:
if(forced or not record_age or (record_age > (random.randint(1,111)*refresh_age))) and not refresh_single_flag: #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)
#popup("Refreshing %s" % artist_id) refresh_artist(artist_id)
refresh_artist(artist_id) if(record_age>0):refresh_single_flag = True
if(record_age>0):refresh_single_flag = True last_db_check = time.time()
last_db_check = time.time() except Exception as e:
except Exception as e: plugin.log("DB check failed %s"%e)
plugin.log("DB rcheck failed %s"%e)
return return
@ -179,12 +163,9 @@ def check_player_status():
except Exception as e: except Exception as e:
xbmc.log("Subsonic scrobble check failed %e"%e, xbmc.LOGINFO) xbmc.log("Subsonic scrobble check failed %e"%e, xbmc.LOGINFO)
return return
#pass
#xbmc.log("Playing stopped", xbmc.LOGINFO)
def scrobble_track(track_id): def scrobble_track(track_id):
connection = get_connection() connection = get_connection()
if connection==False: if connection==False:
return False return False
res = connection.scrobble(track_id) res = connection.scrobble(track_id)
@ -196,8 +177,9 @@ def scrobble_track(track_id):
popup("Scrobble failed") popup("Scrobble failed")
return False return False
if __name__ == '__main__': if __name__ == '__main__':
if(scrobbleEnabled or enhancedInfo): if serviceEnabled:
monitor = xbmc.Monitor() monitor = xbmc.Monitor()
xbmc.log("Subsonic service started", xbmc.LOGINFO) xbmc.log("Subsonic service started", xbmc.LOGINFO)
popup("Subsonic service started") popup("Subsonic service started")
@ -207,4 +189,4 @@ if __name__ == '__main__':
check_player_status() check_player_status()
check_db_status() check_db_status()
else: else:
plugin.log("Subsonic service not started due to settings") plugin.log("Subsonic service not enabled")