2017-01-03 00:07:24 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
###############################################################################
|
|
|
|
import logging
|
|
|
|
from threading import Thread
|
|
|
|
from urlparse import parse_qsl
|
|
|
|
|
|
|
|
from PKC_listitem import PKC_ListItem
|
|
|
|
from pickler import pickle_me, Playback_Successful
|
|
|
|
from playbackutils import PlaybackUtils
|
|
|
|
from utils import window
|
2017-01-25 02:04:42 +11:00
|
|
|
from PlexFunctions import GetPlexMetadata
|
2017-01-03 00:07:24 +11:00
|
|
|
from PlexAPI import API
|
|
|
|
from playqueue import lock
|
2017-01-25 02:04:42 +11:00
|
|
|
import variables as v
|
2017-01-03 00:07:24 +11:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class Playback_Starter(Thread):
|
|
|
|
"""
|
|
|
|
Processes new plays
|
|
|
|
"""
|
|
|
|
def __init__(self, callback=None):
|
|
|
|
self.mgr = callback
|
|
|
|
self.playqueue = self.mgr.playqueue
|
|
|
|
Thread.__init__(self)
|
|
|
|
|
|
|
|
def process_play(self, plex_id, kodi_id=None):
|
|
|
|
"""
|
|
|
|
Processes Kodi playback init for ONE item
|
|
|
|
"""
|
|
|
|
log.info("Process_play called with plex_id %s, kodi_id %s"
|
|
|
|
% (plex_id, kodi_id))
|
|
|
|
if window('plex_authenticated') != "true":
|
|
|
|
log.error('Not yet authenticated for PMS, abort starting playback')
|
|
|
|
# Todo: Warn user with dialog
|
|
|
|
return
|
|
|
|
xml = GetPlexMetadata(plex_id)
|
2017-01-09 01:40:47 +11:00
|
|
|
try:
|
|
|
|
xml[0].attrib
|
|
|
|
except (TypeError, AttributeError):
|
|
|
|
log.error('Could not get a PMS xml for plex id %s' % plex_id)
|
|
|
|
return
|
2017-01-09 01:43:30 +11:00
|
|
|
api = API(xml[0])
|
2017-01-25 02:04:42 +11:00
|
|
|
if api.getType() == v.PLEX_TYPE_PHOTO:
|
2017-01-03 00:07:24 +11:00
|
|
|
# Photo
|
|
|
|
result = Playback_Successful()
|
|
|
|
listitem = PKC_ListItem()
|
|
|
|
listitem = api.CreateListItemFromPlexItem(listitem)
|
|
|
|
api.AddStreamInfo(listitem)
|
2017-01-09 01:03:41 +11:00
|
|
|
api.set_listitem_artwork(listitem)
|
2017-01-03 00:07:24 +11:00
|
|
|
result.listitem = listitem
|
|
|
|
else:
|
|
|
|
# Video and Music
|
2017-01-10 05:56:57 +11:00
|
|
|
playqueue = self.playqueue.get_playqueue_from_type(
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()])
|
2017-01-03 00:07:24 +11:00
|
|
|
with lock:
|
2017-01-10 05:56:57 +11:00
|
|
|
result = PlaybackUtils(xml, playqueue).play(
|
2017-01-03 00:07:24 +11:00
|
|
|
plex_id,
|
|
|
|
kodi_id,
|
|
|
|
xml.attrib.get('librarySectionUUID'))
|
|
|
|
log.info('Done process_play, playqueues: %s'
|
|
|
|
% self.playqueue.playqueues)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def triage(self, item):
|
|
|
|
mode, params = item.split('?', 1)
|
|
|
|
params = dict(parse_qsl(params))
|
|
|
|
log.debug('Received mode: %s, params: %s' % (mode, params))
|
|
|
|
try:
|
|
|
|
if mode == 'play':
|
|
|
|
result = self.process_play(params.get('id'),
|
|
|
|
params.get('dbid'))
|
|
|
|
elif mode == 'companion':
|
|
|
|
result = self.process_companion()
|
|
|
|
except:
|
|
|
|
log.error('Error encountered for mode %s, params %s'
|
|
|
|
% (mode, params))
|
|
|
|
import traceback
|
|
|
|
log.error(traceback.format_exc())
|
|
|
|
# Let default.py know!
|
|
|
|
pickle_me(None)
|
|
|
|
else:
|
|
|
|
pickle_me(result)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
queue = self.mgr.monitor_kodi_play.playback_queue
|
|
|
|
log.info("----===## Starting Playback_Starter ##===----")
|
|
|
|
while True:
|
|
|
|
item = queue.get()
|
|
|
|
if item is None:
|
|
|
|
# Need to shutdown - initiated by monitor_kodi_play
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.triage(item)
|
|
|
|
queue.task_done()
|
|
|
|
log.info("----===## Playback_Starter stopped ##===----")
|