Clean up playlist.py

This commit is contained in:
tomkat83 2016-09-01 19:53:04 +02:00
parent 26652a48c2
commit daf40873c0

View file

@ -2,6 +2,7 @@
############################################################################### ###############################################################################
import logging
import json import json
from urllib import urlencode from urllib import urlencode
from threading import Lock from threading import Lock
@ -10,13 +11,17 @@ from functools import wraps
import xbmc import xbmc
import embydb_functions as embydb import embydb_functions as embydb
import utils from utils import window, tryEncode
import playbackutils import playbackutils
import PlexFunctions import PlexFunctions
import PlexAPI import PlexAPI
############################################################################### ###############################################################################
log = logging.getLogger("PLEX."+__name__)
###############################################################################
class lockMethod: class lockMethod:
""" """
@ -37,7 +42,6 @@ class lockMethod:
return wrapper return wrapper
@utils.logging
class Playlist(): class Playlist():
""" """
Initiate with Playlist(typus='video' or 'music') Initiate with Playlist(typus='video' or 'music')
@ -59,23 +63,23 @@ class Playlist():
# Borg # Borg
self.__dict__ = self._shared_state self.__dict__ = self._shared_state
self.userid = utils.window('currUserId') self.userid = window('currUserId')
self.server = utils.window('pms_server') self.server = window('pms_server')
# Construct the Kodi playlist instance # Construct the Kodi playlist instance
if self.typus == typus: if self.typus == typus:
return return
if typus == 'video': if typus == 'video':
self.playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) self.playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
self.typus = 'video' self.typus = 'video'
self.logMsg('Initiated video playlist', 1) log.info('Initiated video playlist')
elif typus == 'music': elif typus == 'music':
self.playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC) self.playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
self.typus = 'music' self.typus = 'music'
self.logMsg('Initiated music playlist', 1) log.info('Initiated music playlist')
else: else:
self.playlist = None self.playlist = None
self.typus = None self.typus = None
self.logMsg('Empty playlist initiated', 1) log.info('Empty playlist initiated')
if self.playlist is not None: if self.playlist is not None:
self.playlistId = self.playlist.getPlayListId() self.playlistId = self.playlist.getPlayListId()
@ -116,7 +120,7 @@ class Playlist():
""" """
Empties current Kodi playlist and associated variables Empties current Kodi playlist and associated variables
""" """
self.logMsg('Clearing playlist', 1) log.info('Clearing playlist')
self.playlist.clear() self.playlist.clear()
self.items = [] self.items = []
self.queueId = None self.queueId = None
@ -124,7 +128,7 @@ class Playlist():
self.guid = None self.guid = None
def _initiatePlaylist(self): def _initiatePlaylist(self):
self.logMsg('Initiating playlist', 1) log.info('Initiating playlist')
playlist = None playlist = None
with embydb.GetEmbyDB() as emby_db: with embydb.GetEmbyDB() as emby_db:
for item in self.items: for item in self.items:
@ -133,29 +137,28 @@ class Playlist():
try: try:
mediatype = embydb_item[4] mediatype = embydb_item[4]
except TypeError: except TypeError:
self.logMsg('Couldnt find item %s in Kodi db' log.info('Couldnt find item %s in Kodi db' % itemid)
% itemid, 1)
item = PlexFunctions.GetPlexMetadata(itemid) item = PlexFunctions.GetPlexMetadata(itemid)
if item in (None, 401): if item in (None, 401):
self.logMsg('Couldnt find item %s on PMS, trying next' log.info('Couldnt find item %s on PMS, trying next'
% itemid, 1) % itemid)
continue continue
if PlexAPI.API(item[0]).getType() == 'track': if PlexAPI.API(item[0]).getType() == 'track':
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC) playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
self.logMsg('Music playlist initiated', 1) log.info('Music playlist initiated')
self.typus = 'music' self.typus = 'music'
else: else:
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
self.logMsg('Video playlist initiated', 1) log.info('Video playlist initiated')
self.typus = 'video' self.typus = 'video'
else: else:
if mediatype == 'song': if mediatype == 'song':
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC) playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
self.logMsg('Music playlist initiated', 1) log.info('Music playlist initiated')
self.typus = 'music' self.typus = 'music'
else: else:
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
self.logMsg('Video playlist initiated', 1) log.info('Video playlist initiated')
self.typus = 'video' self.typus = 'video'
break break
self.playlist = playlist self.playlist = playlist
@ -173,18 +176,17 @@ class Playlist():
kodiId = embydb_item[0] kodiId = embydb_item[0]
mediatype = embydb_item[4] mediatype = embydb_item[4]
except TypeError: except TypeError:
self.logMsg('Couldnt find item %s in Kodi db' % plexId, 1) log.info('Couldnt find item %s in Kodi db' % plexId)
xml = PlexFunctions.GetPlexMetadata(plexId) xml = PlexFunctions.GetPlexMetadata(plexId)
if xml in (None, 401): if xml in (None, 401):
self.logMsg('Could not download plexId %s' log.error('Could not download plexId %s' % plexId)
% plexId, -1)
else: else:
self.logMsg('Downloaded xml metadata, adding now', 1) log.debug('Downloaded xml metadata, adding now')
self._addtoPlaylist_xbmc(xml[0]) self._addtoPlaylist_xbmc(xml[0])
else: else:
# Add to playlist # Add to playlist
self.logMsg("Adding %s PlexId %s, KodiId %s to playlist." log.debug("Adding %s PlexId %s, KodiId %s to playlist."
% (mediatype, plexId, kodiId), 1) % (mediatype, plexId, kodiId))
self._addtoPlaylist(kodiId, mediatype) self._addtoPlaylist(kodiId, mediatype)
# Add the kodiId # Add the kodiId
if kodiId is not None: if kodiId is not None:
@ -196,8 +198,8 @@ class Playlist():
if startpos is not None: if startpos is not None:
self.player.play(self.playlist, startpos=startpos) self.player.play(self.playlist, startpos=startpos)
else: else:
self.logMsg('Never received a starting item for playlist, ' log.info('Never received a starting item for playlist, '
'starting with the first entry', 1) 'starting with the first entry')
self.player.play(self.playlist) self.player.play(self.playlist)
@lockMethod.decorate @lockMethod.decorate
@ -215,29 +217,29 @@ class Playlist():
id as a string id as a string
offset: First item's time offset to play in Kodi time (an int) offset: First item's time offset to play in Kodi time (an int)
""" """
self.logMsg("---*** PLAY ALL ***---", 1) log.info("---*** PLAY ALL ***---")
self.logMsg('Startitem: %s, offset: %s, items: %s' log.debug('Startitem: %s, offset: %s, items: %s'
% (startitem, offset, items), 1) % (startitem, offset, items))
self.items = items self.items = items
if self.playlist is None: if self.playlist is None:
self._initiatePlaylist() self._initiatePlaylist()
if self.playlist is None: if self.playlist is None:
self.logMsg('Could not create playlist, abort', -1) log.error('Could not create playlist, abort')
return return
utils.window('plex_customplaylist', value="true") window('plex_customplaylist', value="true")
if offset != 0: if offset != 0:
# Seek to the starting position # Seek to the starting position
utils.window('plex_customplaylist.seektime', str(offset)) window('plex_customplaylist.seektime', str(offset))
self._processItems(startitem, startPlayer=True) self._processItems(startitem, startPlayer=True)
# Log playlist # Log playlist
self._verifyPlaylist() self._verifyPlaylist()
self.logMsg('Internal playlist: %s' % self.items, 2) log.debug('Internal playlist: %s' % self.items)
@lockMethod.decorate @lockMethod.decorate
def modifyPlaylist(self, itemids): def modifyPlaylist(self, itemids):
self.logMsg("---*** ADD TO PLAYLIST ***---", 1) log.info("---*** MODIFY PLAYLIST ***---")
self.logMsg("Items: %s" % itemids, 1) log.debug("Items: %s" % itemids)
self._initiatePlaylist(itemids) self._initiatePlaylist(itemids)
self._processItems(itemids, startPlayer=True) self._processItems(itemids, startPlayer=True)
@ -262,11 +264,10 @@ class Playlist():
} }
} }
if dbid is not None: if dbid is not None:
pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype): pl['params']['item'] = {'%sid' % tryEncode(mediatype): int(dbid)}
int(dbid)}
else: else:
pl['params']['item'] = {'file': url} pl['params']['item'] = {'file': url}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
def _addtoPlaylist_xbmc(self, item): def _addtoPlaylist_xbmc(self, item):
API = PlexAPI.API(item) API = PlexAPI.API(item)
@ -285,26 +286,26 @@ class Playlist():
self.playlist.add(playurl, listitem) self.playlist.add(playurl, listitem)
@lockMethod.decorate @lockMethod.decorate
def insertintoPlaylist(self, position, dbid=None, mediatype=None, url=None): def insertintoPlaylist(self,
position,
dbid=None,
mediatype=None,
url=None):
pl = { pl = {
'jsonrpc': "2.0", 'jsonrpc': "2.0",
'id': 1, 'id': 1,
'method': "Playlist.Insert", 'method': "Playlist.Insert",
'params': { 'params': {
'playlistid': self.playlistId, 'playlistid': self.playlistId,
'position': position 'position': position
} }
} }
if dbid is not None: if dbid is not None:
pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype): pl['params']['item'] = {'%sid' % tryEncode(mediatype): int(dbid)}
int(dbid)}
else: else:
pl['params']['item'] = {'file': url} pl['params']['item'] = {'file': url}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
@lockMethod.decorate @lockMethod.decorate
def verifyPlaylist(self): def verifyPlaylist(self):
@ -312,30 +313,25 @@ class Playlist():
def _verifyPlaylist(self): def _verifyPlaylist(self):
pl = { pl = {
'jsonrpc': "2.0", 'jsonrpc': "2.0",
'id': 1, 'id': 1,
'method': "Playlist.GetItems", 'method': "Playlist.GetItems",
'params': { 'params': {
'playlistid': self.playlistId, 'playlistid': self.playlistId,
'properties': ['title', 'file'] 'properties': ['title', 'file']
} }
} }
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
@lockMethod.decorate @lockMethod.decorate
def removefromPlaylist(self, position): def removefromPlaylist(self, position):
pl = { pl = {
'jsonrpc': "2.0", 'jsonrpc': "2.0",
'id': 1, 'id': 1,
'method': "Playlist.Remove", 'method': "Playlist.Remove",
'params': { 'params': {
'playlistid': self.playlistId, 'playlistid': self.playlistId,
'position': position 'position': position
} }
} }
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) log.debug(xbmc.executeJSONRPC(json.dumps(pl)))