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() 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: songid = None #### 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)) 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"])) 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"] filename = "stream.mp3" #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") 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
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() 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: artistid = None #### 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', " ") thumb = API().getArtwork(MBitem, "Primary") if thumb: thumb = "<thumb>" + thumb + "</thumb>" fanart = API().getArtwork(MBitem, "Backdrop") if fanart: fanart = "<fanart>" + fanart + "</fanart>" lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #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 2: does the musicbrainzartistId already exist? cursor.execute("SELECT idArtist FROM artist WHERE strMusicBrainzArtistID = ?",(musicBrainsId,)) result = cursor.fetchone() if result != None: artistid = result[0] ##### 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
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 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: albumid = None #### The album details ######### name = utils.convertEncoding(MBitem["Name"]) MBartists = [] for item in MBitem.get("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', " ") thumb = API().getArtwork(MBitem, "Primary") if thumb: thumb = "<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)))