2015-03-16 12:24:19 +11:00
|
|
|
#######################################################################
|
2015-03-15 13:43:06 +11:00
|
|
|
# CLIENTINFORMATION: centralized client data
|
|
|
|
# -------------------------------
|
2015-03-16 12:24:19 +11:00
|
|
|
# addonId, addonName, addon version, clientId, platform
|
|
|
|
#######################################################################
|
2015-03-15 13:43:06 +11:00
|
|
|
# USER: centralized Userdata
|
|
|
|
# -------------------------------
|
2015-03-16 12:24:19 +11:00
|
|
|
# username, userId, token, server, http prefix, LogLevel
|
|
|
|
#######################################################################
|
2015-03-15 13:43:06 +11:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcgui
|
2015-03-14 13:04:18 +11:00
|
|
|
import os
|
2015-03-15 13:43:06 +11:00
|
|
|
from uuid import uuid4 as uuid4
|
2015-03-14 13:04:18 +11:00
|
|
|
from Lock import Lock
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
class ClientInformation():
|
|
|
|
|
2015-03-15 13:43:06 +11:00
|
|
|
def __init__(self):
|
|
|
|
addonId = self.getAddonId()
|
|
|
|
self.addon = xbmcaddon.Addon(id=addonId)
|
2015-03-16 12:24:19 +11:00
|
|
|
self.WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
|
|
|
|
level = User().getLogLevel()
|
|
|
|
self.logLevel = 0
|
|
|
|
|
|
|
|
if (level != None and level != ""):
|
|
|
|
self.logLevel = int(level)
|
|
|
|
|
|
|
|
if (self.logLevel == 2):
|
|
|
|
self.LogCalls = True
|
|
|
|
|
|
|
|
def logMsg(self, msg, level = 1):
|
|
|
|
|
|
|
|
addonName = self.getAddonName()
|
|
|
|
className = self.__class__.__name__
|
|
|
|
|
|
|
|
if (self.logLevel >= level):
|
|
|
|
try:
|
|
|
|
xbmc.log("%s %s -> %s" % (addonName, className, str(msg)))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
try:
|
2015-03-17 08:31:32 +11:00
|
|
|
xbmc.log("%s %s -> %s" % (addonName, className, str(msg)))
|
2015-03-16 12:24:19 +11:00
|
|
|
except: pass
|
2015-03-15 13:43:06 +11:00
|
|
|
|
|
|
|
def getAddonId(self):
|
|
|
|
# To use when declaring xbmcaddon.Addon(id=addonId)
|
|
|
|
addonId = "plugin.video.mb3sync"
|
|
|
|
return addonId
|
2015-03-16 12:24:19 +11:00
|
|
|
|
|
|
|
def getAddonName(self):
|
|
|
|
# Useful for logging
|
|
|
|
addonName = self.addon.getAddonInfo('name').upper()
|
|
|
|
return addonName
|
2015-03-15 13:43:06 +11:00
|
|
|
|
|
|
|
def getVersion(self):
|
|
|
|
|
|
|
|
version = self.addon.getAddonInfo('version')
|
|
|
|
return version
|
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
def getMachineId(self):
|
|
|
|
|
2015-03-16 12:24:19 +11:00
|
|
|
WINDOW = self.WINDOW
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
clientId = WINDOW.getProperty("client_id")
|
2015-03-14 13:04:18 +11:00
|
|
|
if(clientId != None and clientId != ""):
|
|
|
|
return clientId
|
|
|
|
|
|
|
|
# we need to load and or generate a client machine id
|
2015-03-15 13:43:06 +11:00
|
|
|
__addon__ = self.addon
|
2015-03-14 13:04:18 +11:00
|
|
|
__addondir__ = xbmc.translatePath( __addon__.getAddonInfo('path'))
|
|
|
|
machine_guid_lock_path = os.path.join(__addondir__, "machine_guid.lock")
|
|
|
|
machine_guid_path = os.path.join(__addondir__, "machine_guid")
|
|
|
|
clientId = ""
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-03-14 13:04:18 +11:00
|
|
|
try:
|
|
|
|
lock = Lock(machine_guid_lock_path)
|
|
|
|
locked = lock.acquire()
|
|
|
|
|
|
|
|
if(locked == True):
|
|
|
|
|
|
|
|
fd = os.open(machine_guid_path, os.O_CREAT|os.O_RDWR)
|
|
|
|
clientId = os.read(fd, 256)
|
|
|
|
|
|
|
|
if(len(clientId) == 0):
|
|
|
|
uuid = uuid4()
|
|
|
|
clientId = str("%012X" % uuid)
|
2015-03-20 08:50:33 +11:00
|
|
|
self.logMsg("ClientId saved to FILE : %s" % clientId,1)
|
2015-03-14 13:04:18 +11:00
|
|
|
os.write(fd, clientId)
|
|
|
|
os.fsync(fd)
|
|
|
|
|
|
|
|
os.close(fd)
|
|
|
|
|
2015-03-20 08:50:33 +11:00
|
|
|
self.logMsg("ClientId saved to WINDOW : %s" % clientId,1)
|
2015-03-14 08:24:59 +11:00
|
|
|
WINDOW.setProperty("client_id", clientId)
|
2015-03-14 13:04:18 +11:00
|
|
|
|
|
|
|
finally:
|
|
|
|
lock.release()
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
return clientId
|
|
|
|
|
|
|
|
def getPlatform(self):
|
|
|
|
|
|
|
|
if xbmc.getCondVisibility('system.platform.osx'):
|
|
|
|
return "OSX"
|
|
|
|
elif xbmc.getCondVisibility('system.platform.atv2'):
|
|
|
|
return "ATV2"
|
|
|
|
elif xbmc.getCondVisibility('system.platform.ios'):
|
|
|
|
return "iOS"
|
|
|
|
elif xbmc.getCondVisibility('system.platform.windows'):
|
|
|
|
return "Windows"
|
|
|
|
elif xbmc.getCondVisibility('system.platform.linux'):
|
|
|
|
return "Linux/RPi"
|
|
|
|
elif xbmc.getCondVisibility('system.platform.android'):
|
|
|
|
return "Linux/Android"
|
|
|
|
|
|
|
|
return "Unknown"
|
2015-03-15 13:43:06 +11:00
|
|
|
|
|
|
|
|
|
|
|
class User(ClientInformation):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
addonId = self.getAddonId()
|
|
|
|
self.addon = xbmcaddon.Addon(id=addonId)
|
2015-03-16 12:24:19 +11:00
|
|
|
self.WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
|
|
|
|
level = self.getLogLevel()
|
|
|
|
self.logLevel = 0
|
|
|
|
|
|
|
|
if (level != None and level != ""):
|
|
|
|
self.logLevel = int(level)
|
|
|
|
|
|
|
|
if (self.logLevel == 2):
|
|
|
|
self.LogCalls = True
|
|
|
|
|
|
|
|
def logMsg(self, msg, level = 1):
|
|
|
|
|
2015-03-16 12:31:21 +11:00
|
|
|
addonName = self.getAddonName()
|
2015-03-16 12:24:19 +11:00
|
|
|
className = self.__class__.__name__
|
|
|
|
|
|
|
|
if (self.logLevel >= level):
|
|
|
|
try:
|
|
|
|
xbmc.log("%s %s -> %s" % (addonName, className, str(msg)))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
try:
|
2015-03-17 08:31:32 +11:00
|
|
|
xbmc.log("%s %s -> %s" % (addonName, className, str(msg)))
|
2015-03-16 12:24:19 +11:00
|
|
|
except: pass
|
2015-03-15 13:43:06 +11:00
|
|
|
|
|
|
|
def getUsername(self):
|
|
|
|
|
|
|
|
username = self.addon.getSetting('username')
|
|
|
|
return username
|
|
|
|
|
|
|
|
def getUserId(self):
|
|
|
|
|
2015-03-16 12:24:19 +11:00
|
|
|
username = self.getUsername()
|
|
|
|
w_userId = self.WINDOW.getProperty('userId%s' % username)
|
|
|
|
s_userId = self.addon.getSetting('userId%s' % username)
|
|
|
|
|
|
|
|
# Verify if userId is saved to Window
|
|
|
|
if (w_userId != ""):
|
2015-03-20 08:50:33 +11:00
|
|
|
self.logMsg("Returning saved (WINDOW) UserId for user: %s UserId: %s" % (username, w_userId),2)
|
2015-03-16 12:24:19 +11:00
|
|
|
return w_userId
|
|
|
|
# Verify if userId is saved in settings
|
|
|
|
elif (s_userId != ""):
|
2015-03-20 08:50:33 +11:00
|
|
|
self.logMsg("Returning saved (SETTINGS) UserId for user: %s UserId: %s" % (username, s_userId),2)
|
2015-03-16 12:24:19 +11:00
|
|
|
self.WINDOW.setProperty('userId%s' % username, s_userId)
|
|
|
|
return s_userId
|
|
|
|
else:
|
|
|
|
return ""
|
2015-03-15 13:43:06 +11:00
|
|
|
|
|
|
|
def getToken(self):
|
|
|
|
|
2015-03-16 12:24:19 +11:00
|
|
|
username = self.getUsername()
|
|
|
|
w_token = self.WINDOW.getProperty('AccessToken%s' % username)
|
|
|
|
s_token = self.addon.getSetting('AccessToken%s' % username)
|
|
|
|
|
|
|
|
# Verify if token is saved to Window
|
|
|
|
if (w_token != ""):
|
2015-03-20 08:50:33 +11:00
|
|
|
self.logMsg("Returning saved (WINDOW) AccessToken for user: %s Token: %s" % (username, w_token),2)
|
2015-03-16 12:24:19 +11:00
|
|
|
return w_token
|
|
|
|
# Verify if token is saved in settings
|
|
|
|
elif (s_token != ""):
|
2015-03-20 08:50:33 +11:00
|
|
|
self.logMsg("Returning saved (SETTINGS) AccessToken for user: %s Token: %s" % (username, s_token),2)
|
2015-03-16 12:24:19 +11:00
|
|
|
self.WINDOW.setProperty('AccessToken%s' % username, s_token)
|
|
|
|
return s_token
|
|
|
|
else:
|
|
|
|
self.logMsg("User is not authenticated.")
|
|
|
|
return ""
|
2015-03-15 13:43:06 +11:00
|
|
|
|
|
|
|
def getServer(self):
|
|
|
|
|
|
|
|
host = self.addon.getSetting('ipaddress')
|
|
|
|
port = self.addon.getSetting('port')
|
|
|
|
return host + ":" + port
|
2015-03-16 12:24:19 +11:00
|
|
|
|
|
|
|
def getHTTPprefix(self):
|
|
|
|
# For https support
|
|
|
|
prefix = self.addon.getSetting('prefix')
|
|
|
|
if prefix:
|
|
|
|
return "https://"
|
|
|
|
else:
|
|
|
|
return "http://"
|
2015-03-15 13:43:06 +11:00
|
|
|
|
2015-03-16 12:24:19 +11:00
|
|
|
def getLogLevel(self):
|
2015-03-15 13:43:06 +11:00
|
|
|
|
2015-03-16 12:24:19 +11:00
|
|
|
level = self.addon.getSetting('logLevel')
|
2015-03-15 13:43:06 +11:00
|
|
|
return level
|