From 88632dad60fbb56446e82c10c629488a52d8629c Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Thu, 20 Aug 2015 06:42:40 -0500 Subject: [PATCH] Music library - remote Trying strm files to still have plugin paths. Will require restart because I've added a new column to hold the full path for strm in the event we need to delete them. --- resources/lib/Entrypoint.py | 8 ++-- resources/lib/LibrarySync.py | 3 ++ resources/lib/Utils.py | 6 +++ resources/lib/WriteKodiMusicDB.py | 78 ++++++++++++++++++++++++------- 4 files changed, 74 insertions(+), 21 deletions(-) diff --git a/resources/lib/Entrypoint.py b/resources/lib/Entrypoint.py index 6e10a414..4eb2c1ad 100644 --- a/resources/lib/Entrypoint.py +++ b/resources/lib/Entrypoint.py @@ -132,7 +132,7 @@ def getThemeMedia(): server = WINDOW.getProperty('server%s' % currUser) playback = None - library = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/").decode('utf-8') + library = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/themes/").decode('utf-8') # Choose playback method resp = xbmcgui.Dialog().select("Choose playback method for your themes", ["Direct Play", "Direct Stream"]) @@ -160,7 +160,7 @@ def getThemeMedia(): # Create library directory if not xbmcvfs.exists(library): - xbmcvfs.mkdir(library) + xbmcvfs.mkdirs(library) # Get every user view Id userViews = [] @@ -186,7 +186,7 @@ def getThemeMedia(): # Get paths for theme videos for itemId in itemIds: - nfo_path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/%s/" % itemIds[itemId]) + nfo_path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/themes/%s/" % itemIds[itemId]) # Create folders for each content if not xbmcvfs.exists(nfo_path): xbmcvfs.mkdir(nfo_path) @@ -244,7 +244,7 @@ def getThemeMedia(): if itemId in itemIds: continue - nfo_path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/%s/" % musicitemIds[itemId]) + nfo_path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/themes/%s/" % musicitemIds[itemId]) # Create folders for each content if not xbmcvfs.exists(nfo_path): xbmcvfs.mkdir(nfo_path) diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index b5957afb..34147b32 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -164,6 +164,9 @@ class LibrarySync(threading.Thread): try: cursor.execute("ALTER TABLE emby ADD COLUMN kodi_file_id INTEGER") except: pass + try: + cursor.execute("ALTER TABLE emby ADD COLUMN fullpath TEXT") + except: pass connection.commit() self.MusicFullSync(connection,cursor,pDialog) diff --git a/resources/lib/Utils.py b/resources/lib/Utils.py index 37886706..a6fa5947 100644 --- a/resources/lib/Utils.py +++ b/resources/lib/Utils.py @@ -309,6 +309,12 @@ def reset(): cursor.execute("DELETE FROM " + tableName) connection.commit() cursor.close() + + # Remove the strm and nfo + path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/").decode('utf-8') + if xbmcvfs.exists(path): + xbmc.log("Removing %s." % path) + shutil.rmtree(path) # reset the install run flag diff --git a/resources/lib/WriteKodiMusicDB.py b/resources/lib/WriteKodiMusicDB.py index 67450501..c8c0bd12 100644 --- a/resources/lib/WriteKodiMusicDB.py +++ b/resources/lib/WriteKodiMusicDB.py @@ -11,6 +11,7 @@ from ntpath import split as ntsplit import xbmc import xbmcgui import xbmcaddon +import xbmcvfs from ClientInformation import ClientInformation import Utils as utils @@ -201,9 +202,14 @@ class WriteKodiMusicDB(): 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)) + fullpath = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/music/%s/" % embyId).decode('utf-8') + # Create the album folder + if not xbmcvfs.exists(fullpath): + xbmcvfs.mkdir(fullpath) + # 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)) + query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum, fullpath) values(?, ?, ?, ?, ?)" + cursor.execute(query, (embyId, albumid, "album", checksum, fullpath)) # Add genres @@ -270,20 +276,41 @@ class WriteKodiMusicDB(): year = MBitem.get('ProductionYear') bio = API().getOverview(MBitem) duration = timeInfo.get('TotalTime') + albumId = MBitem.get('AlbumId') - # Get the path and filename - playurl = PlayUtils().directPlay(MBitem) + fullpath = "" + if self.directpath: + # Get the path and filename + playurl = PlayUtils().directPlay(MBitem) + try: + path, filename = ntsplit(playurl) + if "/" in playurl: + path = "%s/" % path + elif "\\" in playurl: + path = "%s\\" % path + except: return # playurl returned False + else: + # Plugin path via strm file. We need the album Id for directory and itemId for filename. To maintain, we'll add the path to the emby database. + library = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/music/").decode('utf-8') - try: - path, filename = ntsplit(playurl) - if "/" in playurl: - path = "%s/" % path - elif "\\" in playurl: - path = "%s\\" % path - 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, "") + # Create the music library + if not xbmcvfs.exists(library): + xbmcvfs.mkdirs(library) + + path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/music/%s/" % albumId).decode('utf-8') + + # Create the album folder + if not xbmcvfs.exists(path): + xbmcvfs.mkdir(path) + + filename = "%s.strm" % embyId + fullpath = "%s%s" % (path, filename) + + # Create the strm file + strm = open(fullpath, 'w') + pluginpath = "plugin://plugin.video.emby/music/%s/?id=%s&mode=play" % (albumId, embyId) + strm.write(pluginpath) + strm.close() # Validate the path in database @@ -297,7 +324,7 @@ class WriteKodiMusicDB(): cursor.execute(query, (pathid, path)) # Get the album - cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (MBitem.get("AlbumId"),)) + cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (albumId,)) try: albumid = cursor.fetchone()[0] except: @@ -346,8 +373,8 @@ class WriteKodiMusicDB(): 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)) + query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum, fullpath) values(?, ?, ?, ?, ?)" + cursor.execute(query, (embyId, songid, "song", checksum, fullpath)) # Add genres @@ -393,10 +420,27 @@ class WriteKodiMusicDB(): 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,)) + + cursor.execute("SELECT fullpath FROM emby WHERE emby_id = ?", (id,)) + try: # Delete the strm file + fullpath = cursor.fetchone()[0] + except: pass + else: + self.logMsg("Delete the strm %s." % fullpath) + xbmcvfs.delete(fullpath) + 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,)) + cursor.execute("SELECT fullpath FROM emby WHERE emby_id = ?", (id,)) + try: # Delete the folder + fullpath = cursor.fetchone()[0] + except: pass + else: + self.logMsg("Delete the album folder %s." % fullpath) + xbmcvfs.rmdir(fullpath) + # Delete the record in emby table cursor.execute("DELETE FROM emby WHERE emby_id = ?", (id,))