diff --git a/resources/lib/PlexFunctions.py b/resources/lib/PlexFunctions.py index a22115bb..95d30597 100644 --- a/resources/lib/PlexFunctions.py +++ b/resources/lib/PlexFunctions.py @@ -165,7 +165,7 @@ def GetPlexMetadata(key): Can be called with either Plex key '/library/metadata/xxxx'metadata OR with the digits 'xxxx' only. - Returns None if something went wrong + Returns None or 401 if something went wrong """ key = str(key) if '/library/metadata/' in key: diff --git a/resources/lib/playbackutils.py b/resources/lib/playbackutils.py index 3c781215..040d495a 100644 --- a/resources/lib/playbackutils.py +++ b/resources/lib/playbackutils.py @@ -40,7 +40,10 @@ class PlaybackUtils(): self.artwork = artwork.Artwork() self.emby = embyserver.Read_EmbyServer() - self.pl = playlist.Playlist() + if self.API.getType() == 'track': + self.pl = playlist.Playlist(typus='music') + else: + self.pl = playlist.Playlist(typus='video') def play(self, itemid, dbid=None): @@ -91,7 +94,7 @@ class PlaybackUtils(): ############### ORGANIZE CURRENT PLAYLIST ################ homeScreen = xbmc.getCondVisibility('Window.IsActive(home)') - playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) + playlist = self.pl.playlist startPos = max(playlist.getposition(), 0) # Can return -1 sizePlaylist = playlist.size() self.currentPosition = startPos diff --git a/resources/lib/playlist.py b/resources/lib/playlist.py index d059f384..591e6a65 100644 --- a/resources/lib/playlist.py +++ b/resources/lib/playlist.py @@ -8,7 +8,6 @@ from urllib import urlencode import xbmc import embydb_functions as embydb -import read_embyserver as embyserver import utils import playbackutils import PlexFunctions @@ -19,95 +18,105 @@ import PlexAPI @utils.logging class Playlist(): + """ + Initiate with Playlist(typus='video' or 'music') + """ - def __init__(self): + def __init__(self, typus=None): self.userid = utils.window('currUserId') self.server = utils.window('pms_server') + if typus == 'video': + self.playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) + elif typus == 'music': + self.playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC) + else: + self.playlist = None - self.emby = embyserver.Read_EmbyServer() - - def playAll(self, itemids, startat): - window = utils.window - - embyconn = utils.kodiSQL('emby') - embycursor = embyconn.cursor() - emby_db = embydb.Embydb_Functions(embycursor) - - player = xbmc.Player() - playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) - playlist.clear() - - self.logMsg("---*** PLAY ALL ***---", 1) - self.logMsg("Items: %s and start at: %s" % (itemids, startat), 1) - - started = False - window('plex_customplaylist', value="true") - - if startat != 0: - # Seek to the starting position - window('plex_customplaylist.seektime', str(startat)) - + def __initiatePlaylist(self, itemids): + playlist = None with embydb.GetEmbyDB() as emby_db: for itemid in itemids: + embydb_item = emby_db.getItem_byId(itemid) + try: + mediatype = embydb_item[4] + except TypeError: + self.logMsg('Couldnt find item %s in Kodi db' % itemid, 1) + item = PlexFunctions.GetPlexMetadata(itemid) + if item in (None, 401): + continue + if PlexAPI.API(item[0]).getType() == 'track': + playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC) + self.logMsg('Music playlist initiated', 1) + else: + playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) + self.logMsg('Video playlist initiated', 1) + else: + if mediatype == 'song': + playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC) + self.logMsg('Music playlist initiated', 1) + else: + playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) + self.logMsg('Video playlist initiated', 1) + break + if playlist is not None: + playlist.clear() + self.playlist = playlist + + def __addToPlaylist(self, itemids, startPlayer=False): + started = False + with embydb.GetEmbyDB() as emby_db: + for itemid in itemids: + self.logMsg("Adding %s to playlist." % itemid, 1) embydb_item = emby_db.getItem_byId(itemid) try: dbid = embydb_item[0] mediatype = embydb_item[4] except TypeError: - # Item is not found in our database, add item manually - self.logMsg("Item was not found in the database, manually " - "adding item.", 1) + self.logMsg('Couldnt find item %s in Kodi db' % itemid, 1) item = PlexFunctions.GetPlexMetadata(itemid) - if item is None or item == 401: + if item in (None, 401): self.logMsg('Could not download itemid %s' % itemid, -1) else: - self.addtoPlaylist_xbmc(playlist, item) + self.addtoPlaylist_xbmc(self.playlist, item) else: # Add to playlist self.addtoPlaylist(dbid, mediatype) - - self.logMsg("Adding %s to playlist." % itemid, 1) - - if not started: + if started is False and startPlayer is True: started = True - player.play(playlist) + xbmc.Player().play(self.playlist) + def playAll(self, itemids, startat): + self.logMsg("---*** PLAY ALL ***---", 1) + self.logMsg("Items: %s and start at: %s" % (itemids, startat), 1) + + if self.playlist is None: + self.__initiatePlaylist(itemids) + if self.playlist is None: + self.logMsg('Could not create playlist, abort', -1) + return + + utils.window('plex_customplaylist', value="true") + if startat != 0: + # Seek to the starting position + utils.window('plex_customplaylist.seektime', str(startat)) + self.__addToPlaylist(itemids, startPlayer=True) self.verifyPlaylist() def modifyPlaylist(self, itemids): - - embyconn = utils.kodiSQL('emby') - embycursor = embyconn.cursor() - emby_db = embydb.Embydb_Functions(embycursor) - self.logMsg("---*** ADD TO PLAYLIST ***---", 1) self.logMsg("Items: %s" % itemids, 1) - # player = xbmc.Player() - playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) - - for itemid in itemids: - embydb_item = emby_db.getItem_byId(itemid) - try: - dbid = embydb_item[0] - mediatype = embydb_item[4] - except TypeError: - self.logMsg('item %s not found in Kodi DB, add manually' - % itemid, 1) - item = self.emby.getItem(itemid) - self.addtoPlaylist_xbmc(playlist, item) - else: - # Add to playlist - self.addtoPlaylist(dbid, mediatype) - - self.logMsg("Adding %s to playlist." % itemid, 1) + self.__initiatePlaylist(itemids) + self.__addToPlaylist(itemids, startPlayer=True) self.verifyPlaylist() - embycursor.close() - return playlist def addtoPlaylist(self, dbid=None, mediatype=None, url=None): + """ + mediatype: Kodi type: 'movie', 'episode', 'musicvideo', 'artist', + 'album', 'song', 'genre' + """ pl = { @@ -116,17 +125,17 @@ class Playlist(): 'method': "Playlist.Add", 'params': { - 'playlistid': 1 + 'playlistid': self.playlist.getPlayListId() } } if dbid is not None: - pl['params']['item'] = {'%sid' % mediatype: int(dbid)} + pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype): + int(dbid)} else: pl['params']['item'] = {'file': url} - self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) - def addtoPlaylist_xbmc(self, playlist, item): + def addtoPlaylist_xbmc(self, item): API = PlexAPI.API(item[0]) params = { 'mode': "play", @@ -140,7 +149,7 @@ class Playlist(): listitem = API.CreateListItemFromPlexItem() playbackutils.PlaybackUtils(item[0]).setArtwork(listitem) - playlist.add(playurl, listitem) + self.playlist.add(playurl, listitem) def insertintoPlaylist(self, position, dbid=None, mediatype=None, url=None): @@ -151,12 +160,13 @@ class Playlist(): 'method': "Playlist.Insert", 'params': { - 'playlistid': 1, + 'playlistid': self.playlist.getPlayListId(), 'position': position } } if dbid is not None: - pl['params']['item'] = {'%sid' % mediatype: int(dbid)} + pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype): + int(dbid)} else: pl['params']['item'] = {'file': url} @@ -171,7 +181,7 @@ class Playlist(): 'method': "Playlist.GetItems", 'params': { - 'playlistid': 1, + 'playlistid': self.playlist.getPlayListId(), 'properties': ['title', 'file'] } } @@ -186,7 +196,7 @@ class Playlist(): 'method': "Playlist.Remove", 'params': { - 'playlistid': 1, + 'playlistid': self.playlist.getPlayListId(), 'position': position } }