2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
import cProfile
|
|
|
|
import inspect
|
2016-03-03 11:25:17 +11:00
|
|
|
import json
|
2015-12-25 07:07:00 +11:00
|
|
|
import pstats
|
|
|
|
import sqlite3
|
2016-03-14 00:50:29 +11:00
|
|
|
from datetime import datetime, timedelta
|
2016-03-17 18:55:00 +11:00
|
|
|
import StringIO
|
2016-03-14 00:50:29 +11:00
|
|
|
import time
|
2015-12-25 07:07:00 +11:00
|
|
|
import unicodedata
|
|
|
|
import xml.etree.ElementTree as etree
|
2016-01-30 06:07:21 +11:00
|
|
|
from functools import wraps
|
2016-01-28 02:33:02 +11:00
|
|
|
from calendar import timegm
|
2016-03-01 20:40:30 +11:00
|
|
|
import os
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcvfs
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-04-07 02:23:51 +10:00
|
|
|
addonName = 'PlexKodiConnect'
|
2016-01-27 03:20:13 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
def DateToKodi(stamp):
|
|
|
|
"""
|
|
|
|
converts a Unix time stamp (seconds passed sinceJanuary 1 1970) to a
|
|
|
|
propper, human-readable time stamp used by Kodi
|
|
|
|
|
|
|
|
Output: Y-m-d h:m:s = 2009-04-05 23:16:04
|
2016-04-02 19:43:50 +11:00
|
|
|
|
|
|
|
None if an error was encountered
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
|
|
|
try:
|
2016-04-02 19:43:50 +11:00
|
|
|
stamp = float(stamp) + float(window('kodiplextimeoffset'))
|
2016-03-28 01:57:20 +11:00
|
|
|
date_time = time.localtime(stamp)
|
2016-03-12 00:42:14 +11:00
|
|
|
localdate = time.strftime('%Y-%m-%d %H:%M:%S', date_time)
|
|
|
|
except:
|
|
|
|
localdate = None
|
|
|
|
return localdate
|
|
|
|
|
|
|
|
|
|
|
|
def changePlayState(itemType, kodiId, playCount, lastplayed):
|
|
|
|
"""
|
|
|
|
YET UNUSED
|
|
|
|
|
|
|
|
kodiId: int or str
|
|
|
|
playCount: int or str
|
|
|
|
lastplayed: str or int unix timestamp
|
|
|
|
"""
|
|
|
|
logMsg("changePlayState", "start", 1)
|
|
|
|
lastplayed = DateToKodi(lastplayed)
|
|
|
|
|
|
|
|
kodiId = int(kodiId)
|
|
|
|
playCount = int(playCount)
|
|
|
|
method = {
|
|
|
|
'movie': ' VideoLibrary.SetMovieDetails',
|
|
|
|
'episode': 'VideoLibrary.SetEpisodeDetails',
|
|
|
|
'musicvideo': ' VideoLibrary.SetMusicVideoDetails', # TODO
|
|
|
|
'show': 'VideoLibrary.SetTVShowDetails', # TODO
|
|
|
|
'': 'AudioLibrary.SetAlbumDetails', # TODO
|
|
|
|
'': 'AudioLibrary.SetArtistDetails', # TODO
|
|
|
|
'track': 'AudioLibrary.SetSongDetails'
|
|
|
|
}
|
|
|
|
params = {
|
|
|
|
'movie': {
|
|
|
|
'movieid': kodiId,
|
|
|
|
'playcount': playCount,
|
|
|
|
'lastplayed': lastplayed
|
|
|
|
},
|
|
|
|
'episode': {
|
|
|
|
'episodeid': kodiId,
|
|
|
|
'playcount': playCount,
|
|
|
|
'lastplayed': lastplayed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
}
|
|
|
|
query['method'] = method[itemType]
|
|
|
|
query['params'] = params[itemType]
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(query))
|
|
|
|
result = json.loads(result)
|
|
|
|
result = result.get('result')
|
|
|
|
logMsg("changePlayState", "JSON result was: %s" % result, 1)
|
|
|
|
|
|
|
|
|
2016-03-08 02:30:50 +11:00
|
|
|
def IfExists(path):
|
|
|
|
"""
|
|
|
|
Kodi's xbmcvfs.exists is broken - it caches the results for directories.
|
|
|
|
|
|
|
|
path: path to a directory (with a slash at the end)
|
|
|
|
|
|
|
|
Returns True if path exists, else false
|
|
|
|
"""
|
2016-05-07 21:15:02 +10:00
|
|
|
dummyfile = tryEncode(os.path.join(path, 'dummyfile.txt'))
|
2016-03-08 02:30:50 +11:00
|
|
|
try:
|
|
|
|
etree.ElementTree(etree.Element('test')).write(dummyfile)
|
|
|
|
except:
|
|
|
|
# folder does not exist yet
|
|
|
|
answer = False
|
|
|
|
else:
|
|
|
|
# Folder exists. Delete file again.
|
|
|
|
xbmcvfs.delete(dummyfile)
|
|
|
|
answer = True
|
|
|
|
return answer
|
|
|
|
|
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
def LogTime(func):
|
2016-01-25 20:36:24 +11:00
|
|
|
"""
|
2016-01-30 06:07:21 +11:00
|
|
|
Decorator for functions and methods to log the time it took to run the code
|
|
|
|
"""
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
starttotal = datetime.now()
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
elapsedtotal = datetime.now() - starttotal
|
|
|
|
logMsg('%s %s' % (addonName, func.__name__),
|
|
|
|
'It took %s to run the function.' % (elapsedtotal), 1)
|
|
|
|
return result
|
|
|
|
return wrapper
|
2016-01-27 01:13:03 +11:00
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
|
|
|
|
def ThreadMethodsAdditionalStop(windowAttribute):
|
2016-01-27 01:13:03 +11:00
|
|
|
"""
|
2016-01-30 06:07:21 +11:00
|
|
|
Decorator to replace stopThread method to include the Kodi windowAttribute
|
|
|
|
|
|
|
|
Use with any sync threads. @ThreadMethods still required FIRST
|
|
|
|
"""
|
|
|
|
def wrapper(cls):
|
|
|
|
def threadStopped(self):
|
|
|
|
return (self._threadStopped or
|
2016-03-23 20:05:29 +11:00
|
|
|
(window('plex_terminateNow') == "true") or
|
2016-01-30 06:07:21 +11:00
|
|
|
window(windowAttribute) == "true")
|
|
|
|
cls.threadStopped = threadStopped
|
|
|
|
return cls
|
|
|
|
return wrapper
|
2016-01-27 01:13:03 +11:00
|
|
|
|
|
|
|
|
2016-01-27 22:18:54 +11:00
|
|
|
def ThreadMethodsAdditionalSuspend(windowAttribute):
|
|
|
|
"""
|
|
|
|
Decorator to replace threadSuspended(): thread now also suspends if a
|
|
|
|
Kodi windowAttribute is set to 'true', e.g. 'suspend_LibraryThread'
|
|
|
|
|
|
|
|
Use with any library sync threads. @ThreadMethods still required FIRST
|
|
|
|
"""
|
|
|
|
def wrapper(cls):
|
|
|
|
def threadSuspended(self):
|
2016-01-30 06:07:21 +11:00
|
|
|
return (self._threadSuspended or
|
|
|
|
window(windowAttribute) == 'true')
|
2016-01-27 22:18:54 +11:00
|
|
|
cls.threadSuspended = threadSuspended
|
|
|
|
return cls
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2016-01-27 01:13:03 +11:00
|
|
|
def ThreadMethods(cls):
|
|
|
|
"""
|
|
|
|
Decorator to add the following methods to a threading class:
|
|
|
|
|
|
|
|
suspendThread(): pauses the thread
|
|
|
|
resumeThread(): resumes the thread
|
2016-02-08 03:26:44 +11:00
|
|
|
stopThread(): stopps/kills the thread
|
2016-01-27 01:13:03 +11:00
|
|
|
|
|
|
|
threadSuspended(): returns True if thread is suspend_thread
|
|
|
|
threadStopped(): returns True if thread is stopped (or should stop ;-))
|
|
|
|
ALSO stops if Kodi is exited
|
|
|
|
|
|
|
|
Also adds the following class attributes:
|
|
|
|
_threadStopped
|
|
|
|
_threadSuspended
|
2016-01-25 20:36:24 +11:00
|
|
|
"""
|
2016-01-27 01:13:03 +11:00
|
|
|
# Attach new attributes to class
|
|
|
|
cls._threadStopped = False
|
|
|
|
cls._threadSuspended = False
|
|
|
|
|
|
|
|
# Define new class methods and attach them to class
|
|
|
|
def stopThread(self):
|
|
|
|
self._threadStopped = True
|
|
|
|
cls.stopThread = stopThread
|
|
|
|
|
|
|
|
def suspendThread(self):
|
|
|
|
self._threadSuspended = True
|
|
|
|
cls.suspendThread = suspendThread
|
|
|
|
|
|
|
|
def resumeThread(self):
|
|
|
|
self._threadSuspended = False
|
|
|
|
cls.resumeThread = resumeThread
|
|
|
|
|
|
|
|
def threadSuspended(self):
|
|
|
|
return self._threadSuspended
|
|
|
|
cls.threadSuspended = threadSuspended
|
|
|
|
|
|
|
|
def threadStopped(self):
|
2016-03-23 20:05:29 +11:00
|
|
|
return self._threadStopped or (window('plex_terminateNow') == 'true')
|
2016-01-27 01:13:03 +11:00
|
|
|
cls.threadStopped = threadStopped
|
2016-01-25 20:36:24 +11:00
|
|
|
|
2016-01-27 01:13:03 +11:00
|
|
|
# Return class to render this a decorator
|
2016-01-25 20:36:24 +11:00
|
|
|
return cls
|
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
def logging(cls):
|
2016-01-23 01:37:20 +11:00
|
|
|
"""
|
2016-01-27 22:18:54 +11:00
|
|
|
A decorator adding logging capabilities to classes.
|
|
|
|
Also adds self.addonName to the class
|
2016-01-23 01:37:20 +11:00
|
|
|
|
|
|
|
Syntax: self.logMsg(message, loglevel)
|
|
|
|
|
|
|
|
Loglevel: -2 (Error) to 2 (DB debug)
|
|
|
|
"""
|
2016-01-27 03:20:13 +11:00
|
|
|
# Attach new attributes to class
|
|
|
|
cls.addonName = addonName
|
|
|
|
|
|
|
|
# Define new class methods and attach them to class
|
|
|
|
def newFunction(self, msg, lvl=0):
|
2016-01-28 02:33:02 +11:00
|
|
|
title = "%s %s" % (addonName, cls.__name__)
|
2016-01-27 03:20:13 +11:00
|
|
|
logMsg(title, msg, lvl)
|
|
|
|
cls.logMsg = newFunction
|
|
|
|
|
|
|
|
# Return class to render this a decorator
|
|
|
|
return cls
|
2016-01-23 01:37:20 +11:00
|
|
|
|
|
|
|
|
2016-02-01 02:13:40 +11:00
|
|
|
def IntFromStr(string):
|
|
|
|
"""
|
|
|
|
Returns an int from string or the int 0 if something happened
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result = int(string)
|
|
|
|
except:
|
|
|
|
result = 0
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-01-28 02:33:02 +11:00
|
|
|
def getUnixTimestamp(secondsIntoTheFuture=None):
|
|
|
|
"""
|
|
|
|
Returns a Unix time stamp (seconds passed since January 1 1970) for NOW as
|
|
|
|
an integer.
|
|
|
|
|
|
|
|
Optionally, pass secondsIntoTheFuture: positive int's will result in a
|
|
|
|
future timestamp, negative the past
|
|
|
|
"""
|
|
|
|
if secondsIntoTheFuture:
|
|
|
|
future = datetime.utcnow() + timedelta(seconds=secondsIntoTheFuture)
|
|
|
|
else:
|
|
|
|
future = datetime.utcnow()
|
|
|
|
return timegm(future.timetuple())
|
|
|
|
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def logMsg(title, msg, level=1):
|
|
|
|
# Get the logLevel set in UserClient
|
|
|
|
try:
|
2016-05-31 16:06:42 +10:00
|
|
|
logLevel = int(window('plex_logLevel'))
|
2015-12-25 07:07:00 +11:00
|
|
|
except ValueError:
|
|
|
|
logLevel = 0
|
2016-03-09 00:02:01 +11:00
|
|
|
kodiLevel = {
|
|
|
|
-1: xbmc.LOGERROR,
|
|
|
|
0: xbmc.LOGNOTICE,
|
|
|
|
1: xbmc.LOGNOTICE,
|
|
|
|
2: xbmc.LOGNOTICE
|
|
|
|
}
|
2015-12-25 07:07:00 +11:00
|
|
|
if logLevel >= level:
|
2016-01-27 03:20:13 +11:00
|
|
|
if logLevel == 2: # inspect is expensive
|
|
|
|
func = inspect.currentframe().f_back.f_back.f_code
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
2016-01-27 03:20:13 +11:00
|
|
|
xbmc.log("%s -> %s : %s" % (
|
2016-03-09 00:02:01 +11:00
|
|
|
title, func.co_name, msg), level=kodiLevel[level])
|
2015-12-25 07:07:00 +11:00
|
|
|
except UnicodeEncodeError:
|
2016-01-30 06:07:21 +11:00
|
|
|
try:
|
|
|
|
xbmc.log("%s -> %s : %s" % (
|
2016-05-07 21:15:02 +10:00
|
|
|
title, func.co_name, tryEncode(msg)),
|
2016-03-09 00:02:01 +11:00
|
|
|
level=kodiLevel[level])
|
2016-01-30 06:07:21 +11:00
|
|
|
except:
|
2016-03-09 00:02:01 +11:00
|
|
|
xbmc.log("%s -> %s : %s" % (
|
|
|
|
title, func.co_name, 'COULDNT LOG'),
|
|
|
|
level=kodiLevel[level])
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
try:
|
2016-03-09 00:02:01 +11:00
|
|
|
xbmc.log("%s -> %s" % (title, msg), level=kodiLevel[level])
|
2015-12-25 07:07:00 +11:00
|
|
|
except UnicodeEncodeError:
|
2016-01-30 06:07:21 +11:00
|
|
|
try:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmc.log("%s -> %s" % (title, tryEncode(msg)),
|
2016-03-09 00:02:01 +11:00
|
|
|
level=kodiLevel[level])
|
2016-01-30 06:07:21 +11:00
|
|
|
except:
|
2016-03-09 00:02:01 +11:00
|
|
|
xbmc.log("%s -> %s " % (title, 'COULDNT LOG'),
|
|
|
|
level=kodiLevel[level])
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def window(property, value=None, clear=False, windowid=10000):
|
2016-03-08 23:00:03 +11:00
|
|
|
"""
|
|
|
|
Get or set window property - thread safe!
|
|
|
|
|
|
|
|
Returns unicode.
|
|
|
|
|
|
|
|
Property needs to be string; value may be string or unicode
|
|
|
|
"""
|
2015-12-25 07:07:00 +11:00
|
|
|
WINDOW = xbmcgui.Window(windowid)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-21 21:15:31 +11:00
|
|
|
#setproperty accepts both string and unicode but utf-8 strings are adviced by kodi devs because some unicode can give issues
|
2015-12-25 07:07:00 +11:00
|
|
|
if clear:
|
|
|
|
WINDOW.clearProperty(property)
|
|
|
|
elif value is not None:
|
2016-05-07 21:15:02 +10:00
|
|
|
WINDOW.setProperty(property, tryEncode(value))
|
2016-03-08 23:00:03 +11:00
|
|
|
else:
|
2016-05-07 21:15:02 +10:00
|
|
|
return tryDecode(WINDOW.getProperty(property))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def settings(setting, value=None):
|
2016-03-07 23:01:45 +11:00
|
|
|
"""
|
2016-03-08 23:00:03 +11:00
|
|
|
Get or add addon setting. Returns unicode
|
2016-03-07 23:01:45 +11:00
|
|
|
|
|
|
|
Settings needs to be string
|
|
|
|
Value can either be unicode or string
|
|
|
|
"""
|
2015-12-27 21:49:14 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
|
2016-03-07 23:01:45 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
if value is not None:
|
2016-03-07 19:47:30 +11:00
|
|
|
# Takes string or unicode by default!
|
2016-05-07 21:15:02 +10:00
|
|
|
addon.setSetting(setting, tryEncode(value))
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2016-03-08 23:00:03 +11:00
|
|
|
# Should return unicode by default, but just in case
|
2016-05-07 21:15:02 +10:00
|
|
|
return tryDecode(addon.getSetting(setting))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def language(stringid):
|
|
|
|
# Central string retrieval
|
2015-12-27 21:49:14 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
|
2016-01-18 19:45:39 +11:00
|
|
|
string = addon.getLocalizedString(stringid) #returns unicode object
|
2015-12-25 07:07:00 +11:00
|
|
|
return string
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
def kodiSQL(media_type="video"):
|
|
|
|
|
|
|
|
if media_type == "emby":
|
2016-05-07 21:15:02 +10:00
|
|
|
dbPath = tryDecode(xbmc.translatePath("special://database/emby.db"))
|
2016-04-26 21:53:19 +10:00
|
|
|
elif media_type == "music":
|
2015-12-25 07:07:00 +11:00
|
|
|
dbPath = getKodiMusicDBPath()
|
2016-04-26 21:53:19 +10:00
|
|
|
elif media_type == "texture":
|
2016-05-07 21:15:02 +10:00
|
|
|
dbPath = tryDecode(xbmc.translatePath(
|
|
|
|
"special://database/Textures13.db"))
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
dbPath = getKodiVideoDBPath()
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
connection = sqlite3.connect(dbPath)
|
|
|
|
return connection
|
|
|
|
|
|
|
|
def getKodiVideoDBPath():
|
|
|
|
|
|
|
|
dbVersion = {
|
|
|
|
|
|
|
|
"13": 78, # Gotham
|
|
|
|
"14": 90, # Helix
|
|
|
|
"15": 93, # Isengard
|
2016-03-25 07:18:17 +11:00
|
|
|
"16": 99, # Jarvis
|
2016-06-19 23:01:25 +10:00
|
|
|
"17": 106 # Krypton
|
2015-12-25 07:07:00 +11:00
|
|
|
}
|
|
|
|
|
2016-05-07 21:15:02 +10:00
|
|
|
dbPath = tryDecode(xbmc.translatePath(
|
|
|
|
"special://database/MyVideos%s.db"
|
|
|
|
% dbVersion.get(xbmc.getInfoLabel('System.BuildVersion')[:2], "")))
|
2015-12-25 07:07:00 +11:00
|
|
|
return dbPath
|
|
|
|
|
|
|
|
def getKodiMusicDBPath():
|
|
|
|
|
|
|
|
dbVersion = {
|
|
|
|
|
|
|
|
"13": 46, # Gotham
|
|
|
|
"14": 48, # Helix
|
|
|
|
"15": 52, # Isengard
|
2016-03-25 07:18:17 +11:00
|
|
|
"16": 56, # Jarvis
|
|
|
|
"17": 60 # Krypton
|
2015-12-25 07:07:00 +11:00
|
|
|
}
|
|
|
|
|
2016-05-07 21:15:02 +10:00
|
|
|
dbPath = tryDecode(xbmc.translatePath(
|
|
|
|
"special://database/MyMusic%s.db"
|
|
|
|
% dbVersion.get(xbmc.getInfoLabel('System.BuildVersion')[:2], "")))
|
2015-12-25 07:07:00 +11:00
|
|
|
return dbPath
|
|
|
|
|
2016-03-03 11:25:17 +11:00
|
|
|
def getScreensaver():
|
|
|
|
# Get the current screensaver value
|
|
|
|
query = {
|
|
|
|
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 0,
|
|
|
|
'method': "Settings.getSettingValue",
|
|
|
|
'params': {
|
|
|
|
|
|
|
|
'setting': "screensaver.mode"
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 21:53:19 +10:00
|
|
|
return json.loads(xbmc.executeJSONRPC(json.dumps(query)))['result']['value']
|
2016-03-03 11:25:17 +11:00
|
|
|
|
|
|
|
def setScreensaver(value):
|
|
|
|
# Toggle the screensaver
|
|
|
|
query = {
|
|
|
|
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 0,
|
|
|
|
'method': "Settings.setSettingValue",
|
|
|
|
'params': {
|
|
|
|
|
|
|
|
'setting': "screensaver.mode",
|
|
|
|
'value': value
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 21:53:19 +10:00
|
|
|
logMsg("PLEX", "Toggling screensaver: %s %s" % (value, xbmc.executeJSONRPC(json.dumps(query))), 1)
|
2016-03-03 11:25:17 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def reset():
|
|
|
|
|
|
|
|
dialog = xbmcgui.Dialog()
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
if dialog.yesno("Warning", "Are you sure you want to reset your local Kodi database?") == 0:
|
2015-12-25 07:07:00 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
# first stop any db sync
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_shouldStop', value="true")
|
2015-12-25 07:07:00 +11:00
|
|
|
count = 10
|
2016-05-31 16:06:42 +10:00
|
|
|
while window('plex_dbScan') == "true":
|
2016-03-04 23:34:30 +11:00
|
|
|
logMsg("PLEX", "Sync is running, will retry: %s..." % count)
|
2015-12-25 07:07:00 +11:00
|
|
|
count -= 1
|
|
|
|
if count == 0:
|
|
|
|
dialog.ok("Warning", "Could not stop the database from running. Try again.")
|
|
|
|
return
|
|
|
|
xbmc.sleep(1000)
|
|
|
|
|
|
|
|
# Clean up the playlists
|
2015-12-26 20:42:13 +11:00
|
|
|
deletePlaylists()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
# Clean up the video nodes
|
2016-01-23 10:12:23 +11:00
|
|
|
deleteNodes()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
# Wipe the kodi databases
|
2016-05-07 05:09:35 +10:00
|
|
|
logMsg("Plex", "Resetting the Kodi video database.", 0)
|
2015-12-25 07:07:00 +11:00
|
|
|
connection = kodiSQL('video')
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
tablename = row[0]
|
|
|
|
if tablename != "version":
|
|
|
|
cursor.execute("DELETE FROM " + tablename)
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
|
|
|
|
2016-01-01 10:16:16 +11:00
|
|
|
if settings('enableMusic') == "true":
|
2016-05-07 05:09:35 +10:00
|
|
|
logMsg("Plex", "Resetting the Kodi music database.")
|
2015-12-25 07:07:00 +11:00
|
|
|
connection = kodiSQL('music')
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
tablename = row[0]
|
|
|
|
if tablename != "version":
|
|
|
|
cursor.execute("DELETE FROM " + tablename)
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
|
|
|
|
2016-05-07 05:09:35 +10:00
|
|
|
# Wipe the Plex database
|
|
|
|
logMsg("Plex", "Resetting the Emby database.", 0)
|
2015-12-25 07:07:00 +11:00
|
|
|
connection = kodiSQL('emby')
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
tablename = row[0]
|
|
|
|
if tablename != "version":
|
|
|
|
cursor.execute("DELETE FROM " + tablename)
|
2016-03-01 10:11:17 +11:00
|
|
|
cursor.execute('DROP table IF EXISTS emby')
|
|
|
|
cursor.execute('DROP table IF EXISTS view')
|
2015-12-25 07:07:00 +11:00
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
2016-03-01 10:11:17 +11:00
|
|
|
|
|
|
|
# Offer to wipe cached thumbnails
|
2016-04-26 21:53:19 +10:00
|
|
|
resp = dialog.yesno("Warning", "Remove all cached artwork?")
|
2016-03-01 10:11:17 +11:00
|
|
|
if resp:
|
|
|
|
logMsg("EMBY", "Resetting all cached artwork.", 0)
|
|
|
|
# Remove all existing textures first
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://thumbnails/"))
|
2016-03-01 10:11:17 +11:00
|
|
|
if xbmcvfs.exists(path):
|
|
|
|
allDirs, allFiles = xbmcvfs.listdir(path)
|
|
|
|
for dir in allDirs:
|
|
|
|
allDirs, allFiles = xbmcvfs.listdir(path+dir)
|
|
|
|
for file in allFiles:
|
|
|
|
if os.path.supports_unicode_filenames:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(os.path.join(
|
|
|
|
path + tryDecode(dir),
|
|
|
|
tryDecode(file)))
|
2016-03-01 10:11:17 +11:00
|
|
|
else:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(os.path.join(
|
|
|
|
tryEncode(path) + dir,
|
|
|
|
file))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-03-01 10:11:17 +11:00
|
|
|
# remove all existing data from texture DB
|
|
|
|
connection = kodiSQL('texture')
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
tableName = row[0]
|
|
|
|
if(tableName != "version"):
|
|
|
|
cursor.execute("DELETE FROM " + tableName)
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
2016-04-26 21:53:19 +10:00
|
|
|
|
|
|
|
# reset the install run flag
|
2015-12-25 07:07:00 +11:00
|
|
|
settings('SyncInstallRunDone', value="false")
|
|
|
|
|
|
|
|
# Remove emby info
|
2016-04-16 05:44:54 +10:00
|
|
|
resp = dialog.yesno("Warning", "Reset all Plex KodiConnect Addon settings?")
|
2016-03-01 10:11:17 +11:00
|
|
|
if resp:
|
2015-12-25 07:07:00 +11:00
|
|
|
# Delete the settings
|
|
|
|
addon = xbmcaddon.Addon()
|
2016-05-07 21:15:02 +10:00
|
|
|
addondir = tryDecode(xbmc.translatePath(addon.getAddonInfo('profile')))
|
2015-12-25 07:07:00 +11:00
|
|
|
dataPath = "%ssettings.xml" % addondir
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(tryEncode(dataPath))
|
2016-03-08 01:31:07 +11:00
|
|
|
logMsg("PLEX", "Deleting: settings.xml", 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
dialog.ok(
|
2016-03-08 21:58:14 +11:00
|
|
|
heading=addonName,
|
2015-12-25 07:07:00 +11:00
|
|
|
line1="Database reset has completed, Kodi will now restart to apply the changes.")
|
|
|
|
xbmc.executebuiltin('RestartApp')
|
|
|
|
|
2016-03-17 18:55:00 +11:00
|
|
|
def profiling(sortby="cumulative"):
|
|
|
|
# Will print results to Kodi log
|
|
|
|
def decorator(func):
|
|
|
|
def wrapper(*args, **kwargs):
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-03-17 18:55:00 +11:00
|
|
|
pr = cProfile.Profile()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 18:55:00 +11:00
|
|
|
pr.enable()
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
pr.disable()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 18:55:00 +11:00
|
|
|
s = StringIO.StringIO()
|
|
|
|
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
|
|
|
|
ps.print_stats()
|
|
|
|
logMsg("EMBY Profiling", s.getvalue(), 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 18:55:00 +11:00
|
|
|
return result
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-05 12:09:47 +11:00
|
|
|
def convertdate(date):
|
|
|
|
try:
|
|
|
|
date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
except TypeError:
|
|
|
|
# TypeError: attribute of type 'NoneType' is not callable
|
|
|
|
# Known Kodi/python error
|
|
|
|
date = datetime(*(time.strptime(date, "%Y-%m-%dT%H:%M:%SZ")[0:6]))
|
|
|
|
|
|
|
|
return date
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def normalize_nodes(text):
|
|
|
|
# For video nodes
|
|
|
|
text = text.replace(":", "")
|
|
|
|
text = text.replace("/", "-")
|
|
|
|
text = text.replace("\\", "-")
|
|
|
|
text = text.replace("<", "")
|
|
|
|
text = text.replace(">", "")
|
|
|
|
text = text.replace("*", "")
|
|
|
|
text = text.replace("?", "")
|
|
|
|
text = text.replace('|', "")
|
|
|
|
text = text.replace('(', "")
|
|
|
|
text = text.replace(')', "")
|
|
|
|
text = text.strip()
|
|
|
|
# Remove dots from the last character as windows can not have directories
|
|
|
|
# with dots at the end
|
|
|
|
text = text.rstrip('.')
|
2016-05-07 21:15:02 +10:00
|
|
|
text = tryEncode(unicodedata.normalize('NFKD', unicode(text, 'utf-8')))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
return text
|
|
|
|
|
|
|
|
def normalize_string(text):
|
|
|
|
# For theme media, do not modify unless
|
|
|
|
# modified in TV Tunes
|
|
|
|
text = text.replace(":", "")
|
|
|
|
text = text.replace("/", "-")
|
|
|
|
text = text.replace("\\", "-")
|
|
|
|
text = text.replace("<", "")
|
|
|
|
text = text.replace(">", "")
|
|
|
|
text = text.replace("*", "")
|
|
|
|
text = text.replace("?", "")
|
|
|
|
text = text.replace('|', "")
|
|
|
|
text = text.strip()
|
|
|
|
# Remove dots from the last character as windows can not have directories
|
|
|
|
# with dots at the end
|
|
|
|
text = text.rstrip('.')
|
2016-05-07 21:15:02 +10:00
|
|
|
text = tryEncode(unicodedata.normalize('NFKD', unicode(text, 'utf-8')))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
def indent(elem, level=0):
|
|
|
|
# Prettify xml trees
|
|
|
|
i = "\n" + level*" "
|
|
|
|
if len(elem):
|
|
|
|
if not elem.text or not elem.text.strip():
|
|
|
|
elem.text = i + " "
|
|
|
|
if not elem.tail or not elem.tail.strip():
|
|
|
|
elem.tail = i
|
|
|
|
for elem in elem:
|
|
|
|
indent(elem, level+1)
|
|
|
|
if not elem.tail or not elem.tail.strip():
|
|
|
|
elem.tail = i
|
|
|
|
else:
|
|
|
|
if level and (not elem.tail or not elem.tail.strip()):
|
|
|
|
elem.tail = i
|
|
|
|
|
2016-03-22 23:25:30 +11:00
|
|
|
|
2016-04-22 20:34:35 +10:00
|
|
|
def guisettingsXML():
|
2016-03-22 23:25:30 +11:00
|
|
|
"""
|
2016-04-22 20:34:35 +10:00
|
|
|
Returns special://userdata/guisettings.xml as an etree xml root element
|
2016-03-22 23:25:30 +11:00
|
|
|
"""
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://profile/"))
|
2016-03-22 23:25:30 +11:00
|
|
|
xmlpath = "%sguisettings.xml" % path
|
|
|
|
|
|
|
|
try:
|
|
|
|
xmlparse = etree.parse(xmlpath)
|
|
|
|
except:
|
|
|
|
# Document is blank or missing
|
|
|
|
root = etree.Element('settings')
|
|
|
|
else:
|
|
|
|
root = xmlparse.getroot()
|
2016-04-22 20:34:35 +10:00
|
|
|
return root
|
2016-03-22 23:25:30 +11:00
|
|
|
|
|
|
|
|
2016-04-22 20:34:35 +10:00
|
|
|
def __setXMLTag(element, tag, value, attrib=None):
|
|
|
|
"""
|
|
|
|
Looks for an element's subelement and sets its value.
|
|
|
|
If "subelement" does not exist, create it using attrib and value.
|
|
|
|
|
|
|
|
element : etree element
|
|
|
|
tag : string/unicode for subelement
|
|
|
|
value : string/unicode
|
|
|
|
attrib : dict; will use etree attrib method
|
|
|
|
|
|
|
|
Returns the subelement
|
|
|
|
"""
|
|
|
|
subelement = element.find(tag)
|
|
|
|
if subelement is None:
|
2016-03-22 23:25:30 +11:00
|
|
|
# Setting does not exist yet; create it
|
2016-04-22 20:34:35 +10:00
|
|
|
if attrib is None:
|
|
|
|
etree.SubElement(element, tag).text = value
|
|
|
|
else:
|
|
|
|
etree.SubElement(element, tag, attrib=attrib).text = value
|
2016-03-22 23:25:30 +11:00
|
|
|
else:
|
2016-04-22 20:34:35 +10:00
|
|
|
subelement.text = value
|
|
|
|
return subelement
|
2016-03-22 23:25:30 +11:00
|
|
|
|
|
|
|
|
2016-04-22 20:34:35 +10:00
|
|
|
def __setSubElement(element, subelement):
|
2016-03-30 05:03:53 +11:00
|
|
|
"""
|
2016-04-22 20:34:35 +10:00
|
|
|
Returns an etree element's subelement. Creates one if not exist
|
2016-03-30 05:03:53 +11:00
|
|
|
"""
|
2016-04-22 20:34:35 +10:00
|
|
|
answ = element.find(subelement)
|
|
|
|
if answ is None:
|
|
|
|
answ = etree.SubElement(element, subelement)
|
|
|
|
return answ
|
2016-03-30 05:03:53 +11:00
|
|
|
|
|
|
|
|
2016-03-22 23:25:30 +11:00
|
|
|
def advancedSettingsXML():
|
|
|
|
"""
|
2016-04-22 20:34:35 +10:00
|
|
|
Kodi tweaks
|
2016-03-22 23:25:30 +11:00
|
|
|
|
|
|
|
Changes advancedsettings.xml, musiclibrary:
|
|
|
|
backgroundupdate set to "true"
|
2016-04-22 20:34:35 +10:00
|
|
|
|
|
|
|
Overrides guisettings.xml in Kodi userdata folder:
|
|
|
|
updateonstartup : set to "false"
|
|
|
|
usetags : set to "false"
|
|
|
|
findremotethumbs : set to "false"
|
2016-03-22 23:25:30 +11:00
|
|
|
"""
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://profile/"))
|
2016-03-22 23:25:30 +11:00
|
|
|
xmlpath = "%sadvancedsettings.xml" % path
|
|
|
|
|
|
|
|
try:
|
|
|
|
xmlparse = etree.parse(xmlpath)
|
|
|
|
except:
|
|
|
|
# Document is blank or missing
|
|
|
|
root = etree.Element('advancedsettings')
|
|
|
|
else:
|
|
|
|
root = xmlparse.getroot()
|
|
|
|
|
2016-04-22 20:34:35 +10:00
|
|
|
music = __setSubElement(root, 'musiclibrary')
|
|
|
|
__setXMLTag(music, 'backgroundupdate', "true")
|
|
|
|
# __setXMLTag(music, 'updateonstartup', "false")
|
2016-03-22 23:25:30 +11:00
|
|
|
|
2016-04-22 20:34:35 +10:00
|
|
|
# Subtag 'musicfiles'
|
|
|
|
# music = __setSubElement(root, 'musicfiles')
|
|
|
|
# __setXMLTag(music, 'usetags', "false")
|
|
|
|
# __setXMLTag(music, 'findremotethumbs', "false")
|
2016-03-22 23:25:30 +11:00
|
|
|
|
|
|
|
# Prettify and write to file
|
|
|
|
try:
|
|
|
|
indent(root)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
etree.ElementTree(root).write(xmlpath)
|
|
|
|
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def sourcesXML():
|
|
|
|
# To make Master lock compatible
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://profile/"))
|
2015-12-25 07:07:00 +11:00
|
|
|
xmlpath = "%ssources.xml" % path
|
|
|
|
|
|
|
|
try:
|
|
|
|
xmlparse = etree.parse(xmlpath)
|
|
|
|
except: # Document is blank or missing
|
|
|
|
root = etree.Element('sources')
|
|
|
|
else:
|
|
|
|
root = xmlparse.getroot()
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
video = root.find('video')
|
|
|
|
if video is None:
|
|
|
|
video = etree.SubElement(root, 'video')
|
|
|
|
etree.SubElement(video, 'default', attrib={'pathversion': "1"})
|
|
|
|
|
|
|
|
# Add elements
|
2016-02-04 20:41:39 +11:00
|
|
|
count = 2
|
|
|
|
for source in root.findall('.//path'):
|
|
|
|
if source.text == "smb://":
|
|
|
|
count -= 1
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-02-04 20:41:39 +11:00
|
|
|
if count == 0:
|
|
|
|
# sources already set
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Missing smb:// occurences, re-add.
|
|
|
|
for i in range(0, count):
|
|
|
|
source = etree.SubElement(video, 'source')
|
2016-04-08 21:57:55 +10:00
|
|
|
etree.SubElement(source, 'name').text = "Plex"
|
2016-02-04 20:41:39 +11:00
|
|
|
etree.SubElement(source, 'path', attrib={'pathversion': "1"}).text = "smb://"
|
|
|
|
etree.SubElement(source, 'allowsharing').text = "true"
|
2015-12-25 07:07:00 +11:00
|
|
|
# Prettify and write to file
|
|
|
|
try:
|
|
|
|
indent(root)
|
|
|
|
except: pass
|
|
|
|
etree.ElementTree(root).write(xmlpath)
|
|
|
|
|
|
|
|
|
2016-03-16 19:55:19 +11:00
|
|
|
def passwordsXML():
|
2015-12-25 07:07:00 +11:00
|
|
|
# To add network credentials
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://userdata/"))
|
2015-12-25 07:07:00 +11:00
|
|
|
xmlpath = "%spasswords.xml" % path
|
2016-03-16 19:55:19 +11:00
|
|
|
logMsg('passwordsXML', 'Path to passwords.xml: %s' % xmlpath, 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
try:
|
|
|
|
xmlparse = etree.parse(xmlpath)
|
|
|
|
except: # Document is blank or missing
|
|
|
|
root = etree.Element('passwords')
|
2016-03-16 19:55:19 +11:00
|
|
|
skipFind = True
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
root = xmlparse.getroot()
|
2016-03-16 19:55:19 +11:00
|
|
|
skipFind = False
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
dialog = xbmcgui.Dialog()
|
|
|
|
credentials = settings('networkCreds')
|
|
|
|
if credentials:
|
|
|
|
# Present user with options
|
2016-03-16 19:55:19 +11:00
|
|
|
option = dialog.select(
|
|
|
|
"Modify/Remove network credentials", ["Modify", "Remove"])
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if option < 0:
|
|
|
|
# User cancelled dialog
|
|
|
|
return
|
|
|
|
|
|
|
|
elif option == 1:
|
|
|
|
# User selected remove
|
2016-04-26 21:53:19 +10:00
|
|
|
for paths in root.getiterator('passwords'):
|
2015-12-25 07:07:00 +11:00
|
|
|
for path in paths:
|
|
|
|
if path.find('.//from').text == "smb://%s/" % credentials:
|
|
|
|
paths.remove(path)
|
2016-03-16 19:55:19 +11:00
|
|
|
logMsg("passwordsXML",
|
|
|
|
"Successfully removed credentials for: %s"
|
|
|
|
% credentials, 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
etree.ElementTree(root).write(xmlpath)
|
|
|
|
break
|
|
|
|
else:
|
2016-05-07 05:09:35 +10:00
|
|
|
logMsg("Plex", "Failed to find saved server: %s in passwords.xml" % credentials, 1)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
settings('networkCreds', value="")
|
|
|
|
xbmcgui.Dialog().notification(
|
2016-03-16 19:55:19 +11:00
|
|
|
heading='PlexKodiConnect',
|
|
|
|
message="%s removed from passwords.xml" % credentials,
|
|
|
|
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
|
|
|
|
time=1000,
|
|
|
|
sound=False)
|
2015-12-25 07:07:00 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
elif option == 0:
|
|
|
|
# User selected to modify
|
|
|
|
server = dialog.input("Modify the computer name or ip address", credentials)
|
|
|
|
if not server:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
# No credentials added
|
|
|
|
dialog.ok(
|
|
|
|
heading="Network credentials",
|
|
|
|
line1= (
|
2016-01-25 20:36:24 +11:00
|
|
|
"Input the server name or IP address as indicated in your plex library paths. "
|
2016-03-16 19:55:19 +11:00
|
|
|
'For example, the server name: \\\\SERVER-PC\\path\\ or smb://SERVER-PC/path is "SERVER-PC".'))
|
2016-01-05 16:06:47 +11:00
|
|
|
server = dialog.input("Enter the server name or IP address")
|
2015-12-25 07:07:00 +11:00
|
|
|
if not server:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Network username
|
|
|
|
user = dialog.input("Enter the network username")
|
|
|
|
if not user:
|
|
|
|
return
|
|
|
|
# Network password
|
2016-03-16 19:55:19 +11:00
|
|
|
password = dialog.input("Enter the network password",
|
|
|
|
'', # Default input
|
|
|
|
xbmcgui.INPUT_ALPHANUM,
|
|
|
|
xbmcgui.ALPHANUM_HIDE_INPUT)
|
|
|
|
|
|
|
|
# Add elements. Annoying etree bug where findall hangs forever
|
|
|
|
if skipFind is False:
|
|
|
|
skipFind = True
|
|
|
|
for path in root.findall('.//path'):
|
|
|
|
if path.find('.//from').text.lower() == "smb://%s/" % server.lower():
|
|
|
|
# Found the server, rewrite credentials
|
|
|
|
path.find('.//to').text = "smb://%s:%s@%s/" % (user, password, server)
|
|
|
|
skipFind = False
|
|
|
|
break
|
|
|
|
if skipFind:
|
2015-12-25 07:07:00 +11:00
|
|
|
# Server not found, add it.
|
|
|
|
path = etree.SubElement(root, 'path')
|
|
|
|
etree.SubElement(path, 'from', attrib={'pathversion': "1"}).text = "smb://%s/" % server
|
|
|
|
topath = "smb://%s:%s@%s/" % (user, password, server)
|
|
|
|
etree.SubElement(path, 'to', attrib={'pathversion': "1"}).text = topath
|
|
|
|
# Force Kodi to see the credentials without restarting
|
|
|
|
xbmcvfs.exists(topath)
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
# Add credentials
|
2015-12-25 07:07:00 +11:00
|
|
|
settings('networkCreds', value="%s" % server)
|
2016-03-16 06:26:45 +11:00
|
|
|
logMsg("PLEX", "Added server: %s to passwords.xml" % server, 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
# Prettify and write to file
|
|
|
|
try:
|
|
|
|
indent(root)
|
|
|
|
except: pass
|
|
|
|
etree.ElementTree(root).write(xmlpath)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-03-16 06:26:45 +11:00
|
|
|
# dialog.notification(
|
|
|
|
# heading="PlexKodiConnect",
|
|
|
|
# message="Added to passwords.xml",
|
|
|
|
# icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
|
|
|
|
# time=5000,
|
|
|
|
# sound=False)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-23 17:00:24 +11:00
|
|
|
def playlistXSP(mediatype, tagname, viewid, viewtype="", delete=False):
|
2016-03-08 01:31:07 +11:00
|
|
|
"""
|
|
|
|
Feed with tagname as unicode
|
|
|
|
"""
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://profile/playlists/video/"))
|
2015-12-25 07:07:00 +11:00
|
|
|
if viewtype == "mixed":
|
|
|
|
plname = "%s - %s" % (tagname, mediatype)
|
2016-03-03 03:27:21 +11:00
|
|
|
xsppath = "%sPlex %s - %s.xsp" % (path, viewid, mediatype)
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
plname = tagname
|
2016-03-03 03:27:21 +11:00
|
|
|
xsppath = "%sPlex %s.xsp" % (path, viewid)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
# Create the playlist directory
|
2016-05-07 21:15:02 +10:00
|
|
|
if not xbmcvfs.exists(tryEncode(path)):
|
2016-03-03 03:27:21 +11:00
|
|
|
logMsg("PLEX", "Creating directory: %s" % path, 1)
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.mkdirs(tryEncode(path))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
# Only add the playlist if it doesn't already exists
|
2016-05-07 21:15:02 +10:00
|
|
|
if xbmcvfs.exists(tryEncode(xsppath)):
|
2016-03-08 01:31:07 +11:00
|
|
|
logMsg('Path %s does exist' % xsppath, 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
if delete:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(tryEncode(xsppath))
|
2016-03-03 03:27:21 +11:00
|
|
|
logMsg("PLEX", "Successfully removed playlist: %s." % tagname, 1)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
# Using write process since there's no guarantee the xml declaration works with etree
|
|
|
|
itemtypes = {
|
2016-04-14 01:11:25 +10:00
|
|
|
'homevideos': 'movies',
|
|
|
|
'movie': 'movies',
|
|
|
|
'show': 'tvshows'
|
2015-12-25 07:07:00 +11:00
|
|
|
}
|
2016-03-03 03:27:21 +11:00
|
|
|
logMsg("Plex", "Writing playlist file to: %s" % xsppath, 1)
|
2015-12-26 20:09:47 +11:00
|
|
|
try:
|
2016-05-07 21:15:02 +10:00
|
|
|
f = xbmcvfs.File(tryEncode(xsppath), 'wb')
|
2015-12-26 20:09:47 +11:00
|
|
|
except:
|
2016-03-03 03:27:21 +11:00
|
|
|
logMsg("Plex", "Failed to create playlist: %s" % xsppath, -1)
|
2015-12-26 20:09:47 +11:00
|
|
|
return
|
|
|
|
else:
|
2016-05-07 21:15:02 +10:00
|
|
|
f.write(tryEncode(
|
2015-12-26 20:09:47 +11:00
|
|
|
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n'
|
|
|
|
'<smartplaylist type="%s">\n\t'
|
2016-03-03 03:27:21 +11:00
|
|
|
'<name>Plex %s</name>\n\t'
|
2015-12-26 20:09:47 +11:00
|
|
|
'<match>all</match>\n\t'
|
|
|
|
'<rule field="tag" operator="is">\n\t\t'
|
|
|
|
'<value>%s</value>\n\t'
|
2016-03-08 01:31:07 +11:00
|
|
|
'</rule>\n'
|
2016-04-14 01:11:25 +10:00
|
|
|
'</smartplaylist>\n'
|
2016-05-07 21:15:02 +10:00
|
|
|
% (itemtypes.get(mediatype, mediatype), plname, tagname)))
|
2015-12-26 20:09:47 +11:00
|
|
|
f.close()
|
2016-03-03 03:27:21 +11:00
|
|
|
logMsg("Plex", "Successfully added playlist: %s" % tagname)
|
2015-12-26 20:42:13 +11:00
|
|
|
|
|
|
|
def deletePlaylists():
|
|
|
|
|
|
|
|
# Clean up the playlists
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://profile/playlists/video/"))
|
|
|
|
dirs, files = xbmcvfs.listdir(tryEncode(path))
|
2015-12-26 20:42:13 +11:00
|
|
|
for file in files:
|
2016-05-07 21:15:02 +10:00
|
|
|
if tryDecode(file).startswith('Plex'):
|
|
|
|
xbmcvfs.delete(tryEncode("%s%s" % (path, tryDecode(file))))
|
2016-01-23 10:12:23 +11:00
|
|
|
|
|
|
|
def deleteNodes():
|
|
|
|
|
|
|
|
# Clean up video nodes
|
|
|
|
import shutil
|
2016-05-07 21:15:02 +10:00
|
|
|
path = tryDecode(xbmc.translatePath("special://profile/library/video/"))
|
|
|
|
dirs, files = xbmcvfs.listdir(tryEncode(path))
|
2016-01-23 10:12:23 +11:00
|
|
|
for dir in dirs:
|
2016-05-07 21:15:02 +10:00
|
|
|
if tryDecode(dir).startswith('Plex'):
|
2016-02-24 11:40:13 +11:00
|
|
|
try:
|
2016-05-07 21:15:02 +10:00
|
|
|
shutil.rmtree("%s%s" % (path, tryDecode(dir)))
|
2016-02-24 11:40:13 +11:00
|
|
|
except:
|
2016-05-07 21:15:02 +10:00
|
|
|
logMsg("PLEX", "Failed to delete directory: %s"
|
|
|
|
% tryDecode(dir))
|
2016-01-23 10:12:23 +11:00
|
|
|
for file in files:
|
2016-05-07 21:15:02 +10:00
|
|
|
if tryDecode(file).startswith('plex'):
|
2016-02-24 11:40:13 +11:00
|
|
|
try:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(tryEncode("%s%s" % (path, tryDecode(file))))
|
2016-02-24 11:40:13 +11:00
|
|
|
except:
|
2016-05-07 21:15:02 +10:00
|
|
|
logMsg("PLEX", "Failed to file: %s" % tryDecode(file))
|
|
|
|
|
|
|
|
|
|
|
|
def tryEncode(uniString, encoding='utf-8'):
|
|
|
|
"""
|
|
|
|
Will try to encode uniString (in unicode) to encoding. This possibly
|
|
|
|
fails with e.g. Android TV's Python, which does not accept arguments for
|
|
|
|
string.encode()
|
|
|
|
"""
|
|
|
|
try:
|
2016-05-08 06:17:12 +10:00
|
|
|
uniString = uniString.encode(encoding, "ignore")
|
2016-05-07 21:15:02 +10:00
|
|
|
except TypeError:
|
2016-05-08 06:17:12 +10:00
|
|
|
uniString = uniString.encode()
|
2016-05-07 21:15:02 +10:00
|
|
|
return uniString
|
|
|
|
|
|
|
|
|
|
|
|
def tryDecode(string, encoding='utf-8'):
|
|
|
|
"""
|
|
|
|
Will try to decode string (encoded) using encoding. This possibly
|
|
|
|
fails with e.g. Android TV's Python, which does not accept arguments for
|
|
|
|
string.encode()
|
|
|
|
"""
|
|
|
|
try:
|
2016-05-08 06:17:12 +10:00
|
|
|
string = string.decode(encoding, "ignore")
|
2016-05-07 21:15:02 +10:00
|
|
|
except TypeError:
|
2016-05-08 06:17:12 +10:00
|
|
|
string = string.decode()
|
2016-05-16 00:33:53 +10:00
|
|
|
except UnicodeEncodeError:
|
|
|
|
# Already in unicode - e.g. sometimes file paths
|
|
|
|
pass
|
2016-05-07 21:15:02 +10:00
|
|
|
return string
|