PlexKodiConnect/resources/lib/entrypoint.py

957 lines
34 KiB
Python
Raw Normal View History

2015-12-25 07:07:00 +11:00
# -*- coding: utf-8 -*-
2016-02-20 06:03:06 +11:00
###############################################################################
2017-12-10 00:35:08 +11:00
from logging import getLogger
from shutil import copyfile
from os import walk, makedirs
from os.path import basename, join
2017-01-25 05:59:38 +11:00
from sys import argv
from urllib import urlencode
2015-12-25 07:07:00 +11:00
import xbmcplugin
2017-03-26 22:23:37 +11:00
from xbmc import sleep, executebuiltin, translatePath
2017-01-25 05:59:38 +11:00
from xbmcgui import ListItem
2015-12-25 07:07:00 +11:00
2018-02-11 22:59:04 +11:00
from utils import window, settings, language as lang, dialog, try_encode, \
CatchExceptions, exists_dir, plex_command, try_decode
2015-12-25 07:07:00 +11:00
import downloadutils
2017-01-25 05:59:38 +11:00
from PlexFunctions import GetPlexMetadata, GetPlexSectionResults, \
GetMachineIdentifier
from PlexAPI import API
2017-12-09 05:43:06 +11:00
import json_rpc as js
import variables as v
2016-01-02 00:40:40 +11:00
2016-02-20 06:03:06 +11:00
###############################################################################
2017-12-10 00:35:08 +11:00
log = getLogger("PLEX."+__name__)
2016-08-30 23:51:11 +10:00
2017-01-25 06:04:53 +11:00
try:
HANDLE = int(argv[1])
ARGV_0 = argv[0]
except IndexError:
pass
2016-08-30 23:51:11 +10:00
###############################################################################
2016-01-30 06:07:21 +11:00
def chooseServer():
"""
2016-05-30 00:52:00 +10:00
Lets user choose from list of PMS
"""
2016-08-30 23:51:11 +10:00
log.info("Choosing PMS server requested, starting")
import initialsetup
2016-05-25 04:43:52 +10:00
setup = initialsetup.InitialSetup()
2018-02-11 03:59:20 +11:00
server = setup.pick_pms(showDialog=True)
2016-05-25 04:43:52 +10:00
if server is None:
2016-08-30 23:51:11 +10:00
log.error('We did not connect to a new PMS, aborting')
plex_command('SUSPEND_USER_CLIENT', 'False')
plex_command('SUSPEND_LIBRARY_THREAD', 'False')
2016-05-25 04:43:52 +10:00
return
2016-08-30 23:51:11 +10:00
log.info("User chose server %s" % server['name'])
2018-02-11 03:59:20 +11:00
setup.write_pms_to_settings(server)
if not __LogOut():
2016-05-30 00:52:00 +10:00
return
2018-02-11 22:59:04 +11:00
from utils import delete_playlists, delete_nodes
2016-05-30 01:17:33 +10:00
# First remove playlists
2018-02-11 22:59:04 +11:00
delete_playlists()
2016-05-30 01:17:33 +10:00
# Remove video nodes
2018-02-11 22:59:04 +11:00
delete_nodes()
2016-05-30 01:17:33 +10:00
2016-05-30 00:52:00 +10:00
# Log in again
__LogIn()
2016-08-30 23:51:11 +10:00
log.info("Choosing new PMS complete")
2016-05-30 00:52:00 +10:00
# '<PMS> connected'
2017-01-25 05:59:38 +11:00
dialog('notification',
lang(29999),
'%s %s' % (server['name'], lang(39220)),
icon='{plex}',
time=3000,
sound=False)
2016-05-30 00:52:00 +10:00
def togglePlexTV():
2016-08-30 23:51:11 +10:00
if settings('plexToken'):
log.info('Reseting plex.tv credentials in settings')
settings('plexLogin', value="")
2017-05-09 03:29:52 +10:00
settings('plexToken', value="")
2016-08-30 23:51:11 +10:00
settings('plexid', value="")
settings('plexHomeSize', value="1")
settings('plexAvatar', value="")
settings('plex_status', value=lang(39226))
2016-08-30 23:51:11 +10:00
window('plex_token', clear=True)
plex_command('PLEX_TOKEN', '')
2017-05-18 00:14:17 +10:00
plex_command('PLEX_USERNAME', '')
2016-05-30 00:52:00 +10:00
else:
2016-08-30 23:51:11 +10:00
log.info('Login to plex.tv')
2016-05-30 00:52:00 +10:00
import initialsetup
2018-02-11 03:59:20 +11:00
initialsetup.InitialSetup().plex_tv_sign_in()
2017-01-25 05:59:38 +11:00
dialog('notification',
lang(29999),
lang(39221),
icon='{plex}',
time=3000,
sound=False)
2016-03-04 23:34:30 +11:00
2015-12-25 07:07:00 +11:00
##### DO RESET AUTH #####
def resetAuth():
# User tried login and failed too many times
2017-01-25 05:59:38 +11:00
resp = dialog('yesno', heading="{plex}", line1=lang(39206))
2015-12-25 07:07:00 +11:00
if resp == 1:
2016-08-30 23:51:11 +10:00
log.info("Reset login attempts.")
plex_command('PMS_STATUS', 'Auth')
2015-12-25 07:07:00 +11:00
else:
2017-01-25 05:59:38 +11:00
executebuiltin('Addon.OpenSettings(plugin.video.plexkodiconnect)')
2016-08-30 23:51:11 +10:00
2015-12-25 07:07:00 +11:00
def addDirectoryItem(label, path, folder=True):
2017-01-25 05:59:38 +11:00
li = ListItem(label, path=path)
li.setThumbnailImage("special://home/addons/plugin.video.plexkodiconnect/icon.png")
li.setArt({"fanart":"special://home/addons/plugin.video.plexkodiconnect/fanart.jpg"})
li.setArt({"landscape":"special://home/addons/plugin.video.plexkodiconnect/fanart.jpg"})
2017-01-25 05:59:38 +11:00
xbmcplugin.addDirectoryItem(handle=HANDLE, url=path, listitem=li, isFolder=folder)
2015-12-25 07:07:00 +11:00
2016-08-30 23:51:11 +10:00
def doMainListing(content_type=None):
log.debug('Do main listing with content_type: %s' % content_type)
2017-01-25 05:59:38 +11:00
xbmcplugin.setContent(HANDLE, 'files')
2015-12-25 07:07:00 +11:00
# Get emby nodes from the window props
2016-08-30 23:51:11 +10:00
plexprops = window('Plex.nodes.total')
2016-05-31 16:06:42 +10:00
if plexprops:
totalnodes = int(plexprops)
2015-12-25 07:07:00 +11:00
for i in range(totalnodes):
2016-08-30 23:51:11 +10:00
path = window('Plex.nodes.%s.index' % i)
2015-12-25 07:07:00 +11:00
if not path:
2016-08-30 23:51:11 +10:00
path = window('Plex.nodes.%s.content' % i)
if not path:
continue
2016-08-30 23:51:11 +10:00
label = window('Plex.nodes.%s.title' % i)
node_type = window('Plex.nodes.%s.type' % i)
# because we do not use seperate entrypoints for each content type,
# we need to figure out which items to show in each listing. for
# now we just only show picture nodes in the picture library video
# nodes in the video library and all nodes in any other window
if node_type == 'photos' and content_type == 'image':
2015-12-25 07:07:00 +11:00
addDirectoryItem(label, path)
elif (node_type != 'photos' and
content_type not in ('image', 'audio')):
2015-12-25 07:07:00 +11:00
addDirectoryItem(label, path)
2016-04-17 21:36:41 +10:00
# Plex Watch later
if content_type not in ('image', 'audio'):
addDirectoryItem(lang(39211),
"plugin://%s?mode=watchlater" % v.ADDON_ID)
2017-03-08 02:37:36 +11:00
# Plex Channels
addDirectoryItem(lang(30173),
"plugin://%s?mode=channels" % v.ADDON_ID)
2016-03-11 02:04:01 +11:00
# Plex user switch
2017-05-18 00:14:17 +10:00
addDirectoryItem(lang(39200),
"plugin://%s?mode=switchuser" % v.ADDON_ID)
# some extra entries for settings and stuff
addDirectoryItem(lang(39201),
"plugin://%s?mode=settings" % v.ADDON_ID)
addDirectoryItem(lang(39203),
"plugin://%s?mode=refreshplaylist" % v.ADDON_ID)
addDirectoryItem(lang(39204),
"plugin://%s?mode=manualsync" % v.ADDON_ID)
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(HANDLE)
2015-12-25 07:07:00 +11:00
2016-02-29 16:20:59 +11:00
2016-01-25 20:36:24 +11:00
def switchPlexUser():
"""
Signs out currently logged in user (if applicable). Triggers sign-in of a
new user
"""
# Guess these user avatars are a future feature. Skipping for now
# Delete any userimages. Since there's always only 1 user: position = 0
# position = 0
2016-08-30 23:51:11 +10:00
# window('EmbyAdditionalUserImage.%s' % position, clear=True)
log.info("Plex home user switch requested")
if not __LogOut():
2016-05-30 00:52:00 +10:00
return
2016-03-10 01:53:46 +11:00
2016-05-30 00:52:00 +10:00
# First remove playlists of old user
2018-02-11 22:59:04 +11:00
from utils import delete_playlists, delete_nodes
delete_playlists()
# Remove video nodes
2018-02-11 22:59:04 +11:00
delete_nodes()
2016-05-30 00:52:00 +10:00
__LogIn()
2016-01-25 20:36:24 +11:00
2016-01-22 21:10:42 +11:00
#### SHOW SUBFOLDERS FOR NODE #####
def GetSubFolders(nodeindex):
nodetypes = ["",".recent",".recentepisodes",".inprogress",".inprogressepisodes",".unwatched",".nextepisodes",".sets",".genres",".random",".recommended"]
for node in nodetypes:
2016-08-30 23:51:11 +10:00
title = window('Plex.nodes.%s%s.title' %(nodeindex,node))
2016-01-22 21:10:42 +11:00
if title:
2016-08-30 23:51:11 +10:00
path = window('Plex.nodes.%s%s.content' %(nodeindex,node))
2016-01-22 21:10:42 +11:00
addDirectoryItem(title, path)
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(HANDLE)
2015-12-25 07:07:00 +11:00
##### LISTITEM SETUP FOR VIDEONODES #####
def createListItem(item, appendShowTitle=False, appendSxxExx=False):
2015-12-25 07:07:00 +11:00
title = item['title']
2017-01-25 05:59:38 +11:00
li = ListItem(title)
2015-12-25 07:07:00 +11:00
li.setProperty('IsPlayable', "true")
metadata = {
2015-12-25 07:07:00 +11:00
'duration': str(item['runtime']/60),
'Plot': item['plot'],
'Playcount': item['playcount']
}
if "episode" in item:
episode = item['episode']
metadata['Episode'] = episode
if "season" in item:
season = item['season']
metadata['Season'] = season
if season and episode:
li.setProperty('episodeno', "s%.2de%.2d" % (season, episode))
if appendSxxExx is True:
title = "S%.2dE%.2d - %s" % (season, episode, title)
2015-12-25 07:07:00 +11:00
if "firstaired" in item:
metadata['Premiered'] = item['firstaired']
if "showtitle" in item:
metadata['TVshowTitle'] = item['showtitle']
if appendShowTitle is True:
title = item['showtitle'] + ' - ' + title
2015-12-25 07:07:00 +11:00
if "rating" in item:
metadata['Rating'] = str(round(float(item['rating']),1))
if "director" in item:
metadata['Director'] = " / ".join(item['director'])
if "writer" in item:
metadata['Writer'] = " / ".join(item['writer'])
if "cast" in item:
cast = []
castandrole = []
for person in item['cast']:
name = person['name']
cast.append(name)
castandrole.append((name, person['role']))
metadata['Cast'] = cast
metadata['CastAndRole'] = castandrole
metadata['Title'] = title
li.setLabel(title)
2015-12-25 07:07:00 +11:00
li.setInfo(type="Video", infoLabels=metadata)
li.setProperty('resumetime', str(item['resume']['position']))
li.setProperty('totaltime', str(item['resume']['total']))
li.setArt(item['art'])
li.setThumbnailImage(item['art'].get('thumb',''))
2017-01-03 00:07:24 +11:00
li.setArt({'icon': 'DefaultTVShows.png'})
2015-12-25 07:07:00 +11:00
li.setProperty('dbid', str(item['episodeid']))
li.setProperty('fanart_image', item['art'].get('tvshow.fanart',''))
2017-08-03 02:54:05 +10:00
try:
2017-08-03 04:01:13 +10:00
li.addContextMenuItems([(lang(30032), 'XBMC.Action(Info)',)])
2017-08-03 02:54:05 +10:00
except TypeError:
# Kodi fuck-up
pass
2015-12-25 07:07:00 +11:00
for key, value in item['streamdetails'].iteritems():
for stream in value:
li.addStreamInfo(key, stream)
return li
##### GET NEXTUP EPISODES FOR TAGNAME #####
def getNextUpEpisodes(tagname, limit):
count = 0
# if the addon is called with nextup parameter,
# we return the nextepisodes list of the given tagname
2017-01-25 05:59:38 +11:00
xbmcplugin.setContent(HANDLE, 'episodes')
2015-12-25 07:07:00 +11:00
# First we get a list of all the TV shows - filtered by tag
2017-01-25 05:59:38 +11:00
params = {
'sort': {'order': "descending", 'method': "lastplayed"},
'filter': {
'and': [
{'operator': "true", 'field': "inprogress", 'value': ""},
{'operator': "is", 'field': "tag", 'value': "%s" % tagname}
]},
'properties': ['title', 'studio', 'mpaa', 'file', 'art']
2015-12-25 07:07:00 +11:00
}
2017-12-09 05:43:06 +11:00
for item in js.get_tv_shows(params):
if settings('ignoreSpecialsNextEpisodes') == "true":
params = {
'tvshowid': item['tvshowid'],
'sort': {'method': "episode"},
'filter': {
'and': [
{'operator': "lessthan",
'field': "playcount",
'value': "1"},
{'operator': "greaterthan",
'field': "season",
'value': "0"}]},
'properties': [
"title", "playcount", "season", "episode", "showtitle",
"plot", "file", "rating", "resume", "tvshowid", "art",
"streamdetails", "firstaired", "runtime", "writer",
"dateadded", "lastplayed"
],
'limits': {"end": 1}
}
else:
params = {
'tvshowid': item['tvshowid'],
'sort': {'method': "episode"},
'filter': {
'operator': "lessthan",
'field': "playcount",
'value': "1"},
'properties': [
"title", "playcount", "season", "episode", "showtitle",
"plot", "file", "rating", "resume", "tvshowid", "art",
"streamdetails", "firstaired", "runtime", "writer",
"dateadded", "lastplayed"
],
'limits': {"end": 1}
}
for episode in js.get_episodes(params):
xbmcplugin.addDirectoryItem(handle=HANDLE,
url=episode['file'],
listitem=createListItem(episode))
count += 1
if count == limit:
break
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(handle=HANDLE)
2015-12-25 07:07:00 +11:00
2017-01-25 05:59:38 +11:00
##### GET INPROGRESS EPISODES FOR TAGNAME #####
2015-12-25 07:07:00 +11:00
def getInProgressEpisodes(tagname, limit):
count = 0
# if the addon is called with inprogressepisodes parameter,
# we return the inprogressepisodes list of the given tagname
2017-01-25 05:59:38 +11:00
xbmcplugin.setContent(HANDLE, 'episodes')
2015-12-25 07:07:00 +11:00
# First we get a list of all the in-progress TV shows - filtered by tag
2017-01-25 05:59:38 +11:00
params = {
'sort': {'order': "descending", 'method': "lastplayed"},
'filter': {
'and': [
{'operator': "true", 'field': "inprogress", 'value': ""},
{'operator': "is", 'field': "tag", 'value': "%s" % tagname}
]},
'properties': ['title', 'studio', 'mpaa', 'file', 'art']
2015-12-25 07:07:00 +11:00
}
2017-12-09 05:43:06 +11:00
for item in js.get_tv_shows(params):
params = {
'tvshowid': item['tvshowid'],
'sort': {'method': "episode"},
'filter': {
'operator': "true",
'field': "inprogress",
'value': ""},
'properties': ["title", "playcount", "season", "episode",
"showtitle", "plot", "file", "rating", "resume",
"tvshowid", "art", "cast", "streamdetails", "firstaired",
"runtime", "writer", "dateadded", "lastplayed"]
}
for episode in js.get_episodes(params):
xbmcplugin.addDirectoryItem(handle=HANDLE,
url=episode['file'],
listitem=createListItem(episode))
count += 1
if count == limit:
break
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(handle=HANDLE)
2015-12-25 07:07:00 +11:00
##### GET RECENT EPISODES FOR TAGNAME #####
2016-03-16 00:19:56 +11:00
# def getRecentEpisodes(tagname, limit):
def getRecentEpisodes(viewid, mediatype, tagname, limit):
2015-12-25 07:07:00 +11:00
count = 0
# if the addon is called with recentepisodes parameter,
# we return the recentepisodes list of the given tagname
2017-01-25 05:59:38 +11:00
xbmcplugin.setContent(HANDLE, 'episodes')
2016-08-30 23:51:11 +10:00
appendShowTitle = settings('RecentTvAppendShow') == 'true'
appendSxxExx = settings('RecentTvAppendSeason') == 'true'
2015-12-25 07:07:00 +11:00
# First we get a list of all the TV shows - filtered by tag
2017-12-09 05:43:06 +11:00
allshowsIds = set()
2017-01-25 05:59:38 +11:00
params = {
'sort': {'order': "descending", 'method': "dateadded"},
'filter': {'operator': "is", 'field': "tag", 'value': "%s" % tagname},
2015-12-25 07:07:00 +11:00
}
2017-12-09 05:43:06 +11:00
for tv_show in js.get_tv_shows(params):
allshowsIds.add(tv_show['tvshowid'])
2017-01-25 05:59:38 +11:00
params = {
'sort': {'order': "descending", 'method': "dateadded"},
'properties': ["title", "playcount", "season", "episode", "showtitle",
"plot", "file", "rating", "resume", "tvshowid", "art",
"streamdetails", "firstaired", "runtime", "cast", "writer",
"dateadded", "lastplayed"],
"limits": {"end": limit}
2016-03-16 00:19:56 +11:00
}
2016-08-30 23:51:11 +10:00
if settings('TVShowWatched') == 'false':
2017-01-25 05:59:38 +11:00
params['filter'] = {
'operator': "lessthan",
'field': "playcount",
'value': "1"
}
2017-12-09 05:43:06 +11:00
for episode in js.get_episodes(params):
if episode['tvshowid'] in allshowsIds:
listitem = createListItem(episode,
appendShowTitle=appendShowTitle,
appendSxxExx=appendSxxExx)
xbmcplugin.addDirectoryItem(
handle=HANDLE,
url=episode['file'],
listitem=listitem)
count += 1
if count == limit:
break
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(handle=HANDLE)
2015-12-25 07:07:00 +11:00
def getVideoFiles(plexId, params):
"""
GET VIDEO EXTRAS FOR LISTITEM
returns the video files for the item as plugin listing, can be used for
browsing the actual files or videoextras etc.
"""
if plexId is None:
filename = params.get('filename')
if filename is not None:
filename = filename[0]
import re
regex = re.compile(r'''library/metadata/(\d+)''')
filename = regex.findall(filename)
try:
plexId = filename[0]
except IndexError:
pass
if plexId is None:
2016-08-30 23:51:11 +10:00
log.info('No Plex ID found, abort getting Extras')
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE)
2017-01-25 05:59:38 +11:00
item = GetPlexMetadata(plexId)
try:
path = item[0][0][0].attrib['file']
except:
2016-08-30 23:51:11 +10:00
log.error('Could not get file path for item %s' % plexId)
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE)
# Assign network protocol
if path.startswith('\\\\'):
path = path.replace('\\\\', 'smb://')
path = path.replace('\\', '/')
# Plex returns Windows paths as e.g. 'c:\slfkjelf\slfje\file.mkv'
elif '\\' in path:
path = path.replace('\\', '\\\\')
# Directory only, get rid of filename
path = path.replace(basename(path), '')
if exists_dir(path):
for root, dirs, files in walk(path):
for directory in dirs:
2018-02-11 22:59:04 +11:00
item_path = try_encode(join(root, directory))
li = ListItem(item_path, path=item_path)
xbmcplugin.addDirectoryItem(handle=HANDLE,
url=item_path,
listitem=li,
isFolder=True)
for file in files:
2018-02-11 22:59:04 +11:00
item_path = try_encode(join(root, file))
li = ListItem(item_path, path=item_path)
xbmcplugin.addDirectoryItem(handle=HANDLE,
url=file,
listitem=li)
break
else:
log.error('Kodi cannot access folder %s' % path)
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(HANDLE)
2016-08-30 23:51:11 +10:00
@CatchExceptions(warnuser=False)
def getExtraFanArt(plexid, plexPath):
"""
Get extrafanart for listitem
will be called by skinhelper script to get the extrafanart
for tvshows we get the plexid just from the path
"""
2016-08-30 23:51:11 +10:00
log.debug('Called with plexid: %s, plexPath: %s' % (plexid, plexPath))
if not plexid:
if "plugin.video.plexkodiconnect" in plexPath:
plexid = plexPath.split("/")[-2]
if not plexid:
2016-08-30 23:51:11 +10:00
log.error('Could not get a plexid, aborting')
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE)
2016-07-23 00:55:57 +10:00
# We need to store the images locally for this to work
# because of the caching system in xbmc
2018-02-11 22:59:04 +11:00
fanartDir = try_decode(translatePath(
"special://thumbnails/plex/%s/" % plexid))
if not exists_dir(fanartDir):
2016-07-23 00:55:57 +10:00
# Download the images to the cache directory
makedirs(fanartDir)
2017-01-25 05:59:38 +11:00
xml = GetPlexMetadata(plexid)
2016-07-23 00:55:57 +10:00
if xml is None:
2016-08-30 23:51:11 +10:00
log.error('Could not download metadata for %s' % plexid)
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE)
2017-01-25 05:59:38 +11:00
api = API(xml[0])
backdrops = api.getAllArtwork()['Backdrop']
2016-07-23 00:55:57 +10:00
for count, backdrop in enumerate(backdrops):
# Same ordering as in artwork
2018-02-11 22:59:04 +11:00
fanartFile = try_encode(join(fanartDir, "fanart%.3d.jpg" % count))
2017-01-25 05:59:38 +11:00
li = ListItem("%.3d" % count, path=fanartFile)
2016-07-23 00:55:57 +10:00
xbmcplugin.addDirectoryItem(
2017-01-25 05:59:38 +11:00
handle=HANDLE,
2016-07-23 00:55:57 +10:00
url=fanartFile,
listitem=li)
2018-02-11 22:59:04 +11:00
copyfile(backdrop, try_decode(fanartFile))
2016-07-23 00:55:57 +10:00
else:
2016-08-30 23:51:11 +10:00
log.info("Found cached backdrop.")
2016-07-23 00:55:57 +10:00
# Use existing cached images
for root, dirs, files in walk(fanartDir):
for file in files:
2018-02-11 22:59:04 +11:00
fanartFile = try_encode(join(root, file))
li = ListItem(file, path=fanartFile)
xbmcplugin.addDirectoryItem(handle=HANDLE,
url=fanartFile,
listitem=li)
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(HANDLE)
2016-03-15 23:09:51 +11:00
def getOnDeck(viewid, mediatype, tagname, limit):
"""
Retrieves Plex On Deck items, currently only for TV shows
Input:
viewid: Plex id of the library section, e.g. '1'
mediatype: Kodi mediatype, e.g. 'tvshows', 'movies',
'homevideos', 'photos'
tagname: Name of the Plex library, e.g. "My Movies"
2016-03-16 00:19:56 +11:00
limit: Max. number of items to retrieve, e.g. 50
2016-03-15 23:09:51 +11:00
"""
2017-01-25 05:59:38 +11:00
xbmcplugin.setContent(HANDLE, 'episodes')
2016-08-30 23:51:11 +10:00
appendShowTitle = settings('OnDeckTvAppendShow') == 'true'
appendSxxExx = settings('OnDeckTvAppendSeason') == 'true'
directpaths = settings('useDirectPaths') == 'true'
if settings('OnDeckTVextended') == 'false':
# Chances are that this view is used on Kodi startup
# Wait till we've connected to a PMS. At most 30s
counter = 0
2016-08-30 23:51:11 +10:00
while window('plex_authenticated') != 'true':
counter += 1
if counter >= 300:
2016-08-30 23:51:11 +10:00
log.error('Aborting On Deck view, we were not authenticated '
'for the PMS')
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE, False)
sleep(100)
xml = downloadutils.DownloadUtils().downloadUrl(
'{server}/library/sections/%s/onDeck' % viewid)
if xml in (None, 401):
2016-08-30 23:51:11 +10:00
log.error('Could not download PMS xml for view %s' % viewid)
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE)
limitcounter = 0
for item in xml:
2017-01-25 05:59:38 +11:00
api = API(item)
listitem = api.CreateListItemFromPlexItem(
appendShowTitle=appendShowTitle,
appendSxxExx=appendSxxExx)
2016-05-17 03:27:43 +10:00
if directpaths:
2017-01-25 05:59:38 +11:00
url = api.getFilePath()
else:
params = {
'mode': "play",
2018-02-06 06:44:41 +11:00
'plex_id': api.getRatingKey(),
'plex_type': api.getType()
}
2017-01-03 00:07:24 +11:00
url = "plugin://plugin.video.plexkodiconnect/tvshows/?%s" \
2017-01-25 05:59:38 +11:00
% urlencode(params)
xbmcplugin.addDirectoryItem(
2017-01-25 05:59:38 +11:00
handle=HANDLE,
url=url,
listitem=listitem)
limitcounter += 1
if limitcounter == limit:
break
return xbmcplugin.endOfDirectory(
2017-01-25 05:59:38 +11:00
handle=HANDLE,
2016-08-30 23:51:11 +10:00
cacheToDisc=settings('enableTextureCache') == 'true')
2016-03-15 23:09:51 +11:00
# if the addon is called with nextup parameter,
# we return the nextepisodes list of the given tagname
# First we get a list of all the TV shows - filtered by tag
2017-01-25 05:59:38 +11:00
params = {
'sort': {'order': "descending", 'method': "lastplayed"},
'filter': {
'and': [
{'operator': "true", 'field': "inprogress", 'value': ""},
{'operator': "is", 'field': "tag", 'value': "%s" % tagname}
]}
2016-03-15 23:09:51 +11:00
}
items = js.get_tv_shows(params)
if not items:
# Now items retrieved - empty directory
xbmcplugin.endOfDirectory(handle=HANDLE)
return
2017-01-25 05:59:38 +11:00
params = {
'sort': {'method': "episode"},
'limits': {"end": 1},
'properties': [
"title", "playcount", "season", "episode", "showtitle",
"plot", "file", "rating", "resume", "tvshowid", "art",
"streamdetails", "firstaired", "runtime", "cast", "writer",
"dateadded", "lastplayed"
],
2016-03-15 23:09:51 +11:00
}
2016-08-30 23:51:11 +10:00
if settings('ignoreSpecialsNextEpisodes') == "true":
2017-01-25 05:59:38 +11:00
params['filter'] = {
2016-03-15 23:09:51 +11:00
'and': [
{'operator': "lessthan", 'field': "playcount", 'value': "1"},
{'operator': "greaterthan", 'field': "season", 'value': "0"}
]
}
else:
2017-01-25 05:59:38 +11:00
params['filter'] = {
2016-03-15 23:09:51 +11:00
'or': [
{'operator': "lessthan", 'field': "playcount", 'value': "1"},
{'operator': "true", 'field': "inprogress", 'value': ""}
]
}
2016-03-15 23:09:51 +11:00
# Are there any episodes still in progress/not yet finished watching?!?
# Then we should show this episode, NOT the "next up"
2017-01-25 05:59:38 +11:00
inprog_params = {
'sort': {'method': "episode"},
'filter': {'operator': "true", 'field': "inprogress", 'value': ""},
'properties': params['properties']
2016-03-15 23:09:51 +11:00
}
count = 0
for item in items:
2017-01-25 05:59:38 +11:00
inprog_params['tvshowid'] = item['tvshowid']
2017-12-09 05:43:06 +11:00
episodes = js.get_episodes(inprog_params)
if not episodes:
2016-03-15 23:09:51 +11:00
# No, there are no episodes not yet finished. Get "next up"
2017-01-25 05:59:38 +11:00
params['tvshowid'] = item['tvshowid']
2017-12-09 05:43:06 +11:00
episodes = js.get_episodes(params)
if not episodes:
# Also no episodes currently coming up
continue
2016-03-15 23:09:51 +11:00
for episode in episodes:
# There will always be only 1 episode ('limit=1')
2017-12-09 05:43:06 +11:00
listitem = createListItem(episode,
appendShowTitle=appendShowTitle,
appendSxxExx=appendSxxExx)
xbmcplugin.addDirectoryItem(handle=HANDLE,
url=episode['file'],
listitem=listitem,
isFolder=False)
2016-03-15 23:09:51 +11:00
count += 1
if count >= limit:
break
2017-01-25 05:59:38 +11:00
xbmcplugin.endOfDirectory(handle=HANDLE)
2016-04-17 21:36:41 +10:00
def watchlater():
"""
Listing for plex.tv Watch Later section (if signed in to plex.tv)
"""
2016-08-30 23:51:11 +10:00
if window('plex_token') == '':
log.error('No watch later - not signed in to plex.tv')
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE, False)
if window('plex_restricteduser') == 'true':
2016-08-30 23:51:11 +10:00
log.error('No watch later - restricted user')
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE, False)
2016-04-17 21:36:41 +10:00
xml = downloadutils.DownloadUtils().downloadUrl(
'https://plex.tv/pms/playlists/queue/all',
authenticate=False,
2016-08-30 23:51:11 +10:00
headerOptions={'X-Plex-Token': window('plex_token')})
2016-04-17 21:36:41 +10:00
if xml in (None, 401):
2016-08-30 23:51:11 +10:00
log.error('Could not download watch later list from plex.tv')
2017-01-25 05:59:38 +11:00
return xbmcplugin.endOfDirectory(HANDLE, False)
2016-04-17 21:36:41 +10:00
2016-08-30 23:51:11 +10:00
log.info('Displaying watch later plex.tv items')
2017-01-25 05:59:38 +11:00
xbmcplugin.setContent(HANDLE, 'movies')
2016-04-17 21:36:41 +10:00
for item in xml:
2017-03-08 20:46:10 +11:00
__build_item(item)
2016-04-17 21:36:41 +10:00
xbmcplugin.endOfDirectory(
2017-01-25 05:59:38 +11:00
handle=HANDLE,
2016-08-30 23:51:11 +10:00
cacheToDisc=settings('enableTextureCache') == 'true')
2016-05-30 00:52:00 +10:00
2017-03-08 02:37:36 +11:00
def channels():
"""
Listing for Plex Channels
"""
xml = downloadutils.DownloadUtils().downloadUrl('{server}/channels/all')
try:
xml[0].attrib
2017-05-01 01:07:56 +10:00
except (ValueError, AttributeError, IndexError, TypeError):
2017-03-08 02:37:36 +11:00
log.error('Could not download Plex Channels')
return xbmcplugin.endOfDirectory(HANDLE, False)
log.info('Displaying Plex Channels')
xbmcplugin.setContent(HANDLE, 'files')
2017-03-09 02:21:00 +11:00
for method in v.SORT_METHODS_DIRECTORY:
xbmcplugin.addSortMethod(HANDLE, getattr(xbmcplugin, method))
2017-03-08 02:37:36 +11:00
for item in xml:
__build_folder(item)
xbmcplugin.endOfDirectory(
handle=HANDLE,
cacheToDisc=settings('enableTextureCache') == 'true')
2017-03-09 02:21:00 +11:00
def browse_plex(key=None, plex_section_id=None):
2017-03-08 02:37:36 +11:00
"""
2017-03-09 02:21:00 +11:00
Lists the content of a Plex folder, e.g. channels. Either pass in key (to
be used directly for PMS url {server}<key>) or the plex_section_id
2017-03-08 02:37:36 +11:00
"""
2017-03-09 02:21:00 +11:00
if key:
xml = downloadutils.DownloadUtils().downloadUrl('{server}%s' % key)
else:
2017-04-02 02:28:02 +10:00
xml = GetPlexSectionResults(plex_section_id)
2017-03-08 02:37:36 +11:00
try:
xml[0].attrib
except (ValueError, AttributeError, IndexError, TypeError):
2017-03-08 02:37:36 +11:00
log.error('Could not browse to %s' % key)
return xbmcplugin.endOfDirectory(HANDLE, False)
2017-03-09 02:21:00 +11:00
photos = False
movies = False
clips = False
tvshows = False
episodes = False
songs = False
artists = False
albums = False
musicvideos = False
2017-03-08 02:37:36 +11:00
for item in xml:
if item.tag == 'Directory':
2017-03-09 03:53:43 +11:00
__build_folder(item, plex_section_id=plex_section_id)
2017-03-08 02:37:36 +11:00
else:
2017-03-19 22:14:16 +11:00
typus = item.attrib.get('type')
2017-03-08 02:37:36 +11:00
__build_item(item)
2017-03-09 02:21:00 +11:00
if typus == v.PLEX_TYPE_PHOTO:
photos = True
elif typus == v.PLEX_TYPE_MOVIE:
movies = True
elif typus == v.PLEX_TYPE_CLIP:
clips = True
elif typus in (v.PLEX_TYPE_SHOW, v.PLEX_TYPE_SEASON):
tvshows = True
elif typus == v.PLEX_TYPE_EPISODE:
episodes = True
elif typus == v.PLEX_TYPE_SONG:
songs = True
elif typus == v.PLEX_TYPE_ARTIST:
artists = True
elif typus == v.PLEX_TYPE_ALBUM:
albums = True
elif typus == v.PLEX_TYPE_MUSICVIDEO:
musicvideos = True
# Set the correct content type
2017-03-09 03:02:26 +11:00
if movies is True:
xbmcplugin.setContent(HANDLE, 'movies')
sort_methods = v.SORT_METHODS_MOVIES
2017-03-09 02:21:00 +11:00
elif clips is True:
xbmcplugin.setContent(HANDLE, 'movies')
sort_methods = v.SORT_METHODS_CLIPS
2017-03-09 03:02:26 +11:00
elif photos is True:
2017-03-16 05:26:55 +11:00
xbmcplugin.setContent(HANDLE, 'images')
2017-03-09 03:02:26 +11:00
sort_methods = v.SORT_METHODS_PHOTOS
2017-03-09 02:21:00 +11:00
elif tvshows is True:
xbmcplugin.setContent(HANDLE, 'tvshows')
sort_methods = v.SORT_METHOD_TVSHOWS
elif episodes is True:
xbmcplugin.setContent(HANDLE, 'episodes')
sort_methods = v.SORT_METHODS_EPISODES
elif songs is True:
xbmcplugin.setContent(HANDLE, 'songs')
sort_methods = v.SORT_METHODS_SONGS
elif artists is True:
xbmcplugin.setContent(HANDLE, 'artists')
sort_methods = v.SORT_METHODS_ARTISTS
elif albums is True:
xbmcplugin.setContent(HANDLE, 'albums')
sort_methods = v.SORT_METHODS_ALBUMS
elif musicvideos is True:
xbmcplugin.setContent(HANDLE, 'musicvideos')
sort_methods = v.SORT_METHODS_MOVIES
else:
xbmcplugin.setContent(HANDLE, 'files')
sort_methods = v.SORT_METHODS_DIRECTORY
for method in sort_methods:
xbmcplugin.addSortMethod(HANDLE, getattr(xbmcplugin, method))
# Set the Kodi title for this view
title = xml.attrib.get('librarySectionTitle', xml.attrib.get('title1'))
xbmcplugin.setPluginCategory(HANDLE, title)
2017-03-08 02:37:36 +11:00
xbmcplugin.endOfDirectory(
handle=HANDLE,
cacheToDisc=settings('enableTextureCache') == 'true')
2017-03-09 03:53:43 +11:00
def __build_folder(xml_element, plex_section_id=None):
2017-03-08 20:51:21 +11:00
url = "plugin://%s/" % v.ADDON_ID
2017-03-09 03:53:43 +11:00
key = xml_element.attrib.get('fastKey', xml_element.attrib.get('key'))
if not key.startswith('/'):
key = '/library/sections/%s/%s' % (plex_section_id, key)
2017-03-08 02:37:36 +11:00
params = {
2017-03-09 02:21:00 +11:00
'mode': "browseplex",
2017-03-09 03:53:43 +11:00
'key': key,
'id': plex_section_id
2017-03-08 02:37:36 +11:00
}
listitem = ListItem(xml_element.attrib.get('title'))
listitem.setArt({'thumb': xml_element.attrib.get('thumb'),
'poster': xml_element.attrib.get('art')})
2017-03-08 20:51:21 +11:00
xbmcplugin.addDirectoryItem(handle=HANDLE,
url="%s?%s" % (url, urlencode(params)),
isFolder=True,
listitem=listitem)
2017-03-08 02:37:36 +11:00
def __build_item(xml_element):
api = API(xml_element)
listitem = api.CreateListItemFromPlexItem()
2018-01-29 03:28:02 +11:00
resume = api.getResume()
if resume:
listitem.setProperty('resumetime', str(resume))
if (api.getKey().startswith('/system/services') or
api.getKey().startswith('http')):
2017-03-09 04:24:50 +11:00
params = {
'mode': 'plex_node',
'key': xml_element.attrib.get('key'),
2018-01-29 03:21:28 +11:00
'offset': xml_element.attrib.get('viewOffset', '0'),
2017-03-09 04:24:50 +11:00
}
2017-03-19 22:14:16 +11:00
url = "plugin://%s?%s" % (v.ADDON_ID, urlencode(params))
elif api.getType() == v.PLEX_TYPE_PHOTO:
url = api.get_picture_path()
2017-03-09 04:24:50 +11:00
else:
params = {
2017-03-19 22:14:16 +11:00
'mode': 'play',
2018-01-29 03:21:28 +11:00
'plex_id': api.getRatingKey(),
'plex_type': api.getType(),
}
2017-03-19 22:14:16 +11:00
url = "plugin://%s?%s" % (v.ADDON_ID, urlencode(params))
2017-03-08 20:51:21 +11:00
xbmcplugin.addDirectoryItem(handle=HANDLE,
2017-03-09 04:24:50 +11:00
url=url,
2017-03-08 20:51:21 +11:00
listitem=listitem)
2017-03-08 02:37:36 +11:00
2016-05-30 00:52:00 +10:00
def enterPMS():
"""
Opens dialogs for the user the plug in the PMS details
"""
# "Enter your Plex Media Server's IP or URL. Examples are:"
2017-01-25 05:59:38 +11:00
dialog('ok', lang(29999), lang(39215), '192.168.1.2', 'plex.myServer.org')
ip = dialog('input', "Enter PMS IP or URL")
2016-05-30 00:52:00 +10:00
if ip == '':
return
2017-01-25 05:59:38 +11:00
port = dialog('input', "Enter PMS port", '32400', type='{numeric}')
2016-05-30 00:52:00 +10:00
if port == '':
return
url = '%s:%s' % (ip, port)
# "Does your Plex Media Server support SSL connections?
# (https instead of http)"
2017-01-25 05:59:38 +11:00
https = dialog('yesno', lang(29999), lang(39217))
2016-05-30 00:52:00 +10:00
if https:
url = 'https://%s' % url
else:
url = 'http://%s' % url
https = 'true' if https else 'false'
2017-01-25 05:59:38 +11:00
machineIdentifier = GetMachineIdentifier(url)
2016-05-30 00:52:00 +10:00
if machineIdentifier is None:
# "Error contacting url
# Abort (Yes) or save address anyway (No)"
2017-01-25 05:59:38 +11:00
if dialog('yesno',
lang(29999),
'%s %s. %s' % (lang(39218), url, lang(39219))):
2016-05-30 00:52:00 +10:00
return
else:
2016-08-30 23:51:11 +10:00
settings('plex_machineIdentifier', '')
2016-05-30 00:52:00 +10:00
else:
2016-08-30 23:51:11 +10:00
settings('plex_machineIdentifier', machineIdentifier)
log.info('Set new PMS to https %s, ip %s, port %s, machineIdentifier %s'
2016-08-30 23:51:11 +10:00
% (https, ip, port, machineIdentifier))
settings('https', value=https)
settings('ipaddress', value=ip)
settings('port', value=port)
# Chances are this is a local PMS, so disable SSL certificate check
2016-08-30 23:51:11 +10:00
settings('sslverify', value='false')
2016-05-30 00:52:00 +10:00
# Sign out to trigger new login
if __LogOut():
# Only login again if logout was successful
__LogIn()
def __LogIn():
2016-05-30 00:52:00 +10:00
"""
Resets (clears) window properties to enable (re-)login
2016-05-30 00:52:00 +10:00
SUSPEND_LIBRARY_THREAD is set to False in service.py if user was signed
out!
2016-05-30 00:52:00 +10:00
"""
2017-08-22 02:53:38 +10:00
plex_command('RUN_LIB_SCAN', 'full')
2016-05-30 00:52:00 +10:00
# Restart user client
plex_command('SUSPEND_USER_CLIENT', 'False')
2016-05-30 00:52:00 +10:00
def __LogOut():
2016-05-30 00:52:00 +10:00
"""
Finishes lib scans, logs out user.
2016-05-30 00:52:00 +10:00
Returns True if successfully signed out, False otherwise
"""
# Resetting, please wait
2017-01-25 05:59:38 +11:00
dialog('notification',
lang(29999),
lang(39207),
icon='{plex}',
time=3000,
sound=False)
2016-05-30 00:52:00 +10:00
# Pause library sync thread
plex_command('SUSPEND_LIBRARY_THREAD', 'True')
2016-05-30 00:52:00 +10:00
# Wait max for 10 seconds for all lib scans to shutdown
counter = 0
2016-08-30 23:51:11 +10:00
while window('plex_dbScan') == 'true':
2016-05-30 00:52:00 +10:00
if counter > 200:
# Failed to reset PMS and plex.tv connects. Try to restart Kodi.
2017-01-25 05:59:38 +11:00
dialog('ok', lang(29999), lang(39208))
2016-05-30 00:52:00 +10:00
# Resuming threads, just in case
plex_command('SUSPEND_LIBRARY_THREAD', 'False')
2016-08-30 23:51:11 +10:00
log.error("Could not stop library sync, aborting")
2016-05-30 00:52:00 +10:00
return False
counter += 1
2017-01-25 05:59:38 +11:00
sleep(50)
2016-08-30 23:51:11 +10:00
log.debug("Successfully stopped library sync")
2016-05-30 00:52:00 +10:00
counter = 0
# Log out currently signed in user:
window('plex_serverStatus', value='401')
plex_command('PMS_STATUS', '401')
# Above method needs to have run its course! Hence wait
2016-08-30 23:51:11 +10:00
while window('plex_serverStatus') == "401":
if counter > 100:
# 'Failed to reset PKC. Try to restart Kodi.'
2017-01-25 05:59:38 +11:00
dialog('ok', lang(29999), lang(39208))
2016-08-30 23:51:11 +10:00
log.error("Could not sign out user, aborting")
return False
counter += 1
2017-01-25 05:59:38 +11:00
sleep(50)
# Suspend the user client during procedure
plex_command('SUSPEND_USER_CLIENT', 'True')
2016-05-30 00:52:00 +10:00
return True