2015-03-14 08:24:59 +11:00
|
|
|
#################################################################################################
|
|
|
|
# utils
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
import json
|
|
|
|
import os
|
2015-03-20 10:48:59 +11:00
|
|
|
import cProfile
|
|
|
|
import pstats
|
|
|
|
import time
|
2015-03-14 08:24:59 +11:00
|
|
|
import inspect
|
2015-03-27 22:20:40 +11:00
|
|
|
import sqlite3
|
2015-03-30 09:43:53 +11:00
|
|
|
import string
|
|
|
|
import unicodedata
|
2015-05-07 07:24:13 +10:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
from API import API
|
|
|
|
from PlayUtils import PlayUtils
|
|
|
|
from DownloadUtils import DownloadUtils
|
2015-08-14 19:03:12 +10:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
downloadUtils = DownloadUtils()
|
2015-08-12 19:59:05 +10:00
|
|
|
addon = xbmcaddon.Addon()
|
|
|
|
language = addon.getLocalizedString
|
2015-03-27 08:35:11 +11:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
def logMsg(title, msg, level = 1):
|
2015-06-05 20:12:09 +10:00
|
|
|
|
2015-04-29 08:23:26 +10:00
|
|
|
WINDOW = xbmcgui.Window(10000)
|
2015-06-05 20:12:09 +10:00
|
|
|
# Get the logLevel set in UserClient
|
|
|
|
logLevel = int(WINDOW.getProperty('getLogLevel'))
|
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
if(logLevel >= level):
|
2015-03-20 10:48:59 +11:00
|
|
|
if(logLevel == 2): # inspect.stack() is expensive
|
2015-03-14 08:24:59 +11:00
|
|
|
try:
|
|
|
|
xbmc.log(title + " -> " + inspect.stack()[1][3] + " : " + str(msg))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
xbmc.log(title + " -> " + inspect.stack()[1][3] + " : " + str(msg.encode('utf-8')))
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
xbmc.log(title + " -> " + str(msg))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
xbmc.log(title + " -> " + str(msg.encode('utf-8')))
|
2015-03-17 08:31:32 +11:00
|
|
|
|
|
|
|
def convertEncoding(data):
|
|
|
|
#nasty hack to make sure we have a unicode string
|
|
|
|
try:
|
|
|
|
return data.decode('utf-8')
|
|
|
|
except:
|
|
|
|
return data
|
|
|
|
|
2015-05-08 08:04:40 +10:00
|
|
|
def KodiSQL(type="video"):
|
|
|
|
|
|
|
|
if type == "music":
|
|
|
|
dbPath = getKodiMusicDBPath()
|
2015-06-19 20:53:22 +10:00
|
|
|
elif type == "texture":
|
|
|
|
dbPath = xbmc.translatePath("special://database/Textures13.db")
|
2015-05-08 08:04:40 +10:00
|
|
|
else:
|
|
|
|
dbPath = getKodiVideoDBPath()
|
|
|
|
|
|
|
|
connection = sqlite3.connect(dbPath)
|
2015-05-01 21:30:21 +10:00
|
|
|
|
2015-04-03 20:58:21 +11:00
|
|
|
return connection
|
|
|
|
|
2015-05-08 08:04:40 +10:00
|
|
|
def getKodiVideoDBPath():
|
2015-06-16 10:41:40 +10:00
|
|
|
|
2015-06-18 20:42:57 +10:00
|
|
|
kodibuild = xbmc.getInfoLabel("System.BuildVersion")
|
2015-06-18 12:34:45 +10:00
|
|
|
|
2015-06-18 20:42:57 +10:00
|
|
|
if kodibuild.startswith("13"):
|
|
|
|
# Gotham
|
|
|
|
dbVersion = "78"
|
|
|
|
elif kodibuild.startswith("14"):
|
|
|
|
# Helix
|
|
|
|
dbVersion = "90"
|
|
|
|
elif kodibuild.startswith("15"):
|
|
|
|
# Isengard
|
|
|
|
dbVersion = "93"
|
2015-07-24 21:29:41 +10:00
|
|
|
elif kodibuild.startswith("16"):
|
|
|
|
# Jarvis
|
|
|
|
dbVersion = "94"
|
2015-06-18 20:42:57 +10:00
|
|
|
else:
|
|
|
|
# Not a compatible build
|
|
|
|
xbmc.log("This Kodi version is incompatible. Current version: %s" % kodibuild)
|
2015-06-16 10:41:40 +10:00
|
|
|
|
2015-06-18 20:42:57 +10:00
|
|
|
dbPath = xbmc.translatePath("special://profile/Database/MyVideos" + dbVersion + ".db")
|
2015-04-03 20:58:21 +11:00
|
|
|
|
2015-06-18 20:42:57 +10:00
|
|
|
return dbPath
|
2015-03-27 11:24:49 +11:00
|
|
|
|
2015-05-08 08:04:40 +10:00
|
|
|
def getKodiMusicDBPath():
|
2015-06-18 20:42:57 +10:00
|
|
|
if xbmc.getInfoLabel("System.BuildVersion").startswith("13"):
|
|
|
|
#gotham
|
|
|
|
dbVersion = "46"
|
2015-07-22 23:24:24 +10:00
|
|
|
elif xbmc.getInfoLabel("System.BuildVersion").startswith("14"):
|
|
|
|
#helix
|
|
|
|
dbVersion = "48"
|
2015-06-18 20:42:57 +10:00
|
|
|
elif xbmc.getInfoLabel("System.BuildVersion").startswith("15"):
|
|
|
|
#isengard
|
|
|
|
dbVersion = "52"
|
2015-07-24 21:29:41 +10:00
|
|
|
elif xbmc.getInfoLabel("System.BuildVersion").startswith("16"):
|
|
|
|
#jarvis
|
|
|
|
dbVersion = "53"
|
2015-06-18 20:42:57 +10:00
|
|
|
else:
|
2015-07-22 23:24:24 +10:00
|
|
|
# Not a compatible build
|
|
|
|
xbmc.log("This Kodi version is incompatible. Current version: %s" % kodibuild)
|
|
|
|
|
2015-06-18 20:42:57 +10:00
|
|
|
|
|
|
|
dbPath = xbmc.translatePath("special://profile/Database/MyMusic" + dbVersion + ".db")
|
|
|
|
|
|
|
|
return dbPath
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
def prettifyXml(elem):
|
|
|
|
rough_string = etree.tostring(elem, "utf-8")
|
|
|
|
reparsed = minidom.parseString(rough_string)
|
2015-07-22 23:16:08 +10:00
|
|
|
return reparsed.toprettyxml(indent="\t")
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-03-20 10:48:59 +11:00
|
|
|
def startProfiling():
|
|
|
|
pr = cProfile.Profile()
|
|
|
|
pr.enable()
|
|
|
|
return pr
|
|
|
|
|
|
|
|
def stopProfiling(pr, profileName):
|
|
|
|
pr.disable()
|
|
|
|
ps = pstats.Stats(pr)
|
|
|
|
|
2015-03-26 04:37:21 +11:00
|
|
|
addondir = xbmc.translatePath(xbmcaddon.Addon(id='plugin.video.emby').getAddonInfo('profile'))
|
2015-03-20 10:48:59 +11:00
|
|
|
|
|
|
|
fileTimeStamp = time.strftime("%Y-%m-%d %H-%M-%S")
|
|
|
|
tabFileNamepath = os.path.join(addondir, "profiles")
|
|
|
|
tabFileName = os.path.join(addondir, "profiles" , profileName + "_profile_(" + fileTimeStamp + ").tab")
|
|
|
|
|
|
|
|
if not xbmcvfs.exists(tabFileNamepath):
|
|
|
|
xbmcvfs.mkdir(tabFileNamepath)
|
|
|
|
|
|
|
|
f = open(tabFileName, 'wb')
|
|
|
|
f.write("NumbCalls\tTotalTime\tCumulativeTime\tFunctionName\tFileName\r\n")
|
|
|
|
for (key, value) in ps.stats.items():
|
|
|
|
(filename, count, func_name) = key
|
|
|
|
(ccalls, ncalls, total_time, cumulative_time, callers) = value
|
|
|
|
try:
|
|
|
|
f.write(str(ncalls) + "\t" + "{:10.4f}".format(total_time) + "\t" + "{:10.4f}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n")
|
|
|
|
except ValueError:
|
|
|
|
f.write(str(ncalls) + "\t" + "{0}".format(total_time) + "\t" + "{0}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n")
|
|
|
|
f.close()
|
|
|
|
|
2015-07-20 11:35:14 +10:00
|
|
|
def createSources():
|
|
|
|
# To make Master lock compatible
|
|
|
|
path = xbmc.translatePath("special://profile/").decode("utf-8")
|
|
|
|
xmlpath = "%ssources.xml" % path
|
|
|
|
|
2015-07-26 22:09:09 +10:00
|
|
|
if xbmcvfs.exists(xmlpath):
|
|
|
|
# add some way to writing dummy path to existing sources.xml
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
sources = open(xmlpath, 'w')
|
|
|
|
sources.write(
|
|
|
|
|
|
|
|
'<sources>\n\t'
|
|
|
|
'<programs>\n\t\t'
|
|
|
|
'<default pathversion="1"></default>\n\t'
|
|
|
|
'</programs>\n\t'
|
|
|
|
'<video>\n\t\t'
|
|
|
|
'<default pathversion="1"></default>\n\t\t'
|
|
|
|
'<source>\n\t\t\t'
|
2015-07-27 19:24:00 +10:00
|
|
|
'<name>Emby</name>\n\t\t\t'
|
2015-07-26 22:09:09 +10:00
|
|
|
'<path pathversion="1">smb://embydummy/dummypath1/</path>\n\t\t\t'
|
|
|
|
'<allowsharing>true</allowsharing>\n\t\t'
|
|
|
|
'</source>\n\t\t'
|
|
|
|
'<source>\n\t\t\t'
|
2015-07-27 19:24:00 +10:00
|
|
|
'<name>Emby</name>\n\t\t\t'
|
2015-07-26 22:09:09 +10:00
|
|
|
'<path pathversion="1">smb://embydummy/dummypath2/</path>\n\t\t\t'
|
|
|
|
'<allowsharing>true</allowsharing>\n\t\t'
|
|
|
|
'</source>\n\t'
|
|
|
|
'</video>\n\t'
|
|
|
|
'<music>\n\t\t'
|
|
|
|
'<default pathversion="1"></default>\n\t'
|
|
|
|
'</music>\n\t'
|
|
|
|
'<pictures>\n\t\t'
|
|
|
|
'<default pathversion="1"></default>\n\t'
|
|
|
|
'</pictures>\n\t'
|
|
|
|
'<files>\n\t\t'
|
|
|
|
'<default pathversion="1"></default>\n\t'
|
|
|
|
'</files>\n'
|
|
|
|
'</sources>'
|
|
|
|
)
|
2015-07-20 11:35:14 +10:00
|
|
|
|
2015-08-14 19:03:12 +10:00
|
|
|
def settings(setting, value = None):
|
2015-08-12 19:59:05 +10:00
|
|
|
# Get or add addon setting
|
|
|
|
addon = xbmcaddon.Addon()
|
|
|
|
if value:
|
|
|
|
addon.setSetting(setting, value)
|
|
|
|
else:
|
|
|
|
return addon.getSetting(setting)
|
2015-06-19 18:10:41 +10:00
|
|
|
|
|
|
|
def normalize_string(text):
|
2015-08-03 10:28:13 +10:00
|
|
|
# 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('.')
|
|
|
|
text = unicodedata.normalize('NFKD', unicode(text, 'utf-8')).encode('ascii', 'ignore')
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
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('.')
|
|
|
|
text = unicodedata.normalize('NFKD', unicode(text, 'utf-8')).encode('ascii', 'ignore')
|
|
|
|
|
2015-06-19 18:10:41 +10:00
|
|
|
return text
|
2015-08-14 19:03:12 +10:00
|
|
|
|
|
|
|
def reloadProfile():
|
|
|
|
# Useful to reload the add-on without restarting Kodi.
|
|
|
|
profile = xbmc.getInfoLabel('System.ProfileName')
|
|
|
|
xbmc.executebuiltin("LoadProfile(%s)" % profile)
|
2015-03-18 05:41:26 +11:00
|
|
|
|
2015-08-14 19:03:12 +10:00
|
|
|
|
2015-04-03 06:47:06 +11:00
|
|
|
def reset():
|
2015-04-03 19:39:16 +11:00
|
|
|
|
2015-07-22 23:16:08 +10:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-04-15 12:20:08 +10:00
|
|
|
return_value = xbmcgui.Dialog().yesno("Warning", "Are you sure you want to reset your local Kodi database?")
|
2015-04-12 23:49:36 +10:00
|
|
|
|
2015-04-03 19:39:16 +11:00
|
|
|
if return_value == 0:
|
|
|
|
return
|
2015-04-12 23:49:36 +10:00
|
|
|
|
2015-05-05 09:43:46 +10:00
|
|
|
#cleanup video nodes
|
|
|
|
import shutil
|
2015-05-06 12:45:29 +10:00
|
|
|
path = "special://profile/library/video/"
|
2015-05-05 09:43:46 +10:00
|
|
|
if xbmcvfs.exists(path):
|
|
|
|
allDirs, allFiles = xbmcvfs.listdir(path)
|
|
|
|
for dir in allDirs:
|
|
|
|
if dir.startswith("Emby "):
|
2015-05-06 12:45:29 +10:00
|
|
|
shutil.rmtree(xbmc.translatePath("special://profile/library/video/" + dir))
|
2015-05-06 00:16:34 +10:00
|
|
|
for file in allFiles:
|
|
|
|
if file.startswith("emby"):
|
|
|
|
xbmcvfs.delete(path + file)
|
2015-08-14 19:03:12 +10:00
|
|
|
|
|
|
|
settings('SyncInstallRunDone', "false")
|
2015-05-05 09:43:46 +10:00
|
|
|
|
2015-04-12 23:49:36 +10:00
|
|
|
# Ask if user information should be deleted too.
|
2015-04-15 12:20:08 +10:00
|
|
|
return_user = xbmcgui.Dialog().yesno("Warning", "Reset all Emby Addon settings?")
|
2015-04-12 23:49:36 +10:00
|
|
|
if return_user == 1:
|
2015-07-22 23:16:08 +10:00
|
|
|
WINDOW.setProperty('deletesettings', "true")
|
2015-08-14 19:03:12 +10:00
|
|
|
addon = xbmcaddon.Addon()
|
|
|
|
addondir = xbmc.translatePath(addon.getAddonInfo('profile')).decode('utf-8')
|
|
|
|
dataPath = "%ssettings.xml" % addondir
|
|
|
|
xbmcvfs.delete(dataPath)
|
|
|
|
logMsg("EMBY", "Deleting: settings.xml", 1)
|
2015-04-03 19:39:16 +11:00
|
|
|
|
|
|
|
# first stop any db sync
|
|
|
|
WINDOW.setProperty("SyncDatabaseShouldStop", "true")
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
while(WINDOW.getProperty("SyncDatabaseRunning") == "true"):
|
2015-04-15 12:20:08 +10:00
|
|
|
xbmc.log("Sync Running, will wait : " + str(count))
|
2015-04-03 19:39:16 +11:00
|
|
|
count += 1
|
|
|
|
if(count > 10):
|
2015-04-03 21:49:39 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-04-03 19:39:16 +11:00
|
|
|
dialog.ok('Warning', 'Could not stop DB sync, you should try again.')
|
|
|
|
return
|
|
|
|
xbmc.sleep(1000)
|
2015-04-12 18:34:00 +10:00
|
|
|
|
2015-05-08 08:46:41 +10:00
|
|
|
# delete video db table data
|
|
|
|
print "Doing Video DB Reset"
|
|
|
|
connection = KodiSQL("video")
|
2015-04-12 18:34:00 +10:00
|
|
|
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()
|
|
|
|
|
2015-08-14 19:03:12 +10:00
|
|
|
if settings('enableMusicSync') == "true":
|
2015-05-08 08:46:41 +10:00
|
|
|
# delete video db table data
|
|
|
|
print "Doing Music DB Reset"
|
|
|
|
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()
|
2015-08-20 21:42:40 +10:00
|
|
|
|
|
|
|
# Remove the strm and nfo
|
|
|
|
path = xbmc.translatePath("special://profile/addon_data/plugin.video.emby/library/").decode('utf-8')
|
|
|
|
if xbmcvfs.exists(path):
|
|
|
|
xbmc.log("Removing %s." % path)
|
|
|
|
shutil.rmtree(path)
|
2015-05-08 08:46:41 +10:00
|
|
|
|
|
|
|
|
2015-04-12 18:34:00 +10:00
|
|
|
# reset the install run flag
|
2015-08-14 19:03:12 +10:00
|
|
|
#settings('SyncInstallRunDone', "false")
|
|
|
|
#WINDOW.setProperty("SyncInstallRunDone", "false")
|
2015-04-12 18:34:00 +10:00
|
|
|
|
2015-04-03 06:47:06 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-08-14 19:03:12 +10:00
|
|
|
# Reload would work instead of restart since the add-on is a service.
|
|
|
|
#dialog.ok('Emby Reset', 'Database reset has completed, Kodi will now restart to apply the changes.')
|
|
|
|
#WINDOW.clearProperty("SyncDatabaseShouldStop")
|
|
|
|
#reloadProfile()
|
2015-04-12 23:49:36 +10:00
|
|
|
dialog.ok('Emby Reset', 'Database reset has completed, Kodi will now restart to apply the changes.')
|
2015-07-22 23:16:08 +10:00
|
|
|
xbmc.executebuiltin("RestartApp")
|