def run(self): threadStopped = self.threadStopped threadSuspended = self.threadSuspended log.info("----===## Starting PlayQueue client ##===----") # Initialize the playqueues, if Kodi already got items in them for playqueue in self.playqueues: for i, item in enumerate(PL.get_kodi_playlist_items(playqueue)): if i == 0: PL.init_Plex_playlist(playqueue, kodi_item=item) else: PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=item) while not threadStopped(): while threadSuspended(): if threadStopped(): break sleep(1000) with lock: for playqueue in self.playqueues: kodi_playqueue = PL.get_kodi_playlist_items(playqueue) if playqueue.old_kodi_pl != kodi_playqueue: # compare old and new playqueue self._compare_playqueues(playqueue, kodi_playqueue) playqueue.old_kodi_pl = list(kodi_playqueue) sleep(50) log.info("----===## PlayQueue client stopped ##===----")
def run(self): thread_stopped = self.thread_stopped thread_suspended = self.thread_suspended log.info("----===## Starting PlayQueue client ##===----") # Initialize the playqueues, if Kodi already got items in them for playqueue in self.playqueues: for i, item in enumerate(PL.get_kodi_playlist_items(playqueue)): if i == 0: PL.init_Plex_playlist(playqueue, kodi_item=item) else: PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=item) while not thread_stopped(): while thread_suspended(): if thread_stopped(): break sleep(1000) with lock: for playqueue in self.playqueues: kodi_playqueue = PL.get_kodi_playlist_items(playqueue) if playqueue.old_kodi_pl != kodi_playqueue: # compare old and new playqueue self._compare_playqueues(playqueue, kodi_playqueue) playqueue.old_kodi_pl = list(kodi_playqueue) # Still sleep a bit so Kodi does not become # unresponsive sleep(10) continue sleep(200) log.info("----===## PlayQueue client stopped ##===----")
def _compare_playqueues(self, playqueue, new): """ Used to poll the Kodi playqueue and update the Plex playqueue if needed """ old = list(playqueue.items) index = list(range(0, len(old))) log.debug('Comparing new Kodi playqueue %s with our play queue %s' % (new, old)) if self.thread_stopped(): # Chances are that we got an empty Kodi playlist due to # Kodi exit return for i, new_item in enumerate(new): if (new_item['file'].startswith('plugin://') and not new_item['file'].startswith(PLUGIN)): # Ignore new media added by other addons continue for j, old_item in enumerate(old): try: if (old_item.file.startswith('plugin://') and not old_item['file'].startswith(PLUGIN)): # Ignore media by other addons continue except (TypeError, AttributeError): # were not passed a filename; ignore pass if new_item.get('id') is None: identical = old_item.file == new_item['file'] else: identical = (old_item.kodi_id == new_item['id'] and old_item.kodi_type == new_item['type']) if j == 0 and identical: del old[j], index[j] break elif identical: log.debug( 'Detected playqueue item %s moved to position %s' % (i + j, i)) PL.move_playlist_item(playqueue, i + j, i) del old[j], index[j] break else: log.debug('Detected new Kodi element at position %s: %s ' % (i, new_item)) if playqueue.ID is None: PL.init_Plex_playlist(playqueue, kodi_item=new_item) else: PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=new_item) for j in range(i, len(index)): index[j] += 1 for i in reversed(index): log.debug('Detected deletion of playqueue element at pos %s' % i) PL.delete_playlist_item_from_PMS(playqueue, i) log.debug('Done comparing playqueues')
def _add_remaining_items_to_playlist(playqueue): """ Adds all but the very first item of the Kodi playlist to the Plex playqueue """ items = js.playlist_get_items(playqueue.playlistid) if not items: LOG.error('Could not retrieve Kodi playlist items') return # Remove first item items.pop(0) try: for i, item in enumerate(items): PL.add_item_to_PMS_playlist(playqueue, i + 1, kodi_item=item) except PL.PlaylistError: LOG.info('Could not build Plex playlist for: %s', items)
def _compare_playqueues(self, playqueue, new): """ Used to poll the Kodi playqueue and update the Plex playqueue if needed """ old = list(playqueue.items) index = list(range(0, len(old))) log.debug('Comparing new Kodi playqueue %s with our play queue %s' % (new, old)) for i, new_item in enumerate(new): for j, old_item in enumerate(old): if self.threadStopped(): # Chances are that we got an empty Kodi playlist due to # Kodi exit return if new_item.get('id') is None: identical = old_item.file == new_item['file'] else: identical = (old_item.kodi_id == new_item['id'] and old_item.kodi_type == new_item['type']) if j == 0 and identical: del old[j], index[j] break elif identical: log.debug('Detected playqueue item %s moved to position %s' % (i+j, i)) PL.move_playlist_item(playqueue, i + j, i) del old[j], index[j] break else: log.debug('Detected new Kodi element at position %s: %s ' % (i, new_item)) if playqueue.ID is None: PL.init_Plex_playlist(playqueue, kodi_item=new_item) else: PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=new_item) for j in range(i, len(index)): index[j] += 1 for i in reversed(index): log.debug('Detected deletion of playqueue element at pos %s' % i) PL.delete_playlist_item_from_PMS(playqueue, i) log.debug('Done comparing playqueues')
def _compare_playqueues(self, playqueue, new): """ Used to poll the Kodi playqueue and update the Plex playqueue if needed """ old = list(playqueue.items) index = list(range(0, len(old))) log.debug('Comparing new Kodi playqueue %s with our play queue %s' % (new, old)) for i, new_item in enumerate(new): for j, old_item in enumerate(old): if self.threadStopped(): # Chances are that we got an empty Kodi playlist due to # Kodi exit return if new_item.get('id') is None: identical = old_item.file == new_item['file'] else: identical = (old_item.kodi_id == new_item['id'] and old_item.kodi_type == new_item['type']) if j == 0 and identical: del old[j], index[j] break elif identical: log.debug( 'Detected playqueue item %s moved to position %s' % (i + j, i)) PL.move_playlist_item(playqueue, i + j, i) del old[j], index[j] break else: log.debug('Detected new Kodi element at position %s: %s ' % (i, new_item)) if playqueue.ID is None: PL.init_Plex_playlist(playqueue, kodi_item=new_item) else: PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=new_item) for j in range(i, len(index)): index[j] += 1 for i in reversed(index): log.debug('Detected deletion of playqueue element at pos %s' % i) PL.delete_playlist_item_from_PMS(playqueue, i) log.debug('Done comparing playqueues')
def _init_existing_kodi_playlist(playqueue): """ Will take the playqueue's kodi_pl with MORE than 1 element and initiate playback (without adding trailers) """ LOG.debug('Kodi playlist size: %s', playqueue.kodi_pl.size()) for i, kodi_item in enumerate(js.playlist_get_items(playqueue.playlistid)): if i == 0: item = PL.init_Plex_playlist(playqueue, kodi_item=kodi_item) else: item = PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=kodi_item) item.force_transcode = state.FORCE_TRANSCODE LOG.debug('Done building Plex playlist from Kodi playlist')
def _compare_playqueues(self, playqueue, new): """ Used to poll the Kodi playqueue and update the Plex playqueue if needed """ old = list(playqueue.items) index = list(range(0, len(old))) LOG.debug('Comparing new Kodi playqueue %s with our play queue %s', new, old) for i, new_item in enumerate(new): if (new_item['file'].startswith('plugin://') and not new_item['file'].startswith(PLUGIN)): # Ignore new media added by other addons continue for j, old_item in enumerate(old): if self.stopped(): # Chances are that we got an empty Kodi playlist due to # Kodi exit return try: if (old_item.file.startswith('plugin://') and not old_item.file.startswith(PLUGIN)): # Ignore media by other addons continue except AttributeError: # were not passed a filename; ignore pass if 'id' in new_item: identical = (old_item.kodi_id == new_item['id'] and old_item.kodi_type == new_item['type']) else: try: plex_id = REGEX.findall(new_item['file'])[0] except IndexError: LOG.debug('Comparing paths directly as a fallback') identical = old_item.file == new_item['file'] else: identical = plex_id == old_item.plex_id if j == 0 and identical: del old[j], index[j] break elif identical: LOG.debug('Detected playqueue item %s moved to position %s', i + j, i) PL.move_playlist_item(playqueue, i + j, i) del old[j], index[j] break else: LOG.debug('Detected new Kodi element at position %s: %s ', i, new_item) try: if playqueue.id is None: PL.init_Plex_playlist(playqueue, kodi_item=new_item) else: PL.add_item_to_PMS_playlist(playqueue, i, kodi_item=new_item) except PL.PlaylistError: # Could not add the element pass except IndexError: # This is really a hack - happens when using Addon Paths # and repeatedly starting the same element. Kodi will then # not pass kodi id nor file path AND will also not # start-up playback. Hence kodimonitor kicks off playback. # Also see kodimonitor.py - _playlist_onadd() pass else: for j in range(i, len(index)): index[j] += 1 for i in reversed(index): if self.stopped(): # Chances are that we got an empty Kodi playlist due to # Kodi exit return LOG.debug('Detected deletion of playqueue element at pos %s', i) PL.delete_playlist_item_from_PMS(playqueue, i) LOG.debug('Done comparing playqueues')