2015-03-13 21:24:59 +00:00
|
|
|
#################################################################################################
|
|
|
|
# utils
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
import json
|
|
|
|
import os
|
2015-03-19 23:48:59 +00:00
|
|
|
import cProfile
|
|
|
|
import pstats
|
|
|
|
import time
|
2015-03-13 21:24:59 +00:00
|
|
|
import inspect
|
2015-03-27 11:20:40 +00:00
|
|
|
import sqlite3
|
2015-03-29 22:43:53 +00:00
|
|
|
import string
|
|
|
|
import unicodedata
|
2015-03-13 21:24:59 +00:00
|
|
|
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
from xml.dom import minidom
|
|
|
|
import xml.etree.cElementTree as ET
|
|
|
|
|
|
|
|
from API import API
|
|
|
|
from PlayUtils import PlayUtils
|
|
|
|
from DownloadUtils import DownloadUtils
|
|
|
|
downloadUtils = DownloadUtils()
|
2015-03-25 17:37:21 +00:00
|
|
|
addonSettings = xbmcaddon.Addon(id='plugin.video.emby')
|
2015-03-23 11:30:03 +00:00
|
|
|
language = addonSettings.getLocalizedString
|
2015-03-26 21:35:11 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
def logMsg(title, msg, level = 1):
|
2015-03-18 20:34:52 +00:00
|
|
|
logLevel = int(addonSettings.getSetting("logLevel"))
|
2015-04-28 22:23:26 +00:00
|
|
|
WINDOW = xbmcgui.Window(10000)
|
|
|
|
WINDOW.setProperty('logLevel', str(logLevel))
|
2015-03-13 21:24:59 +00:00
|
|
|
if(logLevel >= level):
|
2015-03-19 23:48:59 +00:00
|
|
|
if(logLevel == 2): # inspect.stack() is expensive
|
2015-03-13 21:24:59 +00: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-16 21:31:32 +00:00
|
|
|
|
|
|
|
def convertEncoding(data):
|
|
|
|
#nasty hack to make sure we have a unicode string
|
|
|
|
try:
|
|
|
|
return data.decode('utf-8')
|
|
|
|
except:
|
|
|
|
return data
|
|
|
|
|
2015-03-26 21:35:11 +00:00
|
|
|
def KodiSQL():
|
2015-04-03 09:58:21 +00:00
|
|
|
connection = sqlite3.connect(getKodiDBPath())
|
|
|
|
|
|
|
|
return connection
|
|
|
|
|
|
|
|
def getKodiDBPath():
|
2015-03-27 00:24:49 +00:00
|
|
|
if xbmc.getInfoLabel("System.BuildVersion").startswith("13"):
|
2015-03-26 21:35:11 +00:00
|
|
|
#gotham
|
|
|
|
dbVersion = "78"
|
2015-03-27 00:24:49 +00:00
|
|
|
if xbmc.getInfoLabel("System.BuildVersion").startswith("15"):
|
2015-03-26 21:35:11 +00:00
|
|
|
#isengard
|
|
|
|
dbVersion = "91"
|
|
|
|
else:
|
|
|
|
#helix
|
|
|
|
dbVersion = "90"
|
|
|
|
|
2015-04-08 01:09:17 +00:00
|
|
|
dbPath = xbmc.translatePath("special://profile/Database/MyVideos" + dbVersion + ".db")
|
2015-04-03 09:58:21 +00:00
|
|
|
|
|
|
|
return dbPath
|
2015-03-27 00:24:49 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
def checkAuthentication():
|
|
|
|
#check authentication
|
|
|
|
if addonSettings.getSetting('username') != "" and addonSettings.getSetting('ipaddress') != "":
|
|
|
|
try:
|
|
|
|
downloadUtils.authenticate()
|
|
|
|
except Exception, e:
|
2015-04-02 19:47:06 +00:00
|
|
|
logMsg("Emby authentication failed",e)
|
2015-03-13 21:24:59 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def prettifyXml(elem):
|
|
|
|
rough_string = etree.tostring(elem, "utf-8")
|
|
|
|
reparsed = minidom.parseString(rough_string)
|
|
|
|
return reparsed.toprettyxml(indent="\t")
|
|
|
|
|
|
|
|
def get_params( paramstring ):
|
|
|
|
xbmc.log("Parameter string: " + paramstring)
|
|
|
|
param={}
|
|
|
|
if len(paramstring)>=2:
|
|
|
|
params=paramstring
|
|
|
|
|
|
|
|
if params[0] == "?":
|
|
|
|
cleanedparams=params[1:]
|
|
|
|
else:
|
|
|
|
cleanedparams=params
|
|
|
|
|
|
|
|
if (params[len(params)-1]=='/'):
|
|
|
|
params=params[0:len(params)-2]
|
|
|
|
|
|
|
|
pairsofparams=cleanedparams.split('&')
|
|
|
|
for i in range(len(pairsofparams)):
|
|
|
|
splitparams={}
|
|
|
|
splitparams=pairsofparams[i].split('=')
|
|
|
|
if (len(splitparams))==2:
|
|
|
|
param[splitparams[0]]=splitparams[1]
|
|
|
|
elif (len(splitparams))==3:
|
|
|
|
param[splitparams[0]]=splitparams[1]+"="+splitparams[2]
|
|
|
|
return param
|
|
|
|
|
2015-03-19 23:48:59 +00:00
|
|
|
def startProfiling():
|
|
|
|
pr = cProfile.Profile()
|
|
|
|
pr.enable()
|
|
|
|
return pr
|
|
|
|
|
|
|
|
def stopProfiling(pr, profileName):
|
|
|
|
pr.disable()
|
|
|
|
ps = pstats.Stats(pr)
|
|
|
|
|
2015-03-25 17:37:21 +00:00
|
|
|
addondir = xbmc.translatePath(xbmcaddon.Addon(id='plugin.video.emby').getAddonInfo('profile'))
|
2015-03-19 23:48:59 +00: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-03-29 22:43:53 +00:00
|
|
|
def CleanName(filename):
|
|
|
|
validFilenameChars = "-_.() %s%s" % (string.ascii_letters, string.digits)
|
|
|
|
cleanedFilename = unicodedata.normalize('NFKD', filename).encode('ASCII', 'ignore')
|
|
|
|
return ''.join(c for c in cleanedFilename if c in validFilenameChars)
|
2015-03-17 18:41:26 +00:00
|
|
|
|
2015-03-31 21:21:51 +00:00
|
|
|
|
|
|
|
def removeDirectory(path):
|
|
|
|
if xbmcvfs.exists(path):
|
|
|
|
allDirs, allFiles = xbmcvfs.listdir(path)
|
|
|
|
for dir in allDirs:
|
|
|
|
xbmcvfs.rmdir(os.path.join(path,dir))
|
|
|
|
for file in allFiles:
|
|
|
|
xbmcvfs.delete(os.path.join(path,file))
|
|
|
|
|
|
|
|
xbmcvfs.rmdir(path)
|
2015-04-02 19:47:06 +00:00
|
|
|
|
|
|
|
def reset():
|
2015-04-03 08:39:16 +00:00
|
|
|
|
2015-04-15 02:20:08 +00:00
|
|
|
return_value = xbmcgui.Dialog().yesno("Warning", "Are you sure you want to reset your local Kodi database?")
|
2015-04-12 13:49:36 +00:00
|
|
|
|
2015-04-03 08:39:16 +00:00
|
|
|
if return_value == 0:
|
|
|
|
return
|
2015-04-12 13:49:36 +00:00
|
|
|
|
|
|
|
# Ask if user information should be deleted too.
|
2015-04-15 02:20:08 +00:00
|
|
|
return_user = xbmcgui.Dialog().yesno("Warning", "Reset all Emby Addon settings?")
|
2015-04-12 13:49:36 +00:00
|
|
|
|
2015-04-15 02:20:08 +00:00
|
|
|
delete_settings = False
|
2015-04-12 13:49:36 +00:00
|
|
|
if return_user == 1:
|
2015-04-15 02:20:08 +00:00
|
|
|
delete_settings = True
|
2015-04-03 08:39:16 +00:00
|
|
|
|
|
|
|
# first stop any db sync
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
WINDOW.setProperty("SyncDatabaseShouldStop", "true")
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
while(WINDOW.getProperty("SyncDatabaseRunning") == "true"):
|
2015-04-15 02:20:08 +00:00
|
|
|
xbmc.log("Sync Running, will wait : " + str(count))
|
2015-04-03 08:39:16 +00:00
|
|
|
count += 1
|
|
|
|
if(count > 10):
|
2015-04-03 10:49:39 +00:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-04-03 08:39:16 +00:00
|
|
|
dialog.ok('Warning', 'Could not stop DB sync, you should try again.')
|
|
|
|
return
|
|
|
|
xbmc.sleep(1000)
|
2015-04-12 08:34:00 +00:00
|
|
|
|
|
|
|
# delete db table data
|
|
|
|
print "Doing DB Reset"
|
|
|
|
connection = KodiSQL()
|
|
|
|
cursor = connection.cursor( )
|
|
|
|
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
tableName = row[0]
|
|
|
|
print tableName
|
|
|
|
if(tableName != "version"):
|
|
|
|
cursor.execute("DELETE FROM " + tableName)
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
# reset the install run flag
|
2015-04-12 09:49:02 +00:00
|
|
|
WINDOW.setProperty("SyncInstallRunDone", "false")
|
2015-04-12 08:34:00 +00:00
|
|
|
|
2015-04-15 02:20:08 +00:00
|
|
|
if (delete_settings == True):
|
2015-04-12 13:49:36 +00:00
|
|
|
addondir = xbmc.translatePath(addonSettings.getAddonInfo('profile'))
|
|
|
|
dataPath = os.path.join(addondir + "settings.xml")
|
|
|
|
xbmcvfs.delete(dataPath)
|
2015-04-15 02:20:08 +00:00
|
|
|
xbmc.log("Deleting : settings.xml")
|
2015-04-12 13:49:36 +00:00
|
|
|
|
2015-04-02 19:47:06 +00:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-04-12 13:49:36 +00:00
|
|
|
dialog.ok('Emby Reset', 'Database reset has completed, Kodi will now restart to apply the changes.')
|
2015-04-03 08:39:16 +00:00
|
|
|
xbmc.executebuiltin("RestartApp")
|
2015-04-02 19:47:06 +00:00
|
|
|
|