From 2bfe3773783f538560e1edc7920128949e7d60a1 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Wed, 22 Jul 2015 08:16:08 -0500 Subject: [PATCH] Clean up Also fix settings not saving if reset was initiated. --- resources/lib/API.py | 6 + resources/lib/ClientInformation.py | 47 +- resources/lib/ConnectionManager.py | 84 ++-- resources/lib/Utils.py | 42 +- resources/lib/WriteKodiMusicDB.py | 723 +++++++++++++---------------- resources/settings.xml | 5 +- service.py | 47 +- 7 files changed, 414 insertions(+), 540 deletions(-) diff --git a/resources/lib/API.py b/resources/lib/API.py index 4b12d9a3..8e232d1a 100644 --- a/resources/lib/API.py +++ b/resources/lib/API.py @@ -317,6 +317,12 @@ class API(): provider = item['ProviderIds']['Imdb'] elif "tvdb" in providername: provider = item['ProviderIds']['Tvdb'] + elif "musicBrainzArtist" in providername: + provider = item['ProviderIds']['MusicBrainzArtist'] + elif "musicBrainzAlbum" in providername: + provider = item['ProviderIds']['MusicBrainzAlbum'] + elif "musicBrainzTrackId" in providername: + provider = item['ProviderIds']['MusicBrainzTrackId'] except: pass return provider diff --git a/resources/lib/ClientInformation.py b/resources/lib/ClientInformation.py index 2625c12e..9d2bceac 100644 --- a/resources/lib/ClientInformation.py +++ b/resources/lib/ClientInformation.py @@ -1,27 +1,27 @@ -import xbmc -import xbmcaddon -import xbmcgui +# -*- coding: utf-8 -*- import os from uuid import uuid4 as uuid4 from Lock import Lock +import xbmc +import xbmcaddon +import xbmcgui + import Utils as utils class ClientInformation(): - def __init__(self): - addonId = self.getAddonId() - self.addon = xbmcaddon.Addon(id=addonId) - self.className = self.__class__.__name__ + self.addon = xbmcaddon.Addon() self.addonName = self.getAddonName() def logMsg(self, msg, lvl=1): - utils.logMsg("%s %s" % (self.addonName, self.className), str(msg), int(lvl)) + className = self.__class__.__name__ + utils.logMsg("%s %s" % (self.addonName, className), msg, int(lvl)) def getAddonId(self): # To use when declaring xbmcaddon.Addon(id=addonId) @@ -37,9 +37,15 @@ class ClientInformation(): def getDeviceName(self): - deviceName = self.addon.getSetting('deviceName') - deviceName = deviceName.replace("\"", "_") - deviceName = deviceName.replace("/", "_") + addon = self.addon + + if addon.getSetting('deviceNameOpt') == "false": + # Use Kodi's deviceName + deviceName = xbmc.getInfoLabel('System.FriendlyName') + else: + deviceName = addon.getSetting('deviceName') + deviceName = deviceName.replace("\"", "_") + deviceName = deviceName.replace("/", "_") return deviceName @@ -48,26 +54,26 @@ class ClientInformation(): WINDOW = xbmcgui.Window(10000) clientId = WINDOW.getProperty("client_id") - if (clientId != None and clientId != ""): + if clientId: return clientId # we need to load and or generate a client machine id - __addon__ = self.addon - __addondir__ = xbmc.translatePath( __addon__.getAddonInfo('path')) - machine_guid_lock_path = os.path.join(__addondir__, "machine_guid.lock") - machine_guid_path = os.path.join(__addondir__, "machine_guid") + addon = self.addon + addondir = addon.getAddonInfo('path').decode('utf-8') + machine_guid_lock_path = xbmc.translatePath(os.path.join(addondir, "machine_guid.lock")).decode('utf-8') + machine_guid_path = xbmc.translatePath(os.path.join(addondir, "machine_guid")).decode('utf-8') clientId = "" try: lock = Lock(machine_guid_lock_path) locked = lock.acquire() - if (locked == True): + if locked: fd = os.open(machine_guid_path, os.O_CREAT|os.O_RDWR) clientId = os.read(fd, 256) - if (len(clientId) == 0): + if len(clientId) == 0: uuid = uuid4() clientId = str("%012X" % uuid) self.logMsg("ClientId saved to FILE: %s" % clientId, 2) @@ -77,8 +83,7 @@ class ClientInformation(): os.close(fd) self.logMsg("ClientId saved to WINDOW: %s" % clientId, 1) - WINDOW.setProperty("client_id", clientId) - + WINDOW.setProperty("client_id", clientId) finally: lock.release() @@ -99,4 +104,4 @@ class ClientInformation(): elif xbmc.getCondVisibility('system.platform.android'): return "Linux/Android" - return "Unknown" + return "Unknown" \ No newline at end of file diff --git a/resources/lib/ConnectionManager.py b/resources/lib/ConnectionManager.py index 0d8bb036..a2befbdb 100644 --- a/resources/lib/ConnectionManager.py +++ b/resources/lib/ConnectionManager.py @@ -1,45 +1,41 @@ +# -*- coding: utf-8 -*- + ################################################################################################# # connection manager class ################################################################################################# +import json +import socket + import xbmc import xbmcgui import xbmcaddon -import json -import urllib -import sys -import socket -import threading -from datetime import datetime - import Utils as utils +from ClientInformation import ClientInformation from DownloadUtils import DownloadUtils from UserClient import UserClient -from ClientInformation import ClientInformation class ConnectionManager(): clientInfo = ClientInformation() - uc = UserClient() + user = UserClient() doUtils = DownloadUtils() addonName = clientInfo.getAddonName() addonId = clientInfo.getAddonId() - addon = xbmcaddon.Addon(id=addonId) + addon = xbmcaddon.Addon() WINDOW = xbmcgui.Window(10000) - logLevel = 0 - def __init__(self): - self.className = self.__class__.__name__ self.__language__ = self.addon.getLocalizedString def logMsg(self, msg, lvl=1): - utils.logMsg("%s %s" % (self.addonName, self.className), msg, int(lvl)) + className = self.__class__.__name__ + utils.logMsg("%s %s" % (self.addonName, className), msg, int(lvl)) def checkServer(self): @@ -47,27 +43,27 @@ class ConnectionManager(): self.logMsg("Connection Manager Called", 2) addon = self.addon - server = self.uc.getServer() + server = self.user.getServer() - if (server != ""): + if server != "": self.logMsg("Server already set", 2) return serverInfo = self.getServerDetails() - if (serverInfo == None): + try: + prefix,ip,port = serverInfo.split(":") + setServer = xbmcgui.Dialog().yesno(self.__language__(30167), "Proceed with the following server?", self.__language__(30169) + serverInfo) + except: # serverInfo is None self.logMsg("getServerDetails failed", 1) xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId) return - - prefix,ip,port = serverInfo.split(":") - setServer = xbmcgui.Dialog().yesno(self.__language__(30167), "Proceed with the following server?", self.__language__(30169) + serverInfo) - if (setServer == 1): + if setServer == 1: self.logMsg("Server selected. Saving information.", 1) addon.setSetting("ipaddress", ip.replace("/", "")) addon.setSetting("port", port) - # If https is enabled + # If https, enable the setting if (prefix == 'https'): addon.setSetting('https', "true") else: @@ -77,51 +73,37 @@ class ConnectionManager(): # Get List of public users self.logMsg("Getting user list", 1) - server = ip.replace("/", "") + ":" + port + server = "%s:%s" % (ip.replace("/", ""), port) url = "%s/mediabrowser/Users/Public?format=json" % serverInfo - try: - result = self.doUtils.downloadUrl(url, authenticate=False) - except Exception, msg: - error = "Unable to connect to %s: %s" % (server, msg) - self.logMsg(error, 1) - return "" - - if (result == ""): + result = self.doUtils.downloadUrl(url, authenticate=False) + if result == "": + self.logMsg("Unable to connect to %s." % server, 1) return - - self.logMsg("jsonData: %s" % result, 2) + self.logMsg("Result: %s" % result, 2) + + # Process the list returned names = [] userList = [] for user in result: - name = user[u'Name'] + name = user['Name'] userList.append(name) - if(user[u'HasPassword'] == True): - name = name + " (Secure)" + if user['HasPassword']: + name = "%s (Secure)" % name names.append(name) - - self.logMsg("User List: %s" % names, 1) - self.logMsg("User List: %s" % userList, 2) - return_value = xbmcgui.Dialog().select(self.__language__(30200), names) - - if (return_value > -1): - selected_user = userList[return_value] + + self.logMsg("User list: %s" % names, 1) + resp = xbmcgui.Dialog().select(self.__language__(30200), names) + if resp > -1: + selected_user = userList[resp] self.logMsg("Selected User: %s" % selected_user, 1) self.addon.setSetting("username", selected_user) else: self.logMsg("No user selected.", 1) xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId) return - - # Option to play from http - #setPlayback = xbmcgui.Dialog().yesno("Playback option", "Play your files using HTTP?") - #if setPlayback == 1: - #self.logMsg("Playback will be set using HTTP.", 1) - #addon.setSetting("playFromStream", "true") - #else: - #self.logMsg("Playback will be set using SMB.", 1) def getServerDetails(self): diff --git a/resources/lib/Utils.py b/resources/lib/Utils.py index eec80d54..975d73f8 100644 --- a/resources/lib/Utils.py +++ b/resources/lib/Utils.py @@ -21,7 +21,7 @@ from API import API from PlayUtils import PlayUtils from DownloadUtils import DownloadUtils downloadUtils = DownloadUtils() -addonSettings = xbmcaddon.Addon(id='plugin.video.emby') +addonSettings = xbmcaddon.Addon() language = addonSettings.getLocalizedString @@ -102,31 +102,7 @@ def getKodiMusicDBPath(): def prettifyXml(elem): rough_string = etree.tostring(elem, "utf-8") reparsed = minidom.parseString(rough_string) - return reparsed.toprettyxml(indent="\t") - -def get_params( paramstring ): - xbmc.log("Parameter string: " + paramstring) - param={} - if len(paramstring)>=2: - params=paramstring - - if params[0] == "?": - cleanedparams=params[1:] - else: - cleanedparams=params - - if (params[len(params)-1]=='/'): - params=params[0:len(params)-2] - - pairsofparams=cleanedparams.split('&') - for i in range(len(pairsofparams)): - splitparams={} - splitparams=pairsofparams[i].split('=') - if (len(splitparams))==2: - param[splitparams[0]]=splitparams[1] - elif (len(splitparams))==3: - param[splitparams[0]]=splitparams[1]+"="+splitparams[2] - return param + return reparsed.toprettyxml(indent="\t") def startProfiling(): pr = cProfile.Profile() @@ -221,6 +197,7 @@ def normalize_string(text): def reset(): + WINDOW = xbmcgui.Window( 10000 ) return_value = xbmcgui.Dialog().yesno("Warning", "Are you sure you want to reset your local Kodi database?") if return_value == 0: @@ -240,13 +217,10 @@ def reset(): # Ask if user information should be deleted too. return_user = xbmcgui.Dialog().yesno("Warning", "Reset all Emby Addon settings?") - - delete_settings = False if return_user == 1: - delete_settings = True + WINDOW.setProperty('deletesettings', "true") # first stop any db sync - WINDOW = xbmcgui.Window( 10000 ) WINDOW.setProperty("SyncDatabaseShouldStop", "true") count = 0 @@ -290,12 +264,6 @@ def reset(): # reset the install run flag WINDOW.setProperty("SyncInstallRunDone", "false") - if (delete_settings == True): - addondir = xbmc.translatePath(addonSettings.getAddonInfo('profile')) - dataPath = os.path.join(addondir + "settings.xml") - xbmcvfs.delete(dataPath) - xbmc.log("Deleting : settings.xml") - dialog = xbmcgui.Dialog() dialog.ok('Emby Reset', 'Database reset has completed, Kodi will now restart to apply the changes.') - xbmc.executebuiltin("RestartApp") + xbmc.executebuiltin("RestartApp") \ No newline at end of file diff --git a/resources/lib/WriteKodiMusicDB.py b/resources/lib/WriteKodiMusicDB.py index 5d242b33..f0ce882e 100644 --- a/resources/lib/WriteKodiMusicDB.py +++ b/resources/lib/WriteKodiMusicDB.py @@ -1,149 +1,117 @@ +# -*- coding: utf-8 -*- + ################################################################################################# # WriteKodiMusicDB ################################################################################################# +import sqlite3 +from datetime import datetime +from os.path import basename import xbmc import xbmcgui import xbmcaddon -import xbmcvfs -import json -import urllib -import sqlite3 -import os -from decimal import Decimal -from datetime import datetime, timedelta -from DownloadUtils import DownloadUtils +from ClientInformation import ClientInformation +import Utils as utils +from API import API from PlayUtils import PlayUtils from ReadKodiDB import ReadKodiDB from ReadEmbyDB import ReadEmbyDB from TextureCache import TextureCache -from API import API -import Utils as utils - -from xml.etree.ElementTree import Element, SubElement, Comment, tostring -from xml.etree import ElementTree -from xml.dom import minidom -import xml.etree.cElementTree as ET - -addon = xbmcaddon.Addon(id='plugin.video.emby') -addondir = xbmc.translatePath(addon.getAddonInfo('profile')) class WriteKodiMusicDB(): textureCache = TextureCache() + kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2]) + + addon = xbmcaddon.Addon() + addonName = ClientInformation().getAddonName() + WINDOW = xbmcgui.Window(10000) - def updatePlayCountFromKodi(self, id, type, playcount=0): - #when user marks item watched from kodi interface update this in Emby - - utils.logMsg("Emby", "updatePlayCountFromKodi Called") - connection = utils.KodiSQL() - cursor = connection.cursor() - cursor.execute("SELECT emby_id FROM emby WHERE media_type=? AND kodi_id=?",(type,id)) - - emby_id = cursor.fetchone()[0] - cursor.close + username = WINDOW.getProperty('currUser') + userid = WINDOW.getProperty('userId%s' % username) + server = WINDOW.getProperty('server%s' % username) - if(emby_id != None): - addon = xbmcaddon.Addon(id='plugin.video.emby') - downloadUtils = DownloadUtils() - watchedurl = "{server}/mediabrowser/Users/{UserId}/PlayedItems/%s" % emby_id - if playcount != 0: - downloadUtils.downloadUrl(watchedurl, type="POST") - else: - downloadUtils.downloadUrl(watchedurl, type="DELETE") + directpath = addon.getSetting('useDirectPaths') == "true" + + def logMsg(self, msg, lvl = 1): + + className = self.__class__.__name__ + utils.logMsg("%s %s" % (self.addonName, className), msg, int(lvl)) - def addOrUpdateArtistToKodiLibrary( self, embyId ,connection, cursor): - - addon = xbmcaddon.Addon(id='plugin.video.emby') - WINDOW = xbmcgui.Window(10000) - username = WINDOW.getProperty('currUser') - userid = WINDOW.getProperty('userId%s' % username) - server = WINDOW.getProperty('server%s' % username) - downloadUtils = DownloadUtils() + def addOrUpdateArtistToKodiLibrary(self, embyId, connection, cursor): MBitem = ReadEmbyDB().getFullItem(embyId) # If the item already exist in the local Kodi DB we'll perform a full item update # If the item doesn't exist, we'll add it to the database - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(MBitem["Id"],)) - result = cursor.fetchone() - if result != None: - artistid = result[0] - else: + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (embyId,)) + try: + artistid = cursor.fetchone()[0] + except: artistid = None + ##### The artist details ##### + lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + dateadded = API().getDateCreated(MBitem) + checksum = API().getChecksum(MBitem) - #### The artist details ######### - name = utils.convertEncoding(MBitem["Name"]) - musicBrainsId = None - if MBitem.get("ProviderIds"): - if MBitem.get("ProviderIds").get("MusicBrainzArtist"): - musicBrainsId = MBitem.get("ProviderIds").get("MusicBrainzArtist") - - genres = " / ".join(MBitem.get("Genres")) - bio = utils.convertEncoding(API().getOverview(MBitem)) - dateadded = None - if MBitem.get("DateCreated"): - dateadded = MBitem["DateCreated"].split('.')[0].replace('T', " ") - + name = MBitem['Name'] + musicBrainzId = API().getProvider(MBitem, "musicBrainzArtist") + genres = " / ".join(MBitem.get('Genres')) + bio = API().getOverview(MBitem) + + # Associate artwork thumb = API().getArtwork(MBitem, "Primary") if thumb: - thumb = "" + thumb + "" + thumb = "%s" % thumb fanart = API().getArtwork(MBitem, "Backdrop") if fanart: - fanart = "" + fanart + "" - lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + fanart = "%s" % fanart - #safety check 1: does the artist already exist? - cursor.execute("SELECT idArtist FROM artist WHERE strArtist = ?",(name,)) - result = cursor.fetchone() - if result != None: - artistid = result[0] + # Safety check 1: does the artist already exist? + cursor.execute("SELECT idArtist FROM artist WHERE strArtist = ?", (name,)) + try: + artistid = cursor.fetchone()[0] + except: pass - #safety check 2: does the musicbrainzartistId already exist? - cursor.execute("SELECT idArtist FROM artist WHERE strMusicBrainzArtistID = ?",(musicBrainsId,)) - result = cursor.fetchone() - if result != None: - artistid = result[0] + # Safety check 2: does the MusicBrainzArtistId already exist? + cursor.execute("SELECT idArtist FROM artist WHERE strMusicBrainzArtistID = ?", (musicBrainzId,)) + try: + artistid = cursor.fetchone()[0] + except: pass - ##### ADD THE ARTIST ############ - if artistid == None: - - utils.logMsg("ADD artist to Kodi library","Id: %s - Title: %s" % (embyId, name)) - try: - #create the artist - cursor.execute("select coalesce(max(idArtist),0) as artistid from artist") - artistid = cursor.fetchone()[0] - artistid = artistid + 1 - pathsql="insert into artist(idArtist, strArtist, strMusicBrainzArtistID, strGenres, strBiography, strImage, strFanart, lastScraped, dateAdded) values(?, ?, ?, ?, ?, ?, ?, ?, ?)" - cursor.execute(pathsql, (artistid, name, musicBrainsId, genres, bio, thumb, fanart, lastScraped, dateadded)) - - #create the reference in emby table - pathsql = "INSERT into emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" - cursor.execute(pathsql, (MBitem["Id"], artistid, "artist", API().getChecksum(MBitem))) - - except Exception, e: - utils.logMsg("Error while adding artist to Kodi library: ", e) - return - - #### UPDATE THE ARTIST ##### + + ##### UPDATE THE ARTIST ##### + if artistid: + self.logMsg("UPDATE artist to Kodi library, Id: %s - Artist: %s" % (embyId, name), 1) + + query = "UPDATE artist SET strArtist = ?, strMusicBrainzArtistID = ?, strGenres = ?, strBiography = ?, strImage = ?, strFanart = ?, lastScraped = ?, dateadded = ? WHERE idArtist = ?" + cursor.execute(query, (name, musicBrainzId, genres, bio, thumb, fanart, lastScraped, dateadded, artistid)) + + # Update the checksum in emby table + query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" + cursor.execute(query, (checksum, embyId)) + + ##### OR ADD THE ARTIST ##### else: - utils.logMsg("UPDATE artist to Kodi library","Id: %s - Title: %s" % (embyId, name)) - try: - pathsql="update artist SET strArtist = ?, strMusicBrainzArtistID = ?, strGenres = ?, strBiography = ?, strImage = ?, strFanart = ?, lastScraped = ?, dateAdded = ? WHERE idArtist = ?" - cursor.execute(pathsql, (name, musicBrainsId, genres, bio, thumb, fanart, lastScraped, dateadded, artistid)) - - #update the checksum in emby table - cursor.execute("UPDATE emby SET checksum = ? WHERE emby_id = ?", (API().getChecksum(MBitem),MBitem["Id"])) - except Exception, e: - utils.logMsg("Error while updating artist to Kodi library: ", e) - return + self.logMsg("ADD artist to Kodi library, Id: %s - Artist: %s" % (embyId, name), 1) + + # Create the artist + cursor.execute("select coalesce(max(idArtist),0) as artistid from artist") + artistid = cursor.fetchone()[0] + 1 + query = "INSERT INTO artist(idArtist, strArtist, strMusicBrainzArtistID, strGenres, strBiography, strImage, strFanart, lastScraped, dateAdded) values(?, ?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(query, (artistid, name, musicBrainzId, genres, bio, thumb, fanart, lastScraped, dateadded)) + + # Create the reference in emby table + query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" + cursor.execute(query, (embyId, artistid, "artist", checksum)) + - #update artwork + # Update artwork self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), artistid, "artist", "thumb", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), artistid, "artist", "poster", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Banner"), artistid, "artist", "banner", cursor) @@ -153,112 +121,91 @@ class WriteKodiMusicDB(): self.addOrUpdateArt(API().getArtwork(MBitem, "Disc"), artistid, "artist", "discart", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Backdrop"), artistid, "artist", "fanart", cursor) - def addOrUpdateAlbumToKodiLibrary( self, embyId ,connection, cursor): + def addOrUpdateAlbumToKodiLibrary(self, embyId, connection, cursor): - addon = xbmcaddon.Addon(id='plugin.video.emby') - WINDOW = xbmcgui.Window(10000) - username = WINDOW.getProperty('currUser') - userid = WINDOW.getProperty('userId%s' % username) - server = WINDOW.getProperty('server%s' % username) - downloadUtils = DownloadUtils() - - kodiVersion = 14 - if xbmc.getInfoLabel("System.BuildVersion").startswith("15"): - kodiVersion = 15 + kodiVersion = self.kodiversion MBitem = ReadEmbyDB().getFullItem(embyId) # If the item already exist in the local Kodi DB we'll perform a full item update # If the item doesn't exist, we'll add it to the database - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(MBitem["Id"],)) - result = cursor.fetchone() - if result != None: - albumid = result[0] - else: + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (embyId,)) + try: + albumid = cursor.fetchone()[0] + except: albumid = None - - #### The album details ######### - name = utils.convertEncoding(MBitem["Name"]) + genres = MBitem.get('Genres') + ##### The album details ##### + lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + dateadded = API().getDateCreated(MBitem) + checksum = API().getChecksum(MBitem) + + name = MBitem['Name'] + musicBrainzId = API().getProvider(MBitem, "musicBrainzAlbum") + year = MBitem.get('ProductionYear') + genre = " / ".join(genres) + bio = API().getOverview(MBitem) MBartists = [] - for item in MBitem.get("AlbumArtists"): - MBartists.append(item["Name"]) - + for item in MBitem['AlbumArtists']: + MBartists.append(item['Name']) artists = " / ".join(MBartists) - year = MBitem.get("ProductionYear") - musicBrainsId = None - if MBitem.get("ProviderIds"): - if MBitem.get("ProviderIds").get("MusicBrainzAlbum"): - musicBrainsId = MBitem.get("ProviderIds").get("MusicBrainzAlbum") - - genres = " / ".join(MBitem.get("Genres")) - bio = utils.convertEncoding(API().getOverview(MBitem)) - dateadded = None - if MBitem.get("DateCreated"): - dateadded = MBitem["DateCreated"].split('.')[0].replace('T', " ") - + + # Associate the artwork thumb = API().getArtwork(MBitem, "Primary") if thumb: - thumb = "" + thumb + "" - - lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - - ##### ADD THE ALBUM ############ - if albumid == None: - - utils.logMsg("ADD album to Kodi library","Id: %s - Title: %s" % (embyId, name)) - - #safety check: does the strMusicBrainzAlbumID already exist? - cursor.execute("SELECT idAlbum FROM album WHERE strMusicBrainzAlbumID = ?",(musicBrainsId,)) - result = cursor.fetchone() - if result != None: - albumid = result[0] - else: - #create the album - try: - cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") - albumid = cursor.fetchone()[0] - albumid = albumid + 1 - if kodiVersion == 15: - pathsql="insert into album(idAlbum, strAlbum, strMusicBrainzAlbumID, strArtists, iYear, strGenres, strReview, strImage, lastScraped, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - cursor.execute(pathsql, (albumid, name, musicBrainsId, artists, year, genres, bio, thumb, lastScraped, dateadded, "album")) - else: - pathsql="insert into album(idAlbum, strAlbum, strMusicBrainzAlbumID, strArtists, iYear, strGenres, strReview, strImage, lastScraped, dateAdded) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - cursor.execute(pathsql, (albumid, name, musicBrainsId, artists, year, genres, bio, thumb, lastScraped, dateadded)) - - #create the reference in emby table - pathsql = "INSERT into emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" - cursor.execute(pathsql, (MBitem["Id"], albumid, "album", API().getChecksum(MBitem))) - - except Exception, e: - utils.logMsg("Error while adding album to Kodi library: ", e) - return - - #create the reference in emby table - pathsql = "INSERT into emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" - cursor.execute(pathsql, (MBitem["Id"], albumid, "album", API().getChecksum(MBitem))) - - #### UPDATE THE ALBUM ##### - else: - utils.logMsg("UPDATE album to Kodi library","Id: %s - Title: %s" % (embyId, name)) - try: - if kodiVersion == 15: - pathsql="update album SET strAlbum=?, strMusicBrainzAlbumID=?, strArtists=?, iYear=?, strGenres=?, strReview=?, strImage=?, lastScraped=?, dateAdded=?, strReleaseType=? WHERE idAlbum = ?" - cursor.execute(pathsql, (name, musicBrainsId, artists, year, genres, bio, thumb, lastScraped, dateadded, "album", albumid)) - else: - pathsql="update album SET strAlbum=?, strMusicBrainzAlbumID=?, strArtists=?, iYear=?, strGenres=?, strReview=?, strImage=?, lastScraped=?, dateAdded=? WHERE idAlbum = ?" - cursor.execute(pathsql, (name, musicBrainsId, artists, year, genres, bio, thumb, lastScraped, dateadded, albumid)) - - #update the checksum in emby table - cursor.execute("UPDATE emby SET checksum = ? WHERE emby_id = ?", (API().getChecksum(MBitem),MBitem["Id"])) - - except Exception, e: - utils.logMsg("Error while updating album to Kodi library: ", e) - return + thumb = "%s" % thumb - #update artwork + + ##### UPDATE THE ALBUM ##### + if albumid: + self.logMsg("UPDATE album to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) + + if kodiVersion == 15: + # Kodi Isengard + query = "UPDATE album SET strAlbum = ?, strMusicBrainzAlbumID = ?, strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?, lastScraped = ?, dateAdded = ?, strReleaseType = ? WHERE idAlbum = ?" + cursor.execute(query, (name, musicBrainzId, artists, year, genre, bio, thumb, lastScraped, dateadded, "album", albumid)) + else: + # Kodi Gotham and Helix + query = "UPDATE album SET strAlbum = ?, strMusicBrainzAlbumID = ?, strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?, lastScraped = ?, dateAdded = ? WHERE idAlbum = ?" + cursor.execute(query, (name, musicBrainzId, artists, year, genre, bio, thumb, lastScraped, dateadded, albumid)) + + # Update the checksum in emby table + query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" + cursor.execute(query, (checksum, embyId)) + + ##### OR ADD THE ALBUM ##### + else: + self.logMsg("ADD album to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) + + # Safety check: does the strMusicBrainzAlbumID already exist? + cursor.execute("SELECT idAlbum FROM album WHERE strMusicBrainzAlbumID = ?", (musicBrainzId,)) + try: + albumid = cursor.fetchone()[0] + except: + # Create the album + cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") + albumid = cursor.fetchone()[0] + 1 + if kodiVersion == 15: + # Kodi Isengard + query = "INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strArtists, iYear, strGenres, strReview, strImage, lastScraped, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(query, (albumid, name, musicBrainzId, artists, year, genre, bio, thumb, lastScraped, dateadded, "album")) + else: + # Kodi Gotham and Helix + query = "INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strArtists, iYear, strGenres, strReview, strImage, lastScraped, dateAdded) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(query, (albumid, name, musicBrainzId, artists, year, genre, bio, thumb, lastScraped, dateadded)) + + # Create the reference in emby table + query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" + cursor.execute(query, (embyId, albumid, "album", checksum)) + + + # Add genres + self.AddGenresToMedia(albumid, genres, "album", cursor) + + # Update artwork self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), albumid, "album", "thumb", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "BoxRear"), albumid, "album", "poster", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Banner"), albumid, "album", "banner", cursor) @@ -268,192 +215,152 @@ class WriteKodiMusicDB(): self.addOrUpdateArt(API().getArtwork(MBitem, "Disc"), albumid, "album", "discart", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Backdrop"), albumid, "album", "fanart", cursor) - #link album to artist - artistid = None - album_artists = [] - if MBitem.get("AlbumArtists"): - album_artists = MBitem.get("AlbumArtists") - elif MBitem.get("ArtistItems"): - album_artists = MBitem.get("ArtistItems") - - #some stuff here to get the albums linked to artists + # Link album to artists + if MBartists: + album_artists = MBitem['AlbumArtists'] + elif MBitem.get('ArtistItems'): + album_artists = MBitem['ArtistItems'] + for artist in album_artists: - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(artist["Id"],)) - result = cursor.fetchone() - if result: - artistid = result[0] - sql="INSERT OR REPLACE into album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" - cursor.execute(sql, (artistid, albumid, artist["Name"])) - #update discography - sql="INSERT OR REPLACE into discography(idArtist, strAlbum, strYear) values(?, ?, ?)" - cursor.execute(sql, (artistid, name, str(year))) - - #add genres - self.AddGenresToMedia(albumid, MBitem.get("Genres"), "album", cursor) + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) + try: + artistid = cursor.fetchone()[0] + except: pass + else: + query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" + cursor.execute(query, (artistid, albumid, artist['Name'])) + # Update discography + query = "INSERT OR REPLACE INTO discography(idArtist, strAlbum, strYear) values(?, ?, ?)" + cursor.execute(query, (artistid, name, str(year))) - #return the album id - return albumid - - def addOrUpdateSongToKodiLibrary( self, embyId ,connection, cursor): - - addon = xbmcaddon.Addon(id='plugin.video.emby') - WINDOW = xbmcgui.Window(10000) - username = WINDOW.getProperty('currUser') - userid = WINDOW.getProperty('userId%s' % username) - server = WINDOW.getProperty('server%s' % username) - downloadUtils = DownloadUtils() + def addOrUpdateSongToKodiLibrary(self, embyId, connection, cursor): + kodiVersion = self.kodiversion + MBitem = ReadEmbyDB().getFullItem(embyId) - timeInfo = API().getTimeInfo(MBitem) - userData=API().getUserData(MBitem) - - kodiVersion = 14 - if xbmc.getInfoLabel("System.BuildVersion").startswith("15"): - kodiVersion = 15 - # If the item already exist in the local Kodi DB we'll perform a full item update # If the item doesn't exist, we'll add it to the database - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(MBitem["Id"],)) - result = cursor.fetchone() - if result != None: - songid = result[0] - else: + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (embyId,)) + try: + songid = cursor.fetchone()[0] + except: songid = None + timeInfo = API().getTimeInfo(MBitem) + userData = API().getUserData(MBitem) + genres = MBitem.get('Genres') - #### The song details ######### - name = utils.convertEncoding(MBitem["Name"]) - musicBrainzId = None - if MBitem.get("ProviderIds"): - if MBitem.get("ProviderIds").get("MusicBrainzTrackId"): - musicBrainzId = MBitem.get("ProviderIds").get("MusicBrainzTrackId") - - genres = " / ".join(MBitem.get("Genres")) - artists = " / ".join(MBitem.get("Artists")) - track = MBitem.get("IndexNumber") - duration = MBitem.get("RunTimeTicks", 0) / 10000000 - year = MBitem.get("ProductionYear") - bio = utils.convertEncoding(API().getOverview(MBitem)) + ##### The song details ##### + playcount = userData.get('PlayCount') + lastplayed = userData.get('LastPlayedDate') + dateadded = API().getDateCreated(MBitem) + checksum = API().getChecksum(MBitem) - dateadded = None - if MBitem.get("DateCreated"): - dateadded = MBitem["DateCreated"].split('.')[0].replace('T', " ") - - if userData.get("LastPlayedDate") != None: - lastplayed = userData.get("LastPlayedDate") - else: - lastplayed = None - - playcount = None - if userData.get("PlayCount"): - playcount = int(userData.get("PlayCount")) - - #get the album - albumid = None - if MBitem.get("AlbumId"): - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(MBitem.get("AlbumId"),)) - result = cursor.fetchone() - if result: - albumid = result[0] - if albumid == None: - #no album = single in kodi, we need to create a single album for that - cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") - albumid = cursor.fetchone()[0] - albumid = albumid + 1 - if kodiVersion == 15: - pathsql="insert into album(idAlbum, strArtists, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?)" - cursor.execute(pathsql, (albumid, artists, genres, year, dateadded, "single")) - else: - pathsql="insert into album(idAlbum, strArtists, strGenres, iYear, dateAdded) values(?, ?, ?, ?, ?)" - cursor.execute(pathsql, (albumid, artists, genres, year, dateadded)) - #some stuff here to get the album linked to artists - for artist in MBitem.get("ArtistItems"): - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(artist["Id"],)) - result = cursor.fetchone() - if result: - artistid = result[0] - sql="INSERT OR REPLACE into album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" - cursor.execute(sql, (artistid, albumid, artist["Name"])) + name = MBitem['Name'] + musicBrainzId = API().getProvider(MBitem, "musicBrainzTrackId") + genre = " / ".join(genres) + artists = " / ".join(MBitem.get('Artists')) + track = MBitem.get('IndexNumber') + year = MBitem.get('ProductionYear') + bio = API().getOverview(MBitem) + duration = timeInfo.get('TotalTime') - if PlayUtils().isDirectPlay(MBitem): - playurl = PlayUtils().directPlay(MBitem) - #use the direct file path - if "\\" in playurl: - filename = playurl.rsplit("\\",1)[-1] - path = playurl.replace(filename,"") - elif "/" in playurl: - filename = playurl.rsplit("/",1)[-1] - path = playurl.replace(filename,"") - else: - #for transcoding we just use the server's streaming path because I couldn't figure out how to set the plugin path in the music DB - path = server + "/Audio/%s/" %MBitem["Id"] + # Get the path and filename + playurl = PlayUtils().directPlay(MBitem) + try: + filename = basename(playurl) + path = playurl.replace(filename, "") + except: # playurl returned false - using server streaming path, because could not figure out plugin paths for music DB + playurl = PlayUtils().directstream(MBitem, self.server, embyId, "Audio") filename = "stream.mp3" + path = playurl.replace(filename, "") - #get the path - cursor.execute("SELECT idPath as pathid FROM path WHERE strPath = ?",(path,)) - result = cursor.fetchone() - if result != None: - pathid = result[0] - else: - cursor.execute("select coalesce(max(idPath),0) as pathid from path") + + # Validate the path in database + cursor.execute("SELECT idPath as pathid FROM path WHERE strPath = ?", (path,)) + try: pathid = cursor.fetchone()[0] - pathid = pathid + 1 - pathsql = "insert into path(idPath, strPath) values(?, ?)" - cursor.execute(pathsql, (pathid,path)) - - - ##### ADD THE SONG ############ - if songid == None: - - utils.logMsg("ADD song to Kodi library","Id: %s - Title: %s" % (embyId, name)) - try: - #create the song - cursor.execute("select coalesce(max(idSong),0) as songid from song") - songid = cursor.fetchone()[0] - songid = songid + 1 - pathsql="insert into song(idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack, iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - cursor.execute(pathsql, (songid, albumid, pathid, artists, genres, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed)) - - #create the reference in emby table - pathsql = "INSERT into emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" - cursor.execute(pathsql, (MBitem["Id"], songid, "song", API().getChecksum(MBitem))) - except Exception, e: - utils.logMsg("Error while adding song to Kodi library: ", e) - return - - #### UPDATE THE SONG ##### + except: + cursor.execute("select coalesce(max(idPath),0) as pathid from path") + pathid = cursor.fetchone()[0] + 1 + query = "INSERT INTO path(idPath, strPath) values(?, ?)" + cursor.execute(query, (pathid, path)) + + # Get the album + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (MBitem.get("AlbumId"),)) + try: + albumid = cursor.fetchone()[0] + except: + # No album found, create a single's album + cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") + albumid = cursor.fetchone()[0] + 1 + if kodiVersion == 15: + # Kodi Isengard + query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?)" + cursor.execute(query, (albumid, artists, genre, year, dateadded, "single")) + else: + # Kodi Gotham and Helix + query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded) values(?, ?, ?, ?, ?)" + cursor.execute(query, (albumid, artists, genre, year, dateadded)) + + # Link album to artists + for artist in MBitem['ArtistItems']: + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) + try: + artistid = cursor.fetchone()[0] + except: pass + else: + query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" + cursor.execute(query, (artistid, albumid, artist['Name'])) + + + ##### UPDATE THE SONG ##### + if songid: + self.logMsg("UPDATE song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) + + query = "UPDATE song SET idAlbum = ?, strArtists = ?, strGenres = ?, strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?, strFilename = ?, strMusicBrainzTrackID = ?, iTimesPlayed = ?, lastplayed = ? WHERE idSong = ?" + cursor.execute(query, (albumid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed, songid)) + + # Update the checksum in emby table + query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" + cursor.execute(query, (checksum, embyId)) + + ##### OR ADD THE SONG ##### else: - utils.logMsg("UPDATE song to Kodi library","Id: %s - Title: %s" % (embyId, name)) - try: - pathsql="update song SET idAlbum=?, strArtists=?, strGenres=?, strTitle=?, iTrack=?, iDuration=?, iYear=?, strFileName=?, strMusicBrainzTrackID=?, iTimesPlayed=?, lastplayed=? WHERE idSong = ?" - cursor.execute(pathsql, (albumid, artists, genres, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed, songid)) - - #update the checksum in emby table - cursor.execute("UPDATE emby SET checksum = ? WHERE emby_id = ?", (API().getChecksum(MBitem),MBitem["Id"])) - except Exception, e: - utils.logMsg("Error while updating song to Kodi library: ", e) - return + self.logMsg("ADD song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) + + # Create the song + cursor.execute("select coalesce(max(idSong),0) as songid from song") + songid = cursor.fetchone()[0] + 1 + query = "INSERT INTO song(idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack, iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(query, (songid, albumid, pathid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed)) + + # Create the reference in emby table + query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" + cursor.execute(query, (embyId, songid, "song", checksum)) + - #add genres - self.AddGenresToMedia(songid, MBitem.get("Genres"), "song", cursor) + # Add genres + self.AddGenresToMedia(songid, genres, "song", cursor) - #link song to album + # Link song to album if albumid: - sql="INSERT OR REPLACE into albuminfosong(idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration) values(?, ?, ?, ?, ?)" - cursor.execute(sql, (songid, albumid, track, name, duration)) + query = "INSERT OR REPLACE INTO albuminfosong(idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration) values(?, ?, ?, ?, ?)" + cursor.execute(query, (songid, albumid, track, name, duration)) - #link song to artist - for artist in MBitem.get("ArtistItems"): - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?",(artist["Id"],)) - result = cursor.fetchone() - if result: - artistid = result[0] - sql="INSERT OR REPLACE into song_artist(idArtist, idSong, strArtist) values(?, ?, ?)" - cursor.execute(sql, (artistid, songid, artist["Name"])) + # Link song to artist + for artist in MBitem.get('ArtistItems'): + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) + try: + artistid = cursor.fetchone()[0] + except: pass + else: + query = "INSERT OR REPLACE INTO song_artist(idArtist, idSong, strArtist) values(?, ?, ?)" + cursor.execute(query, (artistid, songid, artist['Name'])) - #update artwork + # Update artwork self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), songid, "song", "thumb", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), songid, "song", "poster", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Banner"), songid, "song", "banner", cursor) @@ -463,42 +370,51 @@ class WriteKodiMusicDB(): self.addOrUpdateArt(API().getArtwork(MBitem, "Disc"), songid, "song", "discart", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Backdrop"), songid, "song", "fanart", cursor) - def deleteItemFromKodiLibrary(self, id, connection, cursor ): + def deleteItemFromKodiLibrary(self, id, connection, cursor): cursor.execute("SELECT kodi_id, media_type FROM emby WHERE emby_id=?", (id,)) - result = cursor.fetchone() - if result: + try: + result = cursor.fetchone() kodi_id = result[0] media_type = result[1] - if media_type == "artist": - utils.logMsg("deleting artist from Kodi library --> ",id) + except: pass + else: + if "artist" in media_type: + self.logMsg("Deleting artist from Kodi library, Id: %s" % id, 1) cursor.execute("DELETE FROM artist WHERE idArtist = ?", (kodi_id,)) - if media_type == "song": - utils.logMsg("deleting song from Kodi library --> ",id) + elif "song" in media_type: + self.logMsg("Deleting song from Kodi library, Id: %s" % id, 1) cursor.execute("DELETE FROM song WHERE idSong = ?", (kodi_id,)) - if media_type == "album": - utils.logMsg("deleting album from Kodi library --> ",id) + elif "album" in media_type: + self.logMsg("Deleting album from Kodi library, Id: %s" % id, 1) cursor.execute("DELETE FROM album WHERE idAlbum = ?", (kodi_id,)) - - #delete the record in emby table + + # Delete the record in emby table cursor.execute("DELETE FROM emby WHERE emby_id = ?", (id,)) def addOrUpdateArt(self, imageUrl, kodiId, mediaType, imageType, cursor): - updateDone = False + if imageUrl: - cursor.execute("SELECT url FROM art WHERE media_id = ? AND media_type = ? AND type = ?", (kodiId, mediaType, imageType)) - result = cursor.fetchone() - if(result == None): - utils.logMsg("ArtworkSync", "Adding Art Link for kodiId: " + str(kodiId) + " (" + imageUrl + ")") - cursor.execute("INSERT INTO art(media_id, media_type, type, url) values(?, ?, ?, ?)", (kodiId, mediaType, imageType, imageUrl)) + + cacheimage = False + + cursor.execute("SELECT url FROM art WHERE media_id = ? AND media_type = ? AND type = ?", (kodiId, mediaType, imageType,)) + try: + url = cursor.fetchone()[0] + except: # Image does not exists + cacheimage = True + self.logMsg("Adding Art Link for kodiId: %s (%s)" % (kodiId, imageUrl), 1) + query = "INSERT INTO art(media_id, media_type, type, url) values(?, ?, ?, ?)" + cursor.execute(query, (kodiId, mediaType, imageType, imageUrl)) else: - url = result[0]; - if(url != imageUrl): - utils.logMsg("ArtworkSync", "Updating Art Link for kodiId: " + str(kodiId) + " (" + url + ") -> (" + imageUrl + ")") - cursor.execute("UPDATE art set url = ? WHERE media_id = ? AND media_type = ? AND type = ?", (imageUrl, kodiId, mediaType, imageType)) + if url != imageUrl: + cacheimage = True + self.logMsg("Updating Art Link for kodiId: %s (%s) -> (%s)" % (kodiId, url, imageUrl), 1) + query = "UPDATE art SET url = ? WHERE media_id = ? and media_type = ? and type = ?" + cursor.execute(query, (imageUrl, kodiId, mediaType, imageType)) - #cache fanart textures in Kodi texture cache - if imageType == "fanart": + # Cache fanart textures in Kodi texture cache + if cacheimage and "fanart" in imageType: self.textureCache.CacheTexture(imageUrl) def AddGenresToMedia(self, id, genres, mediatype, cursor): @@ -508,27 +424,18 @@ class WriteKodiMusicDB(): for genre in genres: idGenre = None - cursor.execute("SELECT idGenre as idGenre FROM genre WHERE strGenre = ?",(genre,)) - result = cursor.fetchone() - if result != None: - idGenre = result[0] - #create genre - if idGenre == None: - cursor.execute("select coalesce(max(idGenre),0) as idGenre from genre") + cursor.execute("SELECT idGenre as idGenre FROM genre WHERE strGenre = ?", (genre,)) + try: idGenre = cursor.fetchone()[0] - idGenre = idGenre + 1 - sql="insert into genre(idGenre, strGenre) values(?, ?)" - cursor.execute(sql, (idGenre,genre)) - - #assign genre to item - if mediatype == "album": - sql="INSERT OR REPLACE into album_genre(idGenre, idAlbum) values(?, ?)" - cursor.execute(sql, (idGenre,id)) - elif mediatype == "song": - sql="INSERT OR REPLACE into song_genre(idGenre, idSong) values(?, ?)" - cursor.execute(sql, (idGenre,id)) - else: - return - - - + except: # Create the genre + cursor.execute("select coalesce(max(idGenre),0) as idGenre from genre") + idGenre = cursor.fetchone()[0] + 1 + query = "INSERT INTO genre(idGenre, strGenre) values(?, ?)" + cursor.execute(query, (idGenre, genre)) + finally: # Assign the genre to item + if "album" in mediatype: + query = "INSERT OR REPLACE INTO album_genre(idGenre, idAlbum) values(?, ?)" + cursor.execute(query, (idGenre, id)) + elif "song" in mediatype: + query = "INSERT OR REPLACE INTO song_genre(idGenre, idSong) values(?, ?)" + cursor.execute(query, (idGenre, id)) \ No newline at end of file diff --git a/resources/settings.xml b/resources/settings.xml index bf6eb39a..b9ea75d0 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -3,14 +3,15 @@ + - - + + diff --git a/service.py b/service.py index 00e58901..a5ca821d 100644 --- a/service.py +++ b/service.py @@ -1,4 +1,4 @@ -# -- coding: utf-8 -- +# -*- coding: utf-8 -*- import os import sys @@ -8,6 +8,7 @@ from datetime import datetime import xbmcaddon import xbmc import xbmcgui +import xbmcvfs addon_ = xbmcaddon.Addon(id='plugin.video.emby') addon_path = addon_.getAddonInfo('path').decode('utf-8') @@ -105,6 +106,9 @@ class Service(): while not self.KodiMonitor.abortRequested(): + # Refresh with the latest addon settings + addon = xbmcaddon.Addon() + # Before proceeding, need to make sure: # 1. Server is online # 2. User is set @@ -114,7 +118,7 @@ class Service(): # Emby server is online # Verify if user is set and has access to the server - if (user.currUser != None) and user.HasAccess: + if (user.currUser is not None) and user.HasAccess: # If an item is playing if xbmc.Player().isPlaying(): @@ -125,7 +129,7 @@ class Service(): currentFile = xbmc.Player().getPlayingFile() # Update positionticks - if player.played_information.get(currentFile) != None: + if player.played_information.get(currentFile) is not None: player.played_information[currentFile]["currentPosition"] = playTime td = datetime.today() - lastProgressUpdate @@ -133,23 +137,18 @@ class Service(): # Report progress to Emby server if (secDiff > 3): - try: - player.reportPlayback() - except Exception as msg: - self.logMsg("Exception reporting progress: %s" % msg) + player.reportPlayback() lastProgressUpdate = datetime.today() elif WINDOW.getProperty('commandUpdate') == "true": # Received a remote control command that # requires updating immediately - try: - WINDOW.clearProperty('commandUpdate') - player.reportPlayback() - except: pass + WINDOW.clearProperty('commandUpdate') + player.reportPlayback() lastProgressUpdate = datetime.today() except Exception as e: - self.logMsg("Exception in Playback Monitor Service: %s" % e) + self.logMsg("Exception in Playback Monitor Service: %s" % e, 1) pass else: # Start up events @@ -158,20 +157,20 @@ class Service(): if self.welcome_msg: # Reset authentication warnings self.welcome_msg = False - xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser.decode('utf-8').encode('utf-8'), time=2000, sound=False) + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, icon="special://home/addons/plugin.video.emby/icon.png", time=2000, sound=False) # Start the Websocket Client - if (self.newWebSocketThread == None): + if (self.newWebSocketThread is None): self.newWebSocketThread = "Started" ws.start() # Start the Library Sync Thread - if (self.newLibraryThread == None): + if (self.newLibraryThread is None): self.newLibraryThread = "Started" library.start() else: - if (user.currUser == None) and self.warn_auth: + if (user.currUser is None) and self.warn_auth: # Alert user is not authenticated and suppress future warning self.warn_auth = False self.logMsg("Not authenticated yet.", 1) @@ -206,7 +205,7 @@ class Service(): if self.server_online: self.logMsg("Server is offline.", 1) WINDOW.setProperty('Server_online', "false") - xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName, sound=False) + xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName, icon="special://home/addons/plugin.video.emby/icon.png", sound=False) self.server_online = False else: @@ -218,14 +217,14 @@ class Service(): # Abort was requested while waiting. break # Alert the user that server is online. - xbmcgui.Dialog().notification("Connection successful", "%s Server is online." % self.addonName, time=2000, sound=False) + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, icon="special://home/addons/plugin.video.emby/icon.png", time=2000, sound=False) self.server_online = True self.logMsg("Server is online and ready.", 1) WINDOW.setProperty('Server_online', "true") # Start the User client - if self.newUserClient == None: + if self.newUserClient is None: self.newUserClient = "Started" user.start() break @@ -241,11 +240,17 @@ class Service(): # If user reset library database. if WINDOW.getProperty('SyncInstallRunDone') == "false": addon.setSetting('SyncInstallRunDone', "false") + # If user delete Emby settings + if WINDOW.getProperty('deletesettings') == "true": + addondir = xbmc.translatePath(addon.getAddonInfo('profile')).decode('utf-8') + dataPath = "%ssettings.xml" % addondir + xbmcvfs.delete(dataPath) + self.logMsg("Deleting: settings.xml", 1) - if (self.newWebSocketThread != None): + if (self.newWebSocketThread is not None): ws.stopClient() - if (self.newUserClient != None): + if (self.newUserClient is not None): user.stopClient() self.logMsg("======== STOP %s ========" % self.addonName, 0)