PlexKodiConnect/default.py

218 lines
7.6 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
###############################################################################
2018-06-22 03:24:37 +10:00
from __future__ import absolute_import, division, unicode_literals
import logging
2018-06-22 03:24:37 +10:00
from sys import argv
2017-01-03 00:07:24 +11:00
from urlparse import parse_qsl
import xbmc
import xbmcgui
import xbmcplugin
from resources.lib import entrypoint, utils, transfer, variables as v, loghandler
from resources.lib.tools import unicode_paths
###############################################################################
loghandler.config()
LOG = logging.getLogger('PLEX.default')
2016-08-30 03:12:46 +10:00
###############################################################################
2017-01-03 00:07:24 +11:00
HANDLE = int(argv[1])
2016-06-20 07:24:34 +10:00
class Main():
# MAIN ENTRY POINT
2017-01-03 00:07:24 +11:00
# @utils.profiling()
def __init__(self):
LOG.debug('Full sys.argv received: %s', argv)
# Parse parameters
2019-03-30 20:32:56 +11:00
params = dict(parse_qsl(argv[2][1:]))
2018-06-24 03:15:24 +10:00
arguments = unicode_paths.decode(argv[2])
2019-03-30 20:32:56 +11:00
path = unicode_paths.decode(argv[0])
# Ensure unicode
for key, value in params.iteritems():
params[key.decode('utf-8')] = params.pop(key)
params[key] = value.decode('utf-8')
2017-03-08 20:58:08 +11:00
mode = params.get('mode', '')
itemid = params.get('id', '')
2017-01-03 00:07:24 +11:00
2019-04-06 22:55:44 +11:00
if mode == 'playstrm':
while not utils.window('plex.playlist.play'):
2019-05-05 18:29:04 +10:00
xbmc.sleep(25)
2019-04-06 22:55:44 +11:00
if utils.window('plex.playlist.aborted'):
LOG.info("playback aborted")
break
else:
LOG.info("Playback started")
xbmcplugin.setResolvedUrl(int(argv[1]),
False,
xbmcgui.ListItem())
utils.window('plex.playlist.play', clear=True)
utils.window('plex.playlist.aborted', clear=True)
elif mode == 'play':
2017-03-08 20:39:05 +11:00
self.play()
elif mode == 'plex_node':
self.play()
2017-03-08 20:39:05 +11:00
elif mode == 'browseplex':
2017-03-09 02:21:00 +11:00
entrypoint.browse_plex(key=params.get('key'),
2019-01-09 04:00:54 +11:00
plex_type=params.get('plex_type'),
section_id=params.get('section_id'),
synched=params.get('synched') != 'false',
prompt=params.get('prompt'))
2017-03-08 20:39:05 +11:00
elif mode == 'watchlater':
entrypoint.watchlater()
elif mode == 'channels':
2019-01-09 04:00:54 +11:00
entrypoint.browse_plex(key='/channels/all')
2017-03-08 20:39:05 +11:00
elif mode == 'route_to_extras':
# Hack so we can store this path in the Kodi DB
handle = ('plugin://%s?mode=extras&plex_id=%s'
% (v.ADDON_ID, params.get('plex_id')))
if xbmcgui.getCurrentWindowId() == 10025:
# Video Window
xbmc.executebuiltin('Container.Update(\"%s\")' % handle)
else:
xbmc.executebuiltin('ActivateWindow(videos, \"%s\")' % handle)
2018-05-05 03:03:27 +10:00
elif mode == 'extras':
entrypoint.extras(plex_id=params.get('plex_id'))
2017-03-08 21:02:25 +11:00
elif mode == 'settings':
xbmc.executebuiltin('Addon.OpenSettings(%s)' % v.ADDON_ID)
2017-03-08 20:39:05 +11:00
2017-03-08 21:02:25 +11:00
elif mode == 'enterPMS':
2019-01-09 04:00:54 +11:00
LOG.info('Request to manually enter new PMS address')
transfer.plex_command('enter_new_pms_address')
2017-03-08 20:39:05 +11:00
elif mode == 'reset':
transfer.plex_command('RESET-PKC')
2017-03-08 20:39:05 +11:00
elif mode == 'togglePlexTV':
2019-01-09 04:00:54 +11:00
LOG.info('Toggle of Plex.tv sign-in requested')
transfer.plex_command('toggle_plex_tv_sign_in')
2017-03-08 20:39:05 +11:00
elif mode == 'passwords':
from resources.lib.windows import direct_path_sources
direct_path_sources.start()
2017-03-08 20:39:05 +11:00
elif mode == 'switchuser':
2019-01-09 04:00:54 +11:00
LOG.info('Plex home user switch requested')
transfer.plex_command('switch_plex_user')
2017-03-08 20:39:05 +11:00
2017-03-08 21:02:25 +11:00
elif mode in ('manualsync', 'repair'):
if mode == 'repair':
LOG.info('Requesting repair lib sync')
transfer.plex_command('repair-scan')
elif mode == 'manualsync':
LOG.info('Requesting full library scan')
transfer.plex_command('full-scan')
2017-03-08 20:39:05 +11:00
2017-03-08 21:02:25 +11:00
elif mode == 'texturecache':
LOG.info('Requesting texture caching of all textures')
transfer.plex_command('textures-scan')
2017-03-08 20:39:05 +11:00
2017-03-08 21:02:25 +11:00
elif mode == 'chooseServer':
2019-01-09 04:00:54 +11:00
LOG.info("Choosing PMS server requested, starting")
transfer.plex_command('choose_pms_server')
2017-03-08 20:39:05 +11:00
2017-03-08 21:02:25 +11:00
elif mode == 'deviceid':
2017-03-08 20:39:05 +11:00
self.deviceid()
elif mode == 'fanart':
LOG.info('User requested fanarttv refresh')
transfer.plex_command('fanart-scan')
2017-03-08 20:39:05 +11:00
2018-06-24 03:15:24 +10:00
elif '/extrafanart' in path:
plexpath = arguments[1:]
2017-03-08 20:58:08 +11:00
plexid = itemid
2018-06-15 22:05:48 +10:00
entrypoint.extra_fanart(plexid, plexpath)
entrypoint.get_video_files(plexid, plexpath)
# Called by e.g. 3rd party plugin video extras
2018-06-24 03:15:24 +10:00
elif ('/Extras' in path or '/VideoFiles' in path or
'/Extras' in arguments):
2017-03-08 20:58:08 +11:00
plexId = itemid or None
2018-06-15 22:05:48 +10:00
entrypoint.get_video_files(plexId, params)
elif mode == 'playlists':
2018-07-30 21:20:40 +10:00
entrypoint.playlists(params.get('content_type'))
elif mode == 'hub':
2019-01-09 04:00:54 +11:00
entrypoint.hub(params.get('content_type'))
elif mode == 'select-libraries':
LOG.info('User requested to select Plex libraries')
transfer.plex_command('select-libraries')
elif mode == 'refreshplaylist':
LOG.info('User requested to refresh Kodi playlists and nodes')
transfer.plex_command('refreshplaylist')
else:
2018-06-15 21:49:18 +10:00
entrypoint.show_main_menu(content_type=params.get('content_type'))
2017-03-08 20:39:05 +11:00
2017-08-22 02:53:38 +10:00
@staticmethod
def play():
"""
Start up playback_starter in main Python thread
"""
2018-04-16 02:13:48 +10:00
request = '%s&handle=%s' % (argv[2], HANDLE)
2017-03-08 21:02:25 +11:00
# Put the request into the 'queue'
transfer.plex_command('PLAY-%s' % request)
2018-04-16 02:13:48 +10:00
if HANDLE == -1:
# Handle -1 received, not waiting for main thread
return
# Wait for the result from the main PKC thread
result = transfer.wait_for_transfer()
2017-03-08 20:39:05 +11:00
if result is None:
LOG.error('Error encountered, aborting')
2018-06-22 03:24:37 +10:00
utils.dialog('notification',
heading='{plex}',
message=utils.lang(30128),
icon='{error}',
time=3000)
xbmcplugin.setResolvedUrl(HANDLE, False, xbmcgui.ListItem())
elif result is True:
pass
else:
# Received a xbmcgui.ListItem()
xbmcplugin.setResolvedUrl(HANDLE, True, result)
2017-03-08 20:39:05 +11:00
2017-08-22 02:53:38 +10:00
@staticmethod
def deviceid():
window = xbmcgui.Window(10000)
deviceId_old = window.getProperty('plex_client_Id')
2018-07-04 16:05:24 +10:00
from resources.lib import clientinfo
2017-03-08 20:39:05 +11:00
try:
2018-07-04 16:05:24 +10:00
deviceId = clientinfo.getDeviceId(reset=True)
2017-03-08 20:39:05 +11:00
except Exception as e:
LOG.error('Failed to generate a new device Id: %s' % e)
2018-09-19 00:26:40 +10:00
utils.messageDialog(utils.lang(29999), utils.lang(33032))
2017-03-08 20:39:05 +11:00
else:
LOG.info('Successfully removed old device ID: %s New deviceId:'
2017-03-08 21:02:25 +11:00
'%s' % (deviceId_old, deviceId))
# 'Kodi will now restart to apply the changes'
2018-09-19 00:26:40 +10:00
utils.messageDialog(utils.lang(29999), utils.lang(33033))
xbmc.executebuiltin('RestartApp')
2017-08-22 02:53:38 +10:00
2017-03-08 21:02:25 +11:00
if __name__ == '__main__':
LOG.info('%s started' % v.ADDON_ID)
try:
v.database_paths()
except RuntimeError as err:
# Database does not exists
LOG.error('The current Kodi version is incompatible')
LOG.error('Error: %s', err)
else:
Main()
LOG.info('%s stopped' % v.ADDON_ID)