2016-03-31 20:25:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
import logging
|
2016-03-31 20:25:45 +00:00
|
|
|
import sqlite3
|
|
|
|
import threading
|
|
|
|
from datetime import datetime, timedelta, time
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcvfs
|
|
|
|
|
|
|
|
import api
|
|
|
|
import utils
|
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
|
|
|
import itemtypes
|
|
|
|
import embydb_functions as embydb
|
|
|
|
import kodidb_functions as kodidb
|
|
|
|
import read_embyserver as embyserver
|
|
|
|
import userclient
|
|
|
|
import videonodes
|
2016-10-10 08:19:00 +00:00
|
|
|
from objects import Movies, MusicVideos, TVShows, Music
|
|
|
|
from utils import window, settings, language as lang, should_stop
|
2016-10-09 09:14:45 +00:00
|
|
|
from ga_client import GoogleAnalytics
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
|
|
|
|
|
|
|
##################################################################################################
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
class LibrarySync(threading.Thread):
|
|
|
|
|
|
|
|
_shared_state = {}
|
|
|
|
|
2016-08-06 04:21:32 +00:00
|
|
|
isFastSync = False
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
stop_thread = False
|
|
|
|
suspend_thread = False
|
|
|
|
|
|
|
|
# Track websocketclient updates
|
|
|
|
addedItems = []
|
|
|
|
updateItems = []
|
|
|
|
userdataItems = []
|
|
|
|
removeItems = []
|
|
|
|
forceLibraryUpdate = False
|
|
|
|
refresh_views = False
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
self.monitor = xbmc.Monitor()
|
|
|
|
|
|
|
|
self.clientInfo = clientinfo.ClientInfo()
|
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
|
|
|
self.user = userclient.UserClient()
|
|
|
|
self.emby = embyserver.Read_EmbyServer()
|
|
|
|
self.vnodes = videonodes.VideoNodes()
|
|
|
|
|
2016-09-20 09:17:11 +00:00
|
|
|
self.kodi_version = int(xbmc.getInfoLabel('System.BuildVersion')[:2])
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
|
2016-07-03 03:26:11 +00:00
|
|
|
def progressDialog(self, title):
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
dialog = None
|
|
|
|
|
2016-07-03 03:26:11 +00:00
|
|
|
dialog = xbmcgui.DialogProgressBG()
|
|
|
|
dialog.create("Emby for Kodi", title)
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Show progress dialog: %s" % title)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
return dialog
|
|
|
|
|
|
|
|
def startSync(self):
|
|
|
|
|
2016-10-09 09:14:45 +00:00
|
|
|
ga = GoogleAnalytics()
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
# Run at start up - optional to use the server plugin
|
|
|
|
if settings('SyncInstallRunDone') == "true":
|
|
|
|
# Validate views
|
|
|
|
self.refreshViews()
|
|
|
|
completed = False
|
|
|
|
# Verify if server plugin is installed.
|
|
|
|
if settings('serverSync') == "true":
|
|
|
|
# Try to use fast start up
|
|
|
|
url = "{server}/emby/Plugins?format=json"
|
|
|
|
result = self.doUtils(url)
|
|
|
|
|
|
|
|
for plugin in result:
|
|
|
|
if plugin['Name'] == "Emby.Kodi Sync Queue":
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Found server plugin.")
|
2016-08-06 04:21:32 +00:00
|
|
|
self.isFastSync = True
|
2016-10-14 03:35:34 +00:00
|
|
|
ga.sendEventData("SyncAction", "FastSync")
|
2016-03-31 20:25:45 +00:00
|
|
|
completed = self.fastSync()
|
|
|
|
break
|
|
|
|
|
|
|
|
if not completed:
|
|
|
|
# Fast sync failed or server plugin is not found
|
2016-10-14 03:35:34 +00:00
|
|
|
ga.sendEventData("SyncAction", "Sync")
|
2016-03-31 20:25:45 +00:00
|
|
|
completed = ManualSync().sync()
|
|
|
|
else:
|
|
|
|
# Install sync is not completed
|
2016-10-09 09:14:45 +00:00
|
|
|
ga.sendEventData("SyncAction", "FullSync")
|
2016-03-31 20:25:45 +00:00
|
|
|
completed = self.fullSync()
|
|
|
|
|
|
|
|
return completed
|
|
|
|
|
|
|
|
def fastSync(self):
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
lastSync = settings('LastIncrementalSync')
|
2016-03-31 20:25:45 +00:00
|
|
|
if not lastSync:
|
|
|
|
lastSync = "2010-01-01T00:00:00Z"
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
lastSyncTime = utils.convertDate(lastSync)
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Last sync run: %s" % lastSyncTime)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# get server RetentionDateTime
|
|
|
|
result = self.doUtils("{server}/emby/Emby.Kodi.SyncQueue/GetServerDateTime?format=json")
|
|
|
|
try:
|
|
|
|
retention_time = result['RetentionDateTime']
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
retention_time = "2010-01-01T00:00:00Z"
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
retention_time = utils.convertDate(retention_time)
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("RetentionDateTime: %s" % retention_time)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# if last sync before retention time do a full sync
|
|
|
|
if retention_time > lastSyncTime:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Fast sync server retention insufficient, fall back to full sync")
|
2016-03-31 20:25:45 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
params = {'LastUpdateDT': lastSync}
|
2016-09-09 08:53:56 +00:00
|
|
|
if settings('enableMusic') != "true":
|
|
|
|
params['filter'] = "music"
|
2016-06-18 03:03:28 +00:00
|
|
|
url = "{server}/emby/Emby.Kodi.SyncQueue/{UserId}/GetItems?format=json"
|
|
|
|
result = self.doUtils(url, parameters=params)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
processlist = {
|
|
|
|
|
|
|
|
'added': result['ItemsAdded'],
|
|
|
|
'update': result['ItemsUpdated'],
|
|
|
|
'userdata': result['UserDataChanged'],
|
|
|
|
'remove': result['ItemsRemoved']
|
|
|
|
}
|
|
|
|
|
|
|
|
except (KeyError, TypeError):
|
2016-07-24 08:59:48 +00:00
|
|
|
log.error("Failed to retrieve latest updates using fast sync.")
|
2016-03-31 20:25:45 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Fast sync changes: %s" % result)
|
2016-03-31 20:25:45 +00:00
|
|
|
for action in processlist:
|
|
|
|
self.triage_items(action, processlist[action])
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def saveLastSync(self):
|
|
|
|
|
|
|
|
# Save last sync time
|
|
|
|
overlap = 2
|
|
|
|
|
|
|
|
try: # datetime fails when used more than once, TypeError
|
2016-09-05 04:45:11 +00:00
|
|
|
if self.isFastSync:
|
2016-08-06 04:21:32 +00:00
|
|
|
result = self.doUtils("{server}/emby/Emby.Kodi.SyncQueue/GetServerDateTime?format=json")
|
|
|
|
server_time = result['ServerDateTime']
|
|
|
|
server_time = utils.convertDate(server_time)
|
|
|
|
else:
|
|
|
|
raise Exception("Fast sync server plugin is not enabled.")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# If the server plugin is not installed or an error happened.
|
2016-08-06 04:21:32 +00:00
|
|
|
log.debug("An exception occurred: %s" % e)
|
2016-03-31 20:25:45 +00:00
|
|
|
time_now = datetime.utcnow()-timedelta(minutes=overlap)
|
|
|
|
lastSync = time_now.strftime('%Y-%m-%dT%H:%M:%SZ')
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("New sync time: client time -%s min: %s" % (overlap, lastSync))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
lastSync = (server_time - timedelta(minutes=overlap)).strftime('%Y-%m-%dT%H:%M:%SZ')
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("New sync time: server time -%s min: %s" % (overlap, lastSync))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
finally:
|
2016-06-18 03:03:28 +00:00
|
|
|
settings('LastIncrementalSync', value=lastSync)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def dbCommit(self, connection):
|
|
|
|
# Central commit, verifies if Kodi database update is running
|
|
|
|
kodidb_scan = window('emby_kodiScan') == "true"
|
2016-10-03 23:59:10 +00:00
|
|
|
count = 0
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
while kodidb_scan:
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Kodi scan is running. Waiting...")
|
2016-03-31 20:25:45 +00:00
|
|
|
kodidb_scan = window('emby_kodiScan') == "true"
|
|
|
|
|
2016-10-03 23:59:10 +00:00
|
|
|
if count == 10:
|
|
|
|
log.info("Flag still active, but will try to commit")
|
|
|
|
window('emby_kodiScan', clear=True)
|
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
if should_stop():
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Commit unsuccessful. Sync terminated.")
|
2016-03-31 20:25:45 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if self.monitor.waitForAbort(1):
|
|
|
|
# Abort was requested while waiting. We should exit
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Commit unsuccessful.")
|
2016-03-31 20:25:45 +00:00
|
|
|
break
|
2016-10-03 23:59:10 +00:00
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
try:
|
2016-03-31 20:25:45 +00:00
|
|
|
connection.commit()
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Commit successful.")
|
2016-10-03 23:59:10 +00:00
|
|
|
except sqlite3.OperationalError as error:
|
|
|
|
log.error(error)
|
|
|
|
if "database is locked" in error:
|
|
|
|
log.info("retrying...")
|
|
|
|
window('emby_kodiScan', value="true")
|
|
|
|
self.dbCommit(connection)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-07-03 03:26:11 +00:00
|
|
|
def fullSync(self, manualrun=False, repair=False):
|
2016-03-31 20:25:45 +00:00
|
|
|
# Only run once when first setting up. Can be run manually.
|
2016-06-18 03:03:28 +00:00
|
|
|
music_enabled = settings('enableMusic') == "true"
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(true)')
|
|
|
|
screensaver = utils.getScreensaver()
|
|
|
|
utils.setScreensaver(value="")
|
|
|
|
window('emby_dbScan', value="true")
|
|
|
|
# Add sources
|
|
|
|
utils.sourcesXML()
|
|
|
|
|
2016-10-20 06:55:19 +00:00
|
|
|
embyconn = utils.kodiSQL('emby')
|
2016-10-20 06:55:53 +00:00
|
|
|
embycursor = embyconn.cursor()
|
2016-03-31 20:25:45 +00:00
|
|
|
# content sync: movies, tvshows, musicvideos, music
|
|
|
|
kodiconn = utils.kodiSQL('video')
|
|
|
|
kodicursor = kodiconn.cursor()
|
|
|
|
|
|
|
|
if manualrun:
|
|
|
|
message = "Manual sync"
|
|
|
|
elif repair:
|
|
|
|
message = "Repair sync"
|
2016-09-20 09:17:11 +00:00
|
|
|
repair_list = []
|
|
|
|
choices = ['all', 'movies', 'musicvideos', 'tvshows']
|
|
|
|
if music_enabled:
|
|
|
|
choices.append('music')
|
|
|
|
|
|
|
|
if self.kodi_version > 15:
|
|
|
|
# Jarvis or higher
|
2016-09-20 09:20:00 +00:00
|
|
|
types = xbmcgui.Dialog().multiselect(lang(33094), choices)
|
2016-09-20 09:17:11 +00:00
|
|
|
if types is None:
|
|
|
|
pass
|
|
|
|
elif 0 in types: # all
|
|
|
|
choices.pop(0)
|
|
|
|
repair_list.extend(choices)
|
|
|
|
else:
|
|
|
|
for index in types:
|
|
|
|
repair_list.append(choices[index])
|
|
|
|
else:
|
2016-09-20 09:20:00 +00:00
|
|
|
resp = xbmcgui.Dialog().select(lang(33094), choices)
|
2016-09-20 09:17:11 +00:00
|
|
|
if resp == 0: # all
|
|
|
|
choices.pop(resp)
|
|
|
|
repair_list.extend(choices)
|
|
|
|
else:
|
|
|
|
repair_list.append(choices[resp])
|
|
|
|
|
|
|
|
log.info("Repair queued for: %s", repair_list)
|
2016-03-31 20:25:45 +00:00
|
|
|
else:
|
|
|
|
message = "Initial sync"
|
|
|
|
window('emby_initialScan', value="true")
|
|
|
|
|
2016-07-03 03:26:11 +00:00
|
|
|
pDialog = self.progressDialog("%s" % message)
|
2016-03-31 20:25:45 +00:00
|
|
|
starttotal = datetime.now()
|
|
|
|
|
|
|
|
# Set views
|
|
|
|
self.maintainViews(embycursor, kodicursor)
|
|
|
|
embyconn.commit()
|
|
|
|
|
|
|
|
# Sync video library
|
|
|
|
process = {
|
|
|
|
|
|
|
|
'movies': self.movies,
|
|
|
|
'musicvideos': self.musicvideos,
|
|
|
|
'tvshows': self.tvshows
|
|
|
|
}
|
|
|
|
for itemtype in process:
|
2016-09-20 09:17:11 +00:00
|
|
|
|
|
|
|
if repair and itemtype not in repair_list:
|
|
|
|
continue
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
startTime = datetime.now()
|
|
|
|
completed = process[itemtype](embycursor, kodicursor, pDialog)
|
|
|
|
if not completed:
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
|
|
|
utils.setScreensaver(value=screensaver)
|
|
|
|
window('emby_dbScan', clear=True)
|
|
|
|
if pDialog:
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
embycursor.close()
|
|
|
|
kodicursor.close()
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.dbCommit(kodiconn)
|
|
|
|
embyconn.commit()
|
|
|
|
elapsedTime = datetime.now() - startTime
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("SyncDatabase (finished %s in: %s)"
|
|
|
|
% (itemtype, str(elapsedTime).split('.')[0]))
|
2016-03-31 20:25:45 +00:00
|
|
|
else:
|
|
|
|
# Close the Kodi cursor
|
|
|
|
kodicursor.close()
|
|
|
|
|
|
|
|
# sync music
|
|
|
|
if music_enabled:
|
|
|
|
|
2016-09-20 09:17:11 +00:00
|
|
|
if repair and 'music' not in repair_list:
|
|
|
|
pass
|
2016-03-31 20:25:45 +00:00
|
|
|
else:
|
2016-09-20 09:17:11 +00:00
|
|
|
musicconn = utils.kodiSQL('music')
|
|
|
|
musiccursor = musicconn.cursor()
|
|
|
|
|
|
|
|
startTime = datetime.now()
|
|
|
|
completed = self.music(embycursor, musiccursor, pDialog)
|
|
|
|
if not completed:
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
|
|
|
utils.setScreensaver(value=screensaver)
|
|
|
|
window('emby_dbScan', clear=True)
|
|
|
|
if pDialog:
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
embycursor.close()
|
|
|
|
musiccursor.close()
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
musicconn.commit()
|
|
|
|
embyconn.commit()
|
|
|
|
elapsedTime = datetime.now() - startTime
|
|
|
|
log.info("SyncDatabase (finished music in: %s)"
|
|
|
|
% (str(elapsedTime).split('.')[0]))
|
|
|
|
musiccursor.close()
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
if pDialog:
|
|
|
|
pDialog.close()
|
|
|
|
|
2016-09-15 10:31:15 +00:00
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
current_version = emby_db.get_version(self.clientInfo.get_version())
|
|
|
|
window('emby_version', current_version)
|
|
|
|
embyconn.commit()
|
2016-03-31 20:25:45 +00:00
|
|
|
embycursor.close()
|
|
|
|
|
|
|
|
settings('SyncInstallRunDone', value="true")
|
2016-09-15 10:31:15 +00:00
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
self.saveLastSync()
|
|
|
|
xbmc.executebuiltin('UpdateLibrary(video)')
|
|
|
|
elapsedtotal = datetime.now() - starttotal
|
|
|
|
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
|
|
|
utils.setScreensaver(value=screensaver)
|
|
|
|
window('emby_dbScan', clear=True)
|
|
|
|
window('emby_initialScan', clear=True)
|
2016-07-03 03:26:11 +00:00
|
|
|
|
|
|
|
xbmcgui.Dialog().notification(
|
|
|
|
heading=lang(29999),
|
|
|
|
message="%s %s %s" %
|
|
|
|
(message, lang(33025), str(elapsedtotal).split('.')[0]),
|
|
|
|
icon="special://home/addons/plugin.video.emby/icon.png",
|
|
|
|
sound=False)
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def refreshViews(self):
|
|
|
|
|
|
|
|
embyconn = utils.kodiSQL('emby')
|
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
kodiconn = utils.kodiSQL('video')
|
|
|
|
kodicursor = kodiconn.cursor()
|
|
|
|
|
|
|
|
# Compare views, assign correct tags to items
|
|
|
|
self.maintainViews(embycursor, kodicursor)
|
|
|
|
|
|
|
|
self.dbCommit(kodiconn)
|
|
|
|
kodicursor.close()
|
|
|
|
|
|
|
|
embyconn.commit()
|
|
|
|
embycursor.close()
|
|
|
|
|
|
|
|
def maintainViews(self, embycursor, kodicursor):
|
|
|
|
|
|
|
|
# Compare the views to emby
|
|
|
|
emby = self.emby
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
kodi_db = kodidb.Kodidb_Functions(kodicursor)
|
|
|
|
|
|
|
|
# Get views
|
|
|
|
result = self.doUtils("{server}/emby/Users/{UserId}/Views?format=json")
|
|
|
|
grouped_views = result['Items']
|
|
|
|
ordered_views = self.emby.getViews(sortedlist=True)
|
|
|
|
all_views = []
|
|
|
|
sorted_views = []
|
|
|
|
for view in ordered_views:
|
|
|
|
all_views.append(view['name'])
|
|
|
|
if view['type'] == "music":
|
|
|
|
continue
|
|
|
|
|
|
|
|
if view['type'] == "mixed":
|
|
|
|
sorted_views.append(view['name'])
|
|
|
|
sorted_views.append(view['name'])
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Sorted views: %s" % sorted_views)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# total nodes for window properties
|
|
|
|
self.vnodes.clearProperties()
|
|
|
|
totalnodes = len(sorted_views) + 0
|
|
|
|
|
|
|
|
current_views = emby_db.getViews()
|
|
|
|
# Set views for supported media type
|
|
|
|
emby_mediatypes = {
|
|
|
|
|
|
|
|
'movies': "Movie",
|
|
|
|
'tvshows': "Series",
|
|
|
|
'musicvideos': "MusicVideo",
|
|
|
|
'homevideos': "Video",
|
|
|
|
'music': "Audio",
|
|
|
|
'photos': "Photo"
|
|
|
|
}
|
|
|
|
for mediatype in ['movies', 'tvshows', 'musicvideos', 'homevideos', 'music', 'photos']:
|
|
|
|
|
|
|
|
nodes = [] # Prevent duplicate for nodes of the same type
|
|
|
|
playlists = [] # Prevent duplicate for playlists of the same type
|
|
|
|
# Get media folders from server
|
|
|
|
folders = self.emby.getViews(mediatype, root=True)
|
|
|
|
for folder in folders:
|
|
|
|
|
|
|
|
folderid = folder['id']
|
|
|
|
foldername = folder['name']
|
|
|
|
viewtype = folder['type']
|
|
|
|
|
|
|
|
if foldername not in all_views:
|
|
|
|
# Media folders are grouped into userview
|
|
|
|
params = {
|
|
|
|
'ParentId': folderid,
|
|
|
|
'Recursive': True,
|
|
|
|
'Limit': 1,
|
|
|
|
'IncludeItemTypes': emby_mediatypes[mediatype]
|
|
|
|
} # Get one item from server using the folderid
|
2016-06-18 03:03:28 +00:00
|
|
|
url = "{server}/emby/Users/{UserId}/Items?format=json"
|
|
|
|
result = self.doUtils(url, parameters=params)
|
2016-03-31 20:25:45 +00:00
|
|
|
try:
|
|
|
|
verifyitem = result['Items'][0]['Id']
|
|
|
|
except (TypeError, IndexError):
|
|
|
|
# Something is wrong. Keep the same folder name.
|
|
|
|
# Could be the view is empty or the connection
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for grouped_view in grouped_views:
|
|
|
|
# This is only reserved for the detection of grouped views
|
|
|
|
if (grouped_view['Type'] == "UserView" and
|
|
|
|
grouped_view.get('CollectionType') == mediatype):
|
|
|
|
# Take the userview, and validate the item belong to the view
|
|
|
|
if self.emby.verifyView(grouped_view['Id'], verifyitem):
|
|
|
|
# Take the name of the userview
|
2016-07-25 22:36:43 +00:00
|
|
|
log.info("Found corresponding view: %s %s"
|
|
|
|
% (grouped_view['Name'], grouped_view['Id']))
|
2016-03-31 20:25:45 +00:00
|
|
|
foldername = grouped_view['Name']
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Unable to find a match, add the name to our sorted_view list
|
|
|
|
sorted_views.append(foldername)
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Couldn't find corresponding grouped view: %s" % sorted_views)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# Failsafe
|
|
|
|
try:
|
|
|
|
sorted_views.index(foldername)
|
|
|
|
except ValueError:
|
|
|
|
sorted_views.append(foldername)
|
|
|
|
|
|
|
|
# Get current media folders from emby database
|
|
|
|
view = emby_db.getView_byId(folderid)
|
|
|
|
try:
|
|
|
|
current_viewname = view[0]
|
|
|
|
current_viewtype = view[1]
|
|
|
|
current_tagid = view[2]
|
|
|
|
|
|
|
|
except TypeError:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Creating viewid: %s in Emby database." % folderid)
|
2016-03-31 20:25:45 +00:00
|
|
|
tagid = kodi_db.createTag(foldername)
|
|
|
|
# Create playlist for the video library
|
|
|
|
if (foldername not in playlists and
|
|
|
|
mediatype in ('movies', 'tvshows', 'musicvideos')):
|
|
|
|
utils.playlistXSP(mediatype, foldername, folderid, viewtype)
|
|
|
|
playlists.append(foldername)
|
|
|
|
# Create the video node
|
|
|
|
if foldername not in nodes and mediatype not in ("musicvideos", "music"):
|
|
|
|
self.vnodes.viewNode(sorted_views.index(foldername), foldername, mediatype,
|
|
|
|
viewtype, folderid)
|
|
|
|
if viewtype == "mixed": # Change the value
|
|
|
|
sorted_views[sorted_views.index(foldername)] = "%ss" % foldername
|
|
|
|
nodes.append(foldername)
|
|
|
|
totalnodes += 1
|
|
|
|
# Add view to emby database
|
|
|
|
emby_db.addView(folderid, foldername, viewtype, tagid)
|
|
|
|
|
|
|
|
else:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug(' '.join((
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
"Found viewid: %s" % folderid,
|
|
|
|
"viewname: %s" % current_viewname,
|
|
|
|
"viewtype: %s" % current_viewtype,
|
2016-07-24 08:59:48 +00:00
|
|
|
"tagid: %s" % current_tagid)))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# View is still valid
|
|
|
|
try:
|
|
|
|
current_views.remove(folderid)
|
|
|
|
except ValueError:
|
|
|
|
# View was just created, nothing to remove
|
|
|
|
pass
|
|
|
|
|
|
|
|
# View was modified, update with latest info
|
|
|
|
if current_viewname != foldername:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("viewid: %s new viewname: %s" % (folderid, foldername))
|
2016-03-31 20:25:45 +00:00
|
|
|
tagid = kodi_db.createTag(foldername)
|
|
|
|
|
|
|
|
# Update view with new info
|
|
|
|
emby_db.updateView(foldername, tagid, folderid)
|
|
|
|
|
|
|
|
if mediatype != "music":
|
|
|
|
if emby_db.getView_byName(current_viewname) is None:
|
|
|
|
# The tag could be a combined view. Ensure there's no other tags
|
|
|
|
# with the same name before deleting playlist.
|
|
|
|
utils.playlistXSP(
|
|
|
|
mediatype, current_viewname, folderid, current_viewtype, True)
|
|
|
|
# Delete video node
|
|
|
|
if mediatype != "musicvideos":
|
|
|
|
self.vnodes.viewNode(
|
|
|
|
indexnumber=None,
|
|
|
|
tagname=current_viewname,
|
|
|
|
mediatype=mediatype,
|
|
|
|
viewtype=current_viewtype,
|
|
|
|
viewid=folderid,
|
|
|
|
delete=True)
|
|
|
|
# Added new playlist
|
|
|
|
if (foldername not in playlists and
|
|
|
|
mediatype in ('movies', 'tvshows', 'musicvideos')):
|
|
|
|
utils.playlistXSP(mediatype, foldername, folderid, viewtype)
|
|
|
|
playlists.append(foldername)
|
|
|
|
# Add new video node
|
|
|
|
if foldername not in nodes and mediatype != "musicvideos":
|
|
|
|
self.vnodes.viewNode(sorted_views.index(foldername), foldername,
|
|
|
|
mediatype, viewtype, folderid)
|
|
|
|
if viewtype == "mixed": # Change the value
|
|
|
|
sorted_views[sorted_views.index(foldername)] = "%ss" % foldername
|
|
|
|
nodes.append(foldername)
|
|
|
|
totalnodes += 1
|
|
|
|
|
|
|
|
# Update items with new tag
|
|
|
|
items = emby_db.getItem_byView(folderid)
|
|
|
|
for item in items:
|
|
|
|
# Remove the "s" from viewtype for tags
|
|
|
|
kodi_db.updateTag(
|
|
|
|
current_tagid, tagid, item[0], current_viewtype[:-1])
|
|
|
|
else:
|
|
|
|
# Validate the playlist exists or recreate it
|
|
|
|
if mediatype != "music":
|
|
|
|
if (foldername not in playlists and
|
|
|
|
mediatype in ('movies', 'tvshows', 'musicvideos')):
|
|
|
|
utils.playlistXSP(mediatype, foldername, folderid, viewtype)
|
|
|
|
playlists.append(foldername)
|
|
|
|
# Create the video node if not already exists
|
|
|
|
if foldername not in nodes and mediatype != "musicvideos":
|
|
|
|
self.vnodes.viewNode(sorted_views.index(foldername), foldername,
|
|
|
|
mediatype, viewtype, folderid)
|
|
|
|
if viewtype == "mixed": # Change the value
|
|
|
|
sorted_views[sorted_views.index(foldername)] = "%ss" % foldername
|
|
|
|
nodes.append(foldername)
|
|
|
|
totalnodes += 1
|
|
|
|
else:
|
|
|
|
# Add video nodes listings
|
|
|
|
self.vnodes.singleNode(totalnodes, "Favorite movies", "movies", "favourites")
|
|
|
|
totalnodes += 1
|
|
|
|
self.vnodes.singleNode(totalnodes, "Favorite tvshows", "tvshows", "favourites")
|
|
|
|
totalnodes += 1
|
2016-09-23 18:46:04 +00:00
|
|
|
self.vnodes.singleNode(totalnodes, "Favorite episodes", "episodes", "favourites")
|
|
|
|
totalnodes += 1
|
2016-03-31 20:25:45 +00:00
|
|
|
self.vnodes.singleNode(totalnodes, "channels", "movies", "channels")
|
|
|
|
totalnodes += 1
|
|
|
|
# Save total
|
2016-06-21 01:57:29 +00:00
|
|
|
window('Emby.nodes.total', str(totalnodes))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# Remove any old referenced views
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Removing views: %s" % current_views)
|
2016-03-31 20:25:45 +00:00
|
|
|
for view in current_views:
|
|
|
|
emby_db.removeView(view)
|
2016-10-18 07:15:28 +00:00
|
|
|
# Remove any items that belongs to the old view
|
|
|
|
items = emby_db.get_item_by_view(view)
|
|
|
|
items = [i[0] for i in items] # Convert list of tuple to list
|
|
|
|
self.triage_items("remove", items)
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def movies(self, embycursor, kodicursor, pdialog):
|
|
|
|
|
|
|
|
# Get movies from emby
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
2016-10-10 08:19:00 +00:00
|
|
|
movies = Movies(embycursor, kodicursor, pdialog)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
views = emby_db.getView_byType('movies')
|
|
|
|
views += emby_db.getView_byType('mixed')
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Media folders: %s" % views)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
##### PROCESS MOVIES #####
|
|
|
|
for view in views:
|
|
|
|
|
2016-09-16 02:59:39 +00:00
|
|
|
log.info("Processing: %s", view)
|
2016-10-10 08:19:00 +00:00
|
|
|
view_name = view['name']
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# Get items per view
|
|
|
|
if pdialog:
|
|
|
|
pdialog.update(
|
2016-06-21 01:57:29 +00:00
|
|
|
heading=lang(29999),
|
2016-10-10 08:19:00 +00:00
|
|
|
message="%s %s..." % (lang(33017), view_name))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
all_movies = self.emby.getMovies(view['id'], dialog=pdialog)
|
|
|
|
movies.add_all("Movie", all_movies, view)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
log.debug("Movies finished.")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
##### PROCESS BOXSETS #####
|
|
|
|
if pdialog:
|
2016-06-21 01:57:29 +00:00
|
|
|
pdialog.update(heading=lang(29999), message=lang(33018))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
boxsets = self.emby.getBoxset(dialog=pdialog)
|
|
|
|
total = boxsets['TotalRecordCount']
|
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
movies.process_all("BoxSet", "added", boxsets['Items'], total)
|
|
|
|
log.debug("Boxsets finished.")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def musicvideos(self, embycursor, kodicursor, pdialog):
|
|
|
|
|
|
|
|
# Get musicvideos from emby
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
2016-10-10 08:19:00 +00:00
|
|
|
mvideos = MusicVideos(embycursor, kodicursor, pdialog)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
views = emby_db.getView_byType('musicvideos')
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Media folders: %s" % views)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
for view in views:
|
2016-10-10 08:19:00 +00:00
|
|
|
log.info("Processing: %s", view)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# Get items per view
|
|
|
|
viewId = view['id']
|
|
|
|
viewName = view['name']
|
|
|
|
|
|
|
|
if pdialog:
|
|
|
|
pdialog.update(
|
2016-06-21 01:57:29 +00:00
|
|
|
heading=lang(29999),
|
2016-06-18 03:03:28 +00:00
|
|
|
message="%s %s..." % (lang(33019), viewName))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
# Initial or repair sync
|
2016-10-10 08:19:00 +00:00
|
|
|
all_mvideos = self.emby.getMusicVideos(viewId, dialog=pdialog)
|
|
|
|
mvideos.add_all("MusicVideo", all_mvideos, view)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
else:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("MusicVideos finished.")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def tvshows(self, embycursor, kodicursor, pdialog):
|
|
|
|
|
|
|
|
# Get shows from emby
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
2016-10-10 08:19:00 +00:00
|
|
|
tvshows = TVShows(embycursor, kodicursor, pdialog)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
views = emby_db.getView_byType('tvshows')
|
|
|
|
views += emby_db.getView_byType('mixed')
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Media folders: %s" % views)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
for view in views:
|
|
|
|
|
|
|
|
# Get items per view
|
|
|
|
if pdialog:
|
|
|
|
pdialog.update(
|
2016-06-21 01:57:29 +00:00
|
|
|
heading=lang(29999),
|
2016-06-18 03:03:28 +00:00
|
|
|
message="%s %s..." % (lang(33020), view['name']))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
all_tvshows = self.emby.getShows(view['id'], dialog=pdialog)
|
|
|
|
tvshows.add_all("Series", all_tvshows, view)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
else:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("TVShows finished.")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def music(self, embycursor, kodicursor, pdialog):
|
|
|
|
# Get music from emby
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
2016-10-10 08:19:00 +00:00
|
|
|
music = Music(embycursor, kodicursor, pdialog)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
views = emby_db.getView_byType('music')
|
|
|
|
log.info("Media folders: %s", views)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
# Add music artists and everything will fall into place
|
|
|
|
if pdialog:
|
|
|
|
pdialog.update(heading=lang(29999),
|
|
|
|
message="%s Music..." % lang(33021))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
for view in views:
|
|
|
|
all_artists = self.emby.getArtists(view['id'], dialog=pdialog)
|
|
|
|
music.add_all("MusicArtist", all_artists)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-10 08:19:00 +00:00
|
|
|
log.debug("Finished syncing music")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Reserved for websocket_client.py and fast start
|
|
|
|
def triage_items(self, process, items):
|
|
|
|
|
|
|
|
processlist = {
|
|
|
|
|
|
|
|
'added': self.addedItems,
|
|
|
|
'update': self.updateItems,
|
|
|
|
'userdata': self.userdataItems,
|
|
|
|
'remove': self.removeItems
|
|
|
|
}
|
|
|
|
if items:
|
|
|
|
if process == "userdata":
|
|
|
|
itemids = []
|
|
|
|
for item in items:
|
|
|
|
itemids.append(item['ItemId'])
|
|
|
|
items = itemids
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Queue %s: %s" % (process, items))
|
2016-03-31 20:25:45 +00:00
|
|
|
processlist[process].extend(items)
|
|
|
|
|
|
|
|
def incrementalSync(self):
|
|
|
|
|
|
|
|
embyconn = utils.kodiSQL('emby')
|
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
kodiconn = utils.kodiSQL('video')
|
|
|
|
kodicursor = kodiconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
pDialog = None
|
|
|
|
update_embydb = False
|
|
|
|
|
|
|
|
if self.refresh_views:
|
|
|
|
# Received userconfig update
|
|
|
|
self.refresh_views = False
|
|
|
|
self.maintainViews(embycursor, kodicursor)
|
|
|
|
self.forceLibraryUpdate = True
|
|
|
|
update_embydb = True
|
|
|
|
|
2016-10-20 07:18:55 +00:00
|
|
|
incSyncIndicator = int(settings('incSyncIndicator') or 10)
|
2016-07-03 03:26:11 +00:00
|
|
|
totalUpdates = len(self.addedItems) + len(self.updateItems) + len(self.userdataItems) + len(self.removeItems)
|
|
|
|
|
|
|
|
if incSyncIndicator != -1 and totalUpdates > incSyncIndicator:
|
2016-03-31 20:25:45 +00:00
|
|
|
# Only present dialog if we are going to process items
|
|
|
|
pDialog = self.progressDialog('Incremental sync')
|
2016-07-24 23:46:24 +00:00
|
|
|
log.info("incSyncIndicator=" + str(incSyncIndicator) + " totalUpdates=" + str(totalUpdates))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
process = {
|
|
|
|
|
|
|
|
'added': self.addedItems,
|
|
|
|
'update': self.updateItems,
|
|
|
|
'userdata': self.userdataItems,
|
|
|
|
'remove': self.removeItems
|
|
|
|
}
|
|
|
|
for process_type in ['added', 'update', 'userdata', 'remove']:
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
if process[process_type] and window('emby_kodiScan') != "true":
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
listItems = list(process[process_type])
|
|
|
|
del process[process_type][:] # Reset class list
|
|
|
|
|
|
|
|
items_process = itemtypes.Items(embycursor, kodicursor)
|
|
|
|
update = False
|
|
|
|
|
|
|
|
# Prepare items according to process process_type
|
|
|
|
if process_type == "added":
|
|
|
|
items = self.emby.sortby_mediatype(listItems)
|
|
|
|
|
|
|
|
elif process_type in ("userdata", "remove"):
|
|
|
|
items = emby_db.sortby_mediaType(listItems, unsorted=False)
|
|
|
|
|
|
|
|
else:
|
|
|
|
items = emby_db.sortby_mediaType(listItems)
|
|
|
|
if items.get('Unsorted'):
|
|
|
|
sorted_items = self.emby.sortby_mediatype(items['Unsorted'])
|
|
|
|
doupdate = items_process.itemsbyId(sorted_items, "added", pDialog)
|
|
|
|
if doupdate:
|
|
|
|
embyupdate, kodiupdate_video = doupdate
|
|
|
|
if embyupdate:
|
|
|
|
update_embydb = True
|
|
|
|
if kodiupdate_video:
|
|
|
|
self.forceLibraryUpdate = True
|
|
|
|
del items['Unsorted']
|
|
|
|
|
|
|
|
doupdate = items_process.itemsbyId(items, process_type, pDialog)
|
|
|
|
if doupdate:
|
|
|
|
embyupdate, kodiupdate_video = doupdate
|
|
|
|
if embyupdate:
|
|
|
|
update_embydb = True
|
|
|
|
if kodiupdate_video:
|
|
|
|
self.forceLibraryUpdate = True
|
|
|
|
|
|
|
|
if update_embydb:
|
|
|
|
update_embydb = False
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Updating emby database.")
|
2016-03-31 20:25:45 +00:00
|
|
|
embyconn.commit()
|
|
|
|
self.saveLastSync()
|
|
|
|
|
|
|
|
if self.forceLibraryUpdate:
|
|
|
|
# Force update the Kodi library
|
|
|
|
self.forceLibraryUpdate = False
|
|
|
|
self.dbCommit(kodiconn)
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("Updating video library.")
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_kodiScan', value="true")
|
2016-03-31 20:25:45 +00:00
|
|
|
xbmc.executebuiltin('UpdateLibrary(video)')
|
|
|
|
|
|
|
|
if pDialog:
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
kodicursor.close()
|
|
|
|
embycursor.close()
|
|
|
|
|
|
|
|
|
|
|
|
def compareDBVersion(self, current, minimum):
|
|
|
|
# It returns True is database is up to date. False otherwise.
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("current: %s minimum: %s" % (current, minimum))
|
2016-09-15 10:31:15 +00:00
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
currMajor, currMinor, currPatch = current.split(".")
|
|
|
|
minMajor, minMinor, minPatch = minimum.split(".")
|
|
|
|
|
|
|
|
if currMajor > minMajor:
|
|
|
|
return True
|
|
|
|
elif currMajor == minMajor and (currMinor > minMinor or
|
|
|
|
(currMinor == minMinor and currPatch >= minPatch)):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# Database out of date.
|
|
|
|
return False
|
|
|
|
|
2016-10-20 06:47:42 +00:00
|
|
|
def _verify_emby_database(cls):
|
|
|
|
# Create the tables for the emby database
|
|
|
|
conn = utils.kodiSQL('emby')
|
|
|
|
cursor = conn.cursor()
|
|
|
|
# emby, view, version
|
|
|
|
cursor.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS emby(
|
|
|
|
emby_id TEXT UNIQUE, media_folder TEXT, emby_type TEXT, media_type TEXT,
|
|
|
|
kodi_id INTEGER, kodi_fileid INTEGER, kodi_pathid INTEGER, parent_id INTEGER,
|
|
|
|
checksum INTEGER)""")
|
|
|
|
cursor.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS view(
|
|
|
|
view_id TEXT UNIQUE, view_name TEXT, media_type TEXT, kodi_tagid INTEGER)""")
|
|
|
|
cursor.execute("CREATE TABLE IF NOT EXISTS version(idVersion TEXT)")
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
def run(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.run_internal()
|
2016-09-04 10:18:31 +00:00
|
|
|
except Warning as e:
|
|
|
|
if "restricted" in e:
|
2016-09-05 03:58:46 +00:00
|
|
|
pass
|
|
|
|
elif "401" in e:
|
2016-09-04 10:18:31 +00:00
|
|
|
pass
|
2016-03-31 20:25:45 +00:00
|
|
|
except Exception as e:
|
2016-10-09 22:05:51 +00:00
|
|
|
ga = GoogleAnalytics()
|
2016-10-09 22:50:30 +00:00
|
|
|
errStrings = ga.formatException()
|
|
|
|
ga.sendEventData("Exception", errStrings[0], errStrings[1])
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_dbScan', clear=True)
|
2016-07-25 08:08:45 +00:00
|
|
|
log.exception(e)
|
2016-03-31 20:25:45 +00:00
|
|
|
xbmcgui.Dialog().ok(
|
2016-06-21 01:57:29 +00:00
|
|
|
heading=lang(29999),
|
2016-03-31 20:25:45 +00:00
|
|
|
line1=(
|
|
|
|
"Library sync thread has exited! "
|
|
|
|
"You should restart Kodi now. "
|
2016-10-09 22:05:51 +00:00
|
|
|
"Please report this on the forum."),
|
2016-10-09 22:50:30 +00:00
|
|
|
line2=(errStrings[0] + " (" + errStrings[1] + ")"))
|
2016-06-28 06:09:36 +00:00
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
def run_internal(self):
|
|
|
|
|
|
|
|
dialog = xbmcgui.Dialog()
|
|
|
|
|
|
|
|
startupComplete = False
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.warn("---===### Starting LibrarySync ###===---")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-10-20 06:47:42 +00:00
|
|
|
# Verify database structure, otherwise create it.
|
|
|
|
self._verify_emby_database()
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
while not self.monitor.abortRequested():
|
|
|
|
|
|
|
|
# In the event the server goes offline
|
|
|
|
while self.suspend_thread:
|
|
|
|
# Set in service.py
|
|
|
|
if self.monitor.waitForAbort(5):
|
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
break
|
|
|
|
|
|
|
|
if (window('emby_dbCheck') != "true" and settings('SyncInstallRunDone') == "true"):
|
|
|
|
# Verify the validity of the database
|
2016-09-15 10:31:15 +00:00
|
|
|
|
|
|
|
embyconn = utils.kodiSQL('emby')
|
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
currentVersion = emby_db.get_version()
|
|
|
|
###$ Begin migration $###
|
|
|
|
if currentVersion is None:
|
|
|
|
currentVersion = emby_db.get_version(settings('dbCreatedWithVersion'))
|
|
|
|
embyconn.commit()
|
|
|
|
log.info("Migration of database version completed")
|
|
|
|
###$ End migration $###
|
|
|
|
embycursor.close()
|
|
|
|
window('emby_version', value=currentVersion)
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
minVersion = window('emby_minDBVersion')
|
|
|
|
uptoDate = self.compareDBVersion(currentVersion, minVersion)
|
|
|
|
|
|
|
|
if not uptoDate:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.warn("Database version out of date: %s minimum version required: %s"
|
|
|
|
% (currentVersion, minVersion))
|
2016-03-31 20:25:45 +00:00
|
|
|
|
2016-06-21 01:57:29 +00:00
|
|
|
resp = dialog.yesno(lang(29999), lang(33022))
|
2016-03-31 20:25:45 +00:00
|
|
|
if not resp:
|
2016-07-24 08:59:48 +00:00
|
|
|
log.warn("Database version is out of date! USER IGNORED!")
|
2016-06-21 01:57:29 +00:00
|
|
|
dialog.ok(lang(29999), lang(33023))
|
2016-03-31 20:25:45 +00:00
|
|
|
else:
|
|
|
|
utils.reset()
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
window('emby_dbCheck', value="true")
|
|
|
|
|
|
|
|
|
|
|
|
if not startupComplete:
|
|
|
|
# Verify the video database can be found
|
|
|
|
videoDb = utils.getKodiVideoDBPath()
|
|
|
|
if not xbmcvfs.exists(videoDb):
|
|
|
|
# Database does not exists
|
2016-07-24 08:59:48 +00:00
|
|
|
log.error(
|
2016-03-31 20:25:45 +00:00
|
|
|
"The current Kodi version is incompatible "
|
|
|
|
"with the Emby for Kodi add-on. Please visit "
|
|
|
|
"https://github.com/MediaBrowser/Emby.Kodi/wiki "
|
2016-07-24 08:59:48 +00:00
|
|
|
"to know which Kodi versions are supported.")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
dialog.ok(
|
2016-06-21 01:57:29 +00:00
|
|
|
heading=lang(29999),
|
2016-03-31 20:25:45 +00:00
|
|
|
line1=lang(33024))
|
2016-09-15 10:31:15 +00:00
|
|
|
break
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
# Run start up sync
|
2016-09-15 10:31:15 +00:00
|
|
|
log.warn("Database version: %s", window('emby_version'))
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("SyncDatabase (started)")
|
2016-03-31 20:25:45 +00:00
|
|
|
startTime = datetime.now()
|
|
|
|
librarySync = self.startSync()
|
|
|
|
elapsedTime = datetime.now() - startTime
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("SyncDatabase (finished in: %s) %s"
|
|
|
|
% (str(elapsedTime).split('.')[0], librarySync))
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
# Add other servers at this point
|
2016-09-27 02:53:44 +00:00
|
|
|
# TODO: re-add once plugin listing is created
|
|
|
|
# self.user.load_connect_servers()
|
|
|
|
|
2016-03-31 20:25:45 +00:00
|
|
|
# Only try the initial sync once per kodi session regardless
|
|
|
|
# This will prevent an infinite loop in case something goes wrong.
|
|
|
|
startupComplete = True
|
|
|
|
|
|
|
|
# Process updates
|
2016-10-20 06:12:23 +00:00
|
|
|
if window('emby_dbScan') != "true" and window('emby_shouldStop') != "true":
|
2016-03-31 20:25:45 +00:00
|
|
|
self.incrementalSync()
|
|
|
|
|
|
|
|
if window('emby_onWake') == "true" and window('emby_online') == "true":
|
|
|
|
# Kodi is waking up
|
|
|
|
# Set in kodimonitor.py
|
|
|
|
window('emby_onWake', clear=True)
|
|
|
|
if window('emby_syncRunning') != "true":
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("SyncDatabase onWake (started)")
|
2016-03-31 20:25:45 +00:00
|
|
|
librarySync = self.startSync()
|
2016-07-24 08:59:48 +00:00
|
|
|
log.info("SyncDatabase onWake (finished) %s" % librarySync)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
if self.stop_thread:
|
|
|
|
# Set in service.py
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Service terminated thread.")
|
2016-03-31 20:25:45 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if self.monitor.waitForAbort(1):
|
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
break
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.warn("###===--- LibrarySync Stopped ---===###")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def stopThread(self):
|
|
|
|
self.stop_thread = True
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Ending thread...")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def suspendThread(self):
|
|
|
|
self.suspend_thread = True
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Pausing thread...")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def resumeThread(self):
|
|
|
|
self.suspend_thread = False
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("Resuming thread...")
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManualSync(LibrarySync):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
LibrarySync.__init__(self)
|
|
|
|
|
2016-07-03 03:26:11 +00:00
|
|
|
def sync(self):
|
|
|
|
return self.fullSync(manualrun=True)
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def movies(self, embycursor, kodicursor, pdialog):
|
2016-10-10 08:19:00 +00:00
|
|
|
return Movies(embycursor, kodicursor, pdialog).compare_all()
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def musicvideos(self, embycursor, kodicursor, pdialog):
|
2016-10-10 08:19:00 +00:00
|
|
|
return MusicVideos(embycursor, kodicursor, pdialog).compare_all()
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def tvshows(self, embycursor, kodicursor, pdialog):
|
2016-10-10 08:19:00 +00:00
|
|
|
return TVShows(embycursor, kodicursor, pdialog).compare_all()
|
2016-03-31 20:25:45 +00:00
|
|
|
|
|
|
|
def music(self, embycursor, kodicursor, pdialog):
|
2016-10-10 08:19:00 +00:00
|
|
|
return Music(embycursor, kodicursor).compare_all()
|