PlexKodiConnect/resources/lib/playstrm.py

91 lines
3.4 KiB
Python
Raw Normal View History

2019-03-26 03:15:18 +11:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
from logging import getLogger
2019-05-26 04:49:29 +10:00
from . import app, utils, json_rpc, variables as v, playqueue as PQ
2019-03-26 03:15:18 +11:00
LOG = getLogger('PLEX.playstrm')
class PlayStrmException(Exception):
"""
Any Exception associated with playstrm
"""
pass
class PlayStrm(object):
'''
Workflow: Strm that calls our webservice in database. When played, the
webserivce returns a dummy file to play. Meanwhile, PlayStrm adds the real
listitems for items to play to the playlist.
'''
2019-04-29 02:03:20 +10:00
def __init__(self, params):
LOG.debug('Starting PlayStrm with params: %s', params)
2019-03-26 03:15:18 +11:00
self.plex_id = utils.cast(int, params['plex_id'])
self.plex_type = params.get('plex_type')
if params.get('synched') and params['synched'].lower() == 'false':
self.synched = False
else:
self.synched = True
self.kodi_id = utils.cast(int, params.get('kodi_id'))
self.kodi_type = params.get('kodi_type')
2019-04-29 02:03:20 +10:00
self.force_transcode = params.get('transcode') == 'true'
2019-04-07 21:33:00 +10:00
if app.PLAYSTATE.audioplaylist:
2019-04-06 19:26:01 +11:00
LOG.debug('Audio playlist detected')
self.playqueue = PQ.get_playqueue_from_type(v.KODI_TYPE_AUDIO)
2019-03-26 03:15:18 +11:00
else:
2019-04-06 19:26:01 +11:00
LOG.debug('Video playlist detected')
self.playqueue = PQ.get_playqueue_from_type(v.KODI_TYPE_VIDEO)
2019-03-26 03:15:18 +11:00
2019-04-29 02:03:20 +10:00
def __unicode__(self):
2019-03-26 03:15:18 +11:00
return ("{{"
"'plex_id': {self.plex_id}, "
"'plex_type': '{self.plex_type}', "
"'kodi_id': {self.kodi_id}, "
"'kodi_type': '{self.kodi_type}', "
2019-04-29 02:03:20 +10:00
"}}").format(self=self)
2019-03-26 03:15:18 +11:00
2019-04-29 02:03:20 +10:00
def __str__(self):
return unicode(self).encode('utf-8')
__repr__ = __str__
2019-03-26 03:15:18 +11:00
def play(self, start_position=None, delayed=True):
'''
2019-04-29 02:03:20 +10:00
Create and add a single listitem to the Kodi playlist, potentially
with trailers and different file-parts
2019-03-26 03:15:18 +11:00
'''
2019-04-13 22:39:11 +10:00
LOG.debug('play called with start_position %s, delayed %s',
start_position, delayed)
2019-04-29 02:03:20 +10:00
LOG.debug('Kodi playlist BEFORE: %s',
json_rpc.playlist_get_items(self.playqueue.playlistid))
self.playqueue.init(self.plex_id,
plex_type=self.plex_type,
position=start_position,
synched=self.synched,
force_transcode=self.force_transcode)
2019-03-26 03:15:18 +11:00
LOG.info('Initiating play for %s', self)
2019-04-29 02:03:20 +10:00
LOG.debug('Kodi playlist AFTER: %s',
json_rpc.playlist_get_items(self.playqueue.playlistid))
2019-03-26 03:15:18 +11:00
if not delayed:
2019-04-29 02:03:20 +10:00
self.playqueue.start_playback(start_position)
return self.playqueue.index
2019-03-26 03:15:18 +11:00
def play_folder(self, position=None):
'''
When an entire queue is requested, If requested from Kodi, kodi_type is
provided, add as Kodi would, otherwise queue playlist items using strm
links to setup playback later.
'''
2019-04-29 02:03:20 +10:00
start_position = position or max(self.playqueue.kodi_pl.size(), 0)
index = start_position + 1
LOG.info('Play folder plex_id %s, index: %s', self.plex_id, index)
2019-05-26 04:49:29 +10:00
item = PQ.PlaylistItem(plex_id=self.plex_id,
2019-04-29 02:03:20 +10:00
plex_type=self.plex_type,
kodi_id=self.kodi_id,
kodi_type=self.kodi_type)
self.playqueue.add_item(item, index)
index += 1
return index - 1