PlexKodiConnect/resources/lib/context_entry.py

170 lines
5.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2016-10-23 02:15:10 +11:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
2018-02-03 22:45:48 +11:00
from logging import getLogger
2018-05-05 03:03:27 +10:00
import xbmc
import xbmcgui
2016-10-23 02:15:10 +11:00
2018-06-22 03:24:37 +10:00
from . import context
from . import plexdb_functions as plexdb
from . import utils
from . import plex_functions as PF
from .plex_api import API
from . import playqueue as PQ
from . import variables as v
from . import state
2016-10-23 02:15:10 +11:00
###############################################################################
2018-06-22 03:24:37 +10:00
LOG = getLogger('PLEX.context_entry')
2016-10-23 02:15:10 +11:00
OPTIONS = {
2018-06-22 03:24:37 +10:00
'Refresh': utils.lang(30410),
'Delete': utils.lang(30409),
'Addon': utils.lang(30408),
# 'AddFav': utils.lang(30405),
# 'RemoveFav': utils.lang(30406),
# 'RateSong': utils.lang(30407),
'Transcode': utils.lang(30412),
'PMS_Play': utils.lang(30415), # Use PMS to start playback
'Extras': utils.lang(30235)
2016-10-23 02:15:10 +11:00
}
###############################################################################
class ContextMenu(object):
2018-02-03 22:45:48 +11:00
"""
Class initiated if user opens "Plex options" on a PLEX item using the Kodi
context menu
"""
2016-10-23 02:15:10 +11:00
_selected_option = None
2018-02-16 05:47:01 +11:00
def __init__(self, kodi_id=None, kodi_type=None):
2018-02-03 22:45:48 +11:00
"""
Simply instantiate with ContextMenu() - no need to call any methods
"""
2018-02-16 05:47:01 +11:00
self.kodi_id = kodi_id
self.kodi_type = kodi_type
2018-02-03 22:45:48 +11:00
self.plex_id = self._get_plex_id(self.kodi_id, self.kodi_type)
if self.kodi_type:
self.plex_type = v.PLEX_TYPE_FROM_KODI_TYPE[self.kodi_type]
else:
self.plex_type = None
LOG.debug("Found plex_id: %s plex_type: %s",
self.plex_id, self.plex_type)
if not self.plex_id:
2016-10-23 02:15:10 +11:00
return
2018-05-05 03:03:27 +10:00
xml = PF.GetPlexMetadata(self.plex_id)
try:
xml[0].attrib
except (TypeError, IndexError, KeyError):
self.api = None
else:
self.api = API(xml[0])
2016-10-23 02:15:10 +11:00
if self._select_menu():
self._action_menu()
if self._selected_option in (OPTIONS['Delete'],
OPTIONS['Refresh']):
2018-02-01 23:55:40 +11:00
LOG.info("refreshing container")
2018-05-05 03:03:27 +10:00
xbmc.sleep(500)
xbmc.executebuiltin('Container.Refresh')
2018-02-03 22:45:48 +11:00
@staticmethod
def _get_plex_id(kodi_id, kodi_type):
2018-05-05 03:03:27 +10:00
plex_id = xbmc.getInfoLabel('ListItem.Property(plexid)') or None
2018-02-03 22:45:48 +11:00
if not plex_id and kodi_id and kodi_type:
with plexdb.Get_Plex_DB() as plexcursor:
2018-02-03 22:45:48 +11:00
item = plexcursor.getItem_byKodiId(kodi_id, kodi_type)
2016-10-23 02:15:10 +11:00
try:
2018-02-03 22:45:48 +11:00
plex_id = item[0]
2016-10-23 02:15:10 +11:00
except TypeError:
2018-02-03 22:45:48 +11:00
LOG.info('Could not get the Plex id for context menu')
return plex_id
2016-10-23 02:15:10 +11:00
def _select_menu(self):
2018-02-03 22:45:48 +11:00
"""
Display select dialog
"""
2016-10-23 02:15:10 +11:00
options = []
# if user uses direct paths, give option to initiate playback via PMS
2018-05-05 03:03:27 +10:00
if self.api and self.api.extras():
options.append(OPTIONS['Extras'])
2018-02-03 22:45:48 +11:00
if state.DIRECT_PATHS and self.kodi_type in v.KODI_VIDEOTYPES:
options.append(OPTIONS['PMS_Play'])
2018-02-03 22:45:48 +11:00
if self.kodi_type in v.KODI_VIDEOTYPES:
2016-11-07 01:37:22 +11:00
options.append(OPTIONS['Transcode'])
2018-05-17 13:01:40 +10:00
2016-10-23 02:15:10 +11:00
# Delete item, only if the Plex Home main user is logged in
2018-06-22 03:24:37 +10:00
if (utils.window('plex_restricteduser') != 'true' and
utils.window('plex_allows_mediaDeletion') == 'true'):
2016-10-23 02:15:10 +11:00
options.append(OPTIONS['Delete'])
# Addon settings
options.append(OPTIONS['Addon'])
context_menu = context.ContextMenu(
2018-05-17 03:33:43 +10:00
"script-plex-context.xml",
utils.try_encode(v.ADDON_PATH),
2018-02-03 22:45:48 +11:00
"default",
"1080i")
2016-10-23 02:15:10 +11:00
context_menu.set_options(options)
context_menu.doModal()
if context_menu.is_selected():
self._selected_option = context_menu.get_selected()
return self._selected_option
def _action_menu(self):
2018-02-03 22:45:48 +11:00
"""
Do whatever the user selected to do
"""
2016-10-23 02:15:10 +11:00
selected = self._selected_option
if selected == OPTIONS['Transcode']:
2018-02-03 22:45:48 +11:00
state.FORCE_TRANSCODE = True
2016-11-07 01:37:22 +11:00
self._PMS_play()
elif selected == OPTIONS['PMS_Play']:
self._PMS_play()
2018-05-05 03:03:27 +10:00
elif selected == OPTIONS['Extras']:
self._extras()
2018-05-17 13:01:40 +10:00
2016-10-23 02:15:10 +11:00
elif selected == OPTIONS['Addon']:
2018-05-05 03:03:27 +10:00
xbmc.executebuiltin(
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
2016-10-23 02:15:10 +11:00
elif selected == OPTIONS['Delete']:
self._delete_item()
def _delete_item(self):
2018-02-03 22:45:48 +11:00
"""
Delete item on PMS
"""
2016-10-23 02:15:10 +11:00
delete = True
2018-06-22 03:24:37 +10:00
if utils.settings('skipContextMenu') != "true":
if not utils.dialog("yesno", heading="{plex}", line1=utils.lang(33041)):
2018-02-03 22:45:48 +11:00
LOG.info("User skipped deletion for: %s", self.plex_id)
2016-10-23 02:15:10 +11:00
delete = False
if delete:
2018-02-03 22:45:48 +11:00
LOG.info("Deleting Plex item with id %s", self.plex_id)
2018-05-05 03:03:27 +10:00
if PF.delete_item_from_pms(self.plex_id) is False:
2018-06-22 03:24:37 +10:00
utils.dialog("ok", heading="{plex}", line1=utils.lang(30414))
def _PMS_play(self):
"""
For using direct paths: Initiates playback using the PMS
"""
2018-02-05 03:36:18 +11:00
playqueue = PQ.get_playqueue_from_type(
v.KODI_PLAYLIST_TYPE_FROM_KODI_TYPE[self.kodi_type])
playqueue.clear()
2018-02-03 22:45:48 +11:00
state.CONTEXT_MENU_PLAY = True
handle = self.api.path(force_first_media=False, force_addon=True)
2018-05-05 03:03:27 +10:00
xbmc.executebuiltin('RunPlugin(%s)' % handle)
def _extras(self):
"""
Displays a list of elements for all the extras of the Plex element
"""
handle = ('plugin://plugin.video.plexkodiconnect?mode=extras&plex_id=%s'
% self.plex_id)
if xbmcgui.getCurrentWindowId() == 10025:
# Video Window
xbmc.executebuiltin('Container.Update(\"%s\")' % handle)
else:
xbmc.executebuiltin('ActivateWindow(videos, \"%s\")' % handle)