def process_indirect(key, offset, resolve=True): """ Called e.g. for Plex "Play later" - Plex items where we need to fetch an additional xml for the actual playurl. In the PMS metadata, indirect="1" is set. Will release default.py with setResolvedUrl Set resolve to False if playback should be kicked off directly, not via setResolvedUrl """ LOG.info('process_indirect called with key: %s, offset: %s', key, offset) result = Playback_Successful() if key.startswith('http') or key.startswith('{server}'): xml = DU().downloadUrl(key) elif key.startswith('/system/services'): xml = DU().downloadUrl('http://node.plexapp.com:32400%s' % key) else: xml = DU().downloadUrl('{server}%s' % key) try: xml[0].attrib except (TypeError, IndexError, AttributeError): LOG.error('Could not download PMS metadata') if resolve is True: # Release default.py pickle_me(result) return if offset != '0': offset = int(v.PLEX_TO_KODI_TIMEFACTOR * float(offset)) # Todo: implement offset api = API(xml[0]) listitem = PKC_ListItem() api.CreateListItemFromPlexItem(listitem) playqueue = PQ.get_playqueue_from_type( v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()]) playqueue.clear() item = PL.Playlist_Item() item.xml = xml[0] item.offset = int(offset) item.plex_type = v.PLEX_TYPE_CLIP item.playmethod = 'DirectStream' # Need to get yet another xml to get the final playback url xml = DU().downloadUrl('http://node.plexapp.com:32400%s' % xml[0][0][0].attrib['key']) try: xml[0].attrib except (TypeError, IndexError, AttributeError): LOG.error('Could not download last xml for playurl') if resolve is True: # Release default.py pickle_me(result) return playurl = xml[0].attrib['key'] item.file = playurl listitem.setPath(tryEncode(playurl)) playqueue.items.append(item) if resolve is True: result.listitem = listitem pickle_me(result) else: thread = Thread(target=Player().play, args={ 'item': tryEncode(playurl), 'listitem': listitem }) thread.setDaemon(True) LOG.info('Done initializing PKC playback, starting Kodi player') thread.start()
def PlayBackStart(self, data): """ Called whenever playback is started. Example data: { u'item': {u'type': u'movie', u'title': u''}, u'player': {u'playerid': 1, u'speed': 1} } Unfortunately when using Widgets, Kodi doesn't tell us shit """ # Get the type of media we're playing try: kodi_type = data['item']['type'] playerid = data['player']['playerid'] except (TypeError, KeyError): LOG.info('Aborting playback report - item invalid for updates %s', data) return if playerid == -1: # Kodi might return -1 for "last player" try: playerid = js.get_player_ids()[0] except IndexError: LOG.error('Could not retreive active player - aborting') return # Remember that this player has been active state.ACTIVE_PLAYERS.append(playerid) playqueue = PQ.PLAYQUEUES[playerid] info = js.get_player_props(playerid) json_item = js.get_item(playerid) path = json_item.get('file') pos = info['position'] if info['position'] != -1 else 0 LOG.debug('Detected position %s for %s', pos, playqueue) status = state.PLAYER_STATES[playerid] try: item = playqueue.items[pos] except IndexError: try: kodi_id, kodi_type, plex_id, plex_type = self._get_ids( json_item) except RuntimeError: return LOG.info('Need to initialize Plex and PKC playqueue') try: if plex_id: item = PL.init_Plex_playlist(playqueue, plex_id=plex_id) else: item = PL.init_Plex_playlist(playqueue, kodi_item={ 'id': kodi_id, 'type': kodi_type, 'file': path }) except PL.PlaylistError: LOG.info('Could not initialize our playlist') # Avoid errors item = PL.Playlist_Item() # Set the Plex container key (e.g. using the Plex playqueue) container_key = None if info['playlistid'] != -1: # -1 is Kodi's answer if there is no playlist container_key = PQ.PLAYQUEUES[playerid].id if container_key is not None: container_key = '/playQueues/%s' % container_key elif plex_id is not None: container_key = '/library/metadata/%s' % plex_id LOG.debug('Set the Plex container_key to: %s', container_key) else: kodi_id = item.kodi_id kodi_type = item.kodi_type plex_id = item.plex_id plex_type = item.plex_type if playqueue.id: container_key = '/playQueues/%s' % playqueue.id else: container_key = '/library/metadata/%s' % plex_id status.update(info) status['container_key'] = container_key status['file'] = path status['kodi_id'] = kodi_id status['kodi_type'] = kodi_type status['plex_id'] = plex_id status['plex_type'] = plex_type status['playmethod'] = item.playmethod status['playcount'] = item.playcount LOG.debug('Set the player state: %s', status)