PlexKodiConnect/default.py

213 lines
6.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
###############################################################################
import logging
2017-01-02 14:07:24 +01:00
from os import path as os_path
from sys import path as sys_path, argv
from urlparse import parse_qsl
2015-05-03 17:39:12 +02:00
2017-01-02 14:07:24 +01:00
from xbmc import translatePath, sleep, executebuiltin
from xbmcaddon import Addon
from xbmcgui import ListItem
2017-01-02 14:07:24 +01:00
from xbmcplugin import setResolvedUrl
2017-01-02 14:07:24 +01:00
_addon = Addon(id='plugin.video.plexkodiconnect')
try:
2016-08-29 19:12:46 +02:00
_addon_path = _addon.getAddonInfo('path').decode('utf-8')
except TypeError:
2016-08-29 19:12:46 +02:00
_addon_path = _addon.getAddonInfo('path').decode()
try:
2017-01-02 14:07:24 +01:00
_base_resource = translatePath(os_path.join(
2016-08-29 19:12:46 +02:00
_addon_path,
'resources',
'lib')).decode('utf-8')
except TypeError:
2017-01-02 14:07:24 +01:00
_base_resource = translatePath(os_path.join(
2016-08-29 19:12:46 +02:00
_addon_path,
'resources',
'lib')).decode()
2017-01-02 14:07:24 +01:00
sys_path.append(_base_resource)
###############################################################################
import entrypoint
2017-09-10 15:06:46 +02:00
from utils import window, reset, passwordsXML, language as lang, dialog, \
plex_command
from pickler import unpickle_me, pickl_window
2017-01-02 14:07:24 +01:00
from PKC_listitem import convert_PKC_to_listitem
2017-03-08 10:39:05 +01:00
import variables as v
###############################################################################
import loghandler
loghandler.config()
2017-03-08 11:02:25 +01:00
log = logging.getLogger('PLEX.default')
2016-08-29 19:12:46 +02:00
###############################################################################
2017-01-02 14:07:24 +01:00
HANDLE = int(argv[1])
2016-06-19 16:24:34 -05:00
class Main():
# MAIN ENTRY POINT
2017-01-02 14:07:24 +01:00
# @utils.profiling()
def __init__(self):
2017-03-08 11:02:25 +01:00
log.debug('Full sys.argv received: %s' % argv)
# Parse parameters
params = dict(parse_qsl(argv[2][1:]))
2017-03-08 10:58:08 +01:00
mode = params.get('mode', '')
itemid = params.get('id', '')
2017-01-02 14:07:24 +01:00
if mode == 'play':
2017-03-08 10:39:05 +01:00
self.play()
elif mode == 'plex_node':
self.play()
2017-03-08 10:39:05 +01:00
elif mode == 'ondeck':
entrypoint.getOnDeck(itemid,
params.get('type'),
params.get('tagname'),
int(params.get('limit')))
elif mode == 'recentepisodes':
entrypoint.getRecentEpisodes(itemid,
params.get('type'),
params.get('tagname'),
int(params.get('limit')))
elif mode == 'nextup':
entrypoint.getNextUpEpisodes(params['tagname'],
int(params['limit']))
2017-03-08 11:02:25 +01:00
elif mode == 'inprogressepisodes':
2017-03-08 10:39:05 +01:00
entrypoint.getInProgressEpisodes(params['tagname'],
int(params['limit']))
elif mode == 'browseplex':
2017-03-08 16:21:00 +01:00
entrypoint.browse_plex(key=params.get('key'),
plex_section_id=params.get('id'))
2017-03-08 10:39:05 +01:00
2017-03-08 11:02:25 +01:00
elif mode == 'getsubfolders':
2017-03-08 10:39:05 +01:00
entrypoint.GetSubFolders(itemid)
elif mode == 'watchlater':
entrypoint.watchlater()
elif mode == 'channels':
entrypoint.channels()
2017-03-08 11:02:25 +01:00
elif mode == 'settings':
2017-03-08 10:39:05 +01:00
executebuiltin('Addon.OpenSettings(%s)' % v.ADDON_ID)
2017-03-08 11:02:25 +01:00
elif mode == 'enterPMS':
2017-03-08 10:39:05 +01:00
entrypoint.enterPMS()
elif mode == 'reset':
reset()
elif mode == 'togglePlexTV':
entrypoint.togglePlexTV()
elif mode == 'resetauth':
entrypoint.resetAuth()
elif mode == 'passwords':
passwordsXML()
elif mode == 'switchuser':
entrypoint.switchPlexUser()
2017-03-08 11:02:25 +01:00
elif mode in ('manualsync', 'repair'):
if window('plex_online') != 'true':
2017-03-08 10:39:05 +01:00
# Server is not online, do not run the sync
2017-04-30 17:11:11 +02:00
dialog('ok', lang(29999), lang(39205))
2017-03-08 11:02:25 +01:00
log.error('Not connected to a PMS.')
2017-03-08 10:39:05 +01:00
else:
if mode == 'repair':
2017-03-08 11:02:25 +01:00
log.info('Requesting repair lib sync')
2017-08-21 18:53:38 +02:00
plex_command('RUN_LIB_SCAN', 'repair')
2017-03-08 10:39:05 +01:00
elif mode == 'manualsync':
2017-03-08 11:02:25 +01:00
log.info('Requesting full library scan')
2017-08-21 18:53:38 +02:00
plex_command('RUN_LIB_SCAN', 'full')
2017-03-08 10:39:05 +01:00
2017-03-08 11:02:25 +01:00
elif mode == 'texturecache':
2017-08-21 18:53:38 +02:00
log.info('Requesting texture caching of all textures')
plex_command('RUN_LIB_SCAN', 'textures')
2017-03-08 10:39:05 +01:00
2017-03-08 11:02:25 +01:00
elif mode == 'chooseServer':
2017-03-08 10:39:05 +01:00
entrypoint.chooseServer()
2017-03-08 11:02:25 +01:00
elif mode == 'refreshplaylist':
2017-03-08 10:39:05 +01:00
log.info('Requesting playlist/nodes refresh')
2017-08-21 18:53:38 +02:00
plex_command('RUN_LIB_SCAN', 'views')
2017-03-08 10:39:05 +01:00
2017-03-08 11:02:25 +01:00
elif mode == 'deviceid':
2017-03-08 10:39:05 +01:00
self.deviceid()
elif mode == 'fanart':
log.info('User requested fanarttv refresh')
2017-08-21 18:53:38 +02:00
plex_command('RUN_LIB_SCAN', 'fanart')
2017-03-08 10:39:05 +01:00
2017-03-08 11:02:25 +01:00
elif '/extrafanart' in argv[0]:
plexpath = argv[2][1:]
2017-03-08 10:58:08 +01:00
plexid = itemid
2016-05-31 08:06:42 +02:00
entrypoint.getExtraFanArt(plexid, plexpath)
2016-08-29 19:12:46 +02:00
entrypoint.getVideoFiles(plexid, plexpath)
# Called by e.g. 3rd party plugin video extras
2017-03-08 11:02:25 +01:00
elif ('/Extras' in argv[0] or '/VideoFiles' in argv[0] or
'/Extras' in argv[2]):
2017-03-08 10:58:08 +01:00
plexId = itemid or None
entrypoint.getVideoFiles(plexId, params)
else:
entrypoint.doMainListing(content_type=params.get('content_type'))
2017-03-08 10:39:05 +01:00
2017-08-21 18:53:38 +02:00
@staticmethod
def play():
"""
Start up playback_starter in main Python thread
"""
2017-03-08 11:02:25 +01:00
# Put the request into the 'queue'
2017-08-21 18:59:47 +02:00
plex_command('PLAY', argv[2])
2017-03-08 10:39:05 +01:00
# Wait for the result
while not pickl_window('plex_result'):
sleep(50)
result = unpickle_me()
if result is None:
log.error('Error encountered, aborting')
dialog('notification',
heading='{plex}',
message=lang(30128),
icon='{error}',
time=3000)
setResolvedUrl(HANDLE, False, ListItem())
elif result.listitem:
listitem = convert_PKC_to_listitem(result.listitem)
setResolvedUrl(HANDLE, True, listitem)
2017-08-21 18:53:38 +02:00
@staticmethod
def deviceid():
2017-03-08 10:39:05 +01:00
deviceId_old = window('plex_client_Id')
from clientinfo import getDeviceId
try:
deviceId = getDeviceId(reset=True)
except Exception as e:
2017-03-08 11:02:25 +01:00
log.error('Failed to generate a new device Id: %s' % e)
2017-03-08 10:39:05 +01:00
dialog('ok', lang(29999), lang(33032))
else:
2017-03-08 11:02:25 +01:00
log.info('Successfully removed old device ID: %s New deviceId:'
'%s' % (deviceId_old, deviceId))
# 'Kodi will now restart to apply the changes'
2017-03-08 10:39:05 +01:00
dialog('ok', lang(29999), lang(33033))
executebuiltin('RestartApp')
2017-08-21 18:53:38 +02:00
2017-03-08 11:02:25 +01:00
if __name__ == '__main__':
2017-03-08 10:39:05 +01:00
log.info('%s started' % v.ADDON_ID)
2016-06-19 16:24:34 -05:00
Main()
2017-03-08 10:39:05 +01:00
log.info('%s stopped' % v.ADDON_ID)