PlexKodiConnect/resources/lib/context_entry.py

186 lines
6.7 KiB
Python
Raw Normal View History

2016-10-23 02:15:10 +11:00
# -*- coding: utf-8 -*-
###############################################################################
2018-02-03 22:45:48 +11:00
from logging import getLogger
2016-10-23 02:15:10 +11:00
2018-02-03 22:45:48 +11:00
from xbmc import getInfoLabel, sleep, executebuiltin, getCondVisibility
from xbmcaddon import Addon
2016-10-23 02:15:10 +11:00
import plexdb_functions as plexdb
2018-02-03 22:45:48 +11:00
from utils import window, settings, dialog, language as lang
2016-10-23 02:15:10 +11:00
from dialogs import context
from PlexFunctions import delete_item_from_pms
2018-02-05 03:36:18 +11:00
import playqueue as PQ
import variables as v
2018-02-03 22:45:48 +11:00
import state
2016-10-23 02:15:10 +11:00
###############################################################################
2018-02-03 22:45:48 +11:00
LOG = getLogger("PLEX." + __name__)
2016-10-23 02:15:10 +11:00
OPTIONS = {
'Refresh': lang(30410),
'Delete': lang(30409),
'Addon': lang(30408),
# 'AddFav': lang(30405),
# 'RemoveFav': lang(30406),
# 'RateSong': lang(30407),
'Transcode': lang(30412),
'PMS_Play': lang(30415) # Use PMS to start playback
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
def __init__(self):
2018-02-03 22:45:48 +11:00
"""
Simply instantiate with ContextMenu() - no need to call any methods
"""
self.kodi_id = getInfoLabel('ListItem.DBID').decode('utf-8')
self.kodi_type = self._get_kodi_type()
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
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-02-03 22:45:48 +11:00
sleep(500)
executebuiltin('Container.Refresh')
@staticmethod
def _get_kodi_type():
kodi_type = getInfoLabel('ListItem.DBTYPE').decode('utf-8')
if not kodi_type:
if getCondVisibility('Container.Content(albums)'):
kodi_type = v.KODI_TYPE_ALBUM
elif getCondVisibility('Container.Content(artists)'):
kodi_type = v.KODI_TYPE_ARTIST
elif getCondVisibility('Container.Content(songs)'):
kodi_type = v.KODI_TYPE_SONG
elif getCondVisibility('Container.Content(pictures)'):
kodi_type = v.KODI_TYPE_PHOTO
2016-10-23 02:15:10 +11:00
else:
2018-02-03 22:45:48 +11:00
LOG.info("kodi_type is unknown")
kodi_type = None
return kodi_type
@staticmethod
def _get_plex_id(kodi_id, kodi_type):
plex_id = getInfoLabel('ListItem.Property(plexid)') or None
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-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-02-12 00:42:49 +11:00
# userdata = self.api.userdata()
2016-10-23 02:15:10 +11:00
# if userdata['Favorite']:
# # Remove from emby favourites
# options.append(OPTIONS['RemoveFav'])
# else:
# # Add to emby favourites
# options.append(OPTIONS['AddFav'])
2018-02-03 22:45:48 +11:00
# if self.kodi_type == "song":
2016-10-23 02:15:10 +11:00
# # Set custom song rating
# options.append(OPTIONS['RateSong'])
# Refresh item
# options.append(OPTIONS['Refresh'])
2016-10-23 02:15:10 +11:00
# Delete item, only if the Plex Home main user is logged in
if (window('plex_restricteduser') != 'true' and
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(
"script-emby-context.xml",
2018-02-03 22:45:48 +11:00
Addon('plugin.video.plexkodiconnect').getAddonInfo('path'),
"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()
# elif selected == OPTIONS['Refresh']:
# self.emby.refreshItem(self.item_id)
2016-10-23 02:15:10 +11:00
# elif selected == OPTIONS['AddFav']:
# self.emby.updateUserRating(self.item_id, favourite=True)
# elif selected == OPTIONS['RemoveFav']:
# self.emby.updateUserRating(self.item_id, favourite=False)
# elif selected == OPTIONS['RateSong']:
# self._rate_song()
elif selected == OPTIONS['Addon']:
2018-02-03 22:45:48 +11:00
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
if settings('skipContextMenu') != "true":
2018-02-03 22:45:48 +11:00
if not dialog("yesno", heading="{plex}", line1=lang(33041)):
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)
if delete_item_from_pms(self.plex_id) is False:
dialog("ok", heading="{plex}", line1=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
params = {
2018-02-03 22:45:48 +11:00
'mode': 'play',
'plex_id': self.plex_id,
'plex_type': self.plex_type
}
from urllib import urlencode
2017-01-03 00:07:24 +11:00
handle = ("plugin://plugin.video.plexkodiconnect/movies?%s"
% urlencode(params))
2018-02-03 22:45:48 +11:00
executebuiltin('RunPlugin(%s)' % handle)