Cleanup playlist.py

This commit is contained in:
tomkat83 2016-06-26 16:10:32 +02:00
parent 3fb6443b2b
commit 40529938ca
3 changed files with 85 additions and 72 deletions

View file

@ -165,7 +165,7 @@ def GetPlexMetadata(key):
Can be called with either Plex key '/library/metadata/xxxx'metadata Can be called with either Plex key '/library/metadata/xxxx'metadata
OR with the digits 'xxxx' only. OR with the digits 'xxxx' only.
Returns None if something went wrong Returns None or 401 if something went wrong
""" """
key = str(key) key = str(key)
if '/library/metadata/' in key: if '/library/metadata/' in key:

View file

@ -40,7 +40,10 @@ class PlaybackUtils():
self.artwork = artwork.Artwork() self.artwork = artwork.Artwork()
self.emby = embyserver.Read_EmbyServer() 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): def play(self, itemid, dbid=None):
@ -91,7 +94,7 @@ class PlaybackUtils():
############### ORGANIZE CURRENT PLAYLIST ################ ############### ORGANIZE CURRENT PLAYLIST ################
homeScreen = xbmc.getCondVisibility('Window.IsActive(home)') homeScreen = xbmc.getCondVisibility('Window.IsActive(home)')
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) playlist = self.pl.playlist
startPos = max(playlist.getposition(), 0) # Can return -1 startPos = max(playlist.getposition(), 0) # Can return -1
sizePlaylist = playlist.size() sizePlaylist = playlist.size()
self.currentPosition = startPos self.currentPosition = startPos

View file

@ -8,7 +8,6 @@ from urllib import urlencode
import xbmc import xbmc
import embydb_functions as embydb import embydb_functions as embydb
import read_embyserver as embyserver
import utils import utils
import playbackutils import playbackutils
import PlexFunctions import PlexFunctions
@ -19,95 +18,105 @@ import PlexAPI
@utils.logging @utils.logging
class Playlist(): class Playlist():
"""
Initiate with Playlist(typus='video' or 'music')
"""
def __init__(self): def __init__(self, typus=None):
self.userid = utils.window('currUserId') self.userid = utils.window('currUserId')
self.server = utils.window('pms_server') 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 __initiatePlaylist(self, itemids):
playlist = None
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))
with embydb.GetEmbyDB() as emby_db: with embydb.GetEmbyDB() as emby_db:
for itemid in itemids: for itemid in itemids:
embydb_item = emby_db.getItem_byId(itemid) embydb_item = emby_db.getItem_byId(itemid)
try: try:
dbid = embydb_item[0]
mediatype = embydb_item[4] mediatype = embydb_item[4]
except TypeError: except TypeError:
# Item is not found in our database, add item manually self.logMsg('Couldnt find item %s in Kodi db' % itemid, 1)
self.logMsg("Item was not found in the database, manually "
"adding item.", 1)
item = PlexFunctions.GetPlexMetadata(itemid) item = PlexFunctions.GetPlexMetadata(itemid)
if item is None or item == 401: if item in (None, 401):
self.logMsg('Could not download itemid %s' continue
% itemid, -1) if PlexAPI.API(item[0]).getType() == 'track':
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
self.logMsg('Music playlist initiated', 1)
else: else:
self.addtoPlaylist_xbmc(playlist, item)
else:
# Add to playlist
self.addtoPlaylist(dbid, mediatype)
self.logMsg("Adding %s to playlist." % itemid, 1)
if not started:
started = True
player.play(playlist)
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) 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: for itemid in itemids:
self.logMsg("Adding %s to playlist." % itemid, 1)
embydb_item = emby_db.getItem_byId(itemid) embydb_item = emby_db.getItem_byId(itemid)
try: try:
dbid = embydb_item[0] dbid = embydb_item[0]
mediatype = embydb_item[4] mediatype = embydb_item[4]
except TypeError: except TypeError:
self.logMsg('item %s not found in Kodi DB, add manually' self.logMsg('Couldnt find item %s in Kodi db' % itemid, 1)
% itemid, 1) item = PlexFunctions.GetPlexMetadata(itemid)
item = self.emby.getItem(itemid) if item in (None, 401):
self.addtoPlaylist_xbmc(playlist, item) self.logMsg('Could not download itemid %s'
% itemid, -1)
else:
self.addtoPlaylist_xbmc(self.playlist, item)
else: else:
# Add to playlist # Add to playlist
self.addtoPlaylist(dbid, mediatype) self.addtoPlaylist(dbid, mediatype)
if started is False and startPlayer is True:
started = True
xbmc.Player().play(self.playlist)
self.logMsg("Adding %s to playlist." % itemid, 1) 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):
self.logMsg("---*** ADD TO PLAYLIST ***---", 1)
self.logMsg("Items: %s" % itemids, 1)
self.__initiatePlaylist(itemids)
self.__addToPlaylist(itemids, startPlayer=True)
self.verifyPlaylist() self.verifyPlaylist()
embycursor.close()
return playlist
def addtoPlaylist(self, dbid=None, mediatype=None, url=None): def addtoPlaylist(self, dbid=None, mediatype=None, url=None):
"""
mediatype: Kodi type: 'movie', 'episode', 'musicvideo', 'artist',
'album', 'song', 'genre'
"""
pl = { pl = {
@ -116,17 +125,17 @@ class Playlist():
'method': "Playlist.Add", 'method': "Playlist.Add",
'params': { 'params': {
'playlistid': 1 'playlistid': self.playlist.getPlayListId()
} }
} }
if dbid is not None: if dbid is not None:
pl['params']['item'] = {'%sid' % mediatype: int(dbid)} pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype):
int(dbid)}
else: else:
pl['params']['item'] = {'file': url} pl['params']['item'] = {'file': url}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
def addtoPlaylist_xbmc(self, playlist, item): def addtoPlaylist_xbmc(self, item):
API = PlexAPI.API(item[0]) API = PlexAPI.API(item[0])
params = { params = {
'mode': "play", 'mode': "play",
@ -140,7 +149,7 @@ class Playlist():
listitem = API.CreateListItemFromPlexItem() listitem = API.CreateListItemFromPlexItem()
playbackutils.PlaybackUtils(item[0]).setArtwork(listitem) 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): def insertintoPlaylist(self, position, dbid=None, mediatype=None, url=None):
@ -151,12 +160,13 @@ class Playlist():
'method': "Playlist.Insert", 'method': "Playlist.Insert",
'params': { 'params': {
'playlistid': 1, 'playlistid': self.playlist.getPlayListId(),
'position': position 'position': position
} }
} }
if dbid is not None: if dbid is not None:
pl['params']['item'] = {'%sid' % mediatype: int(dbid)} pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype):
int(dbid)}
else: else:
pl['params']['item'] = {'file': url} pl['params']['item'] = {'file': url}
@ -171,7 +181,7 @@ class Playlist():
'method': "Playlist.GetItems", 'method': "Playlist.GetItems",
'params': { 'params': {
'playlistid': 1, 'playlistid': self.playlist.getPlayListId(),
'properties': ['title', 'file'] 'properties': ['title', 'file']
} }
} }
@ -186,7 +196,7 @@ class Playlist():
'method': "Playlist.Remove", 'method': "Playlist.Remove",
'params': { 'params': {
'playlistid': 1, 'playlistid': self.playlist.getPlayListId(),
'position': position 'position': position
} }
} }