jellyfin-kodi/resources/lib/librarysync.py

843 lines
32 KiB
Python
Raw Normal View History

2016-03-31 20:25:45 +00:00
# -*- coding: utf-8 -*-
##################################################################################################
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 database
2016-03-31 20:25:45 +00:00
import downloadutils
import itemtypes
2018-03-13 10:32:45 +00:00
import emby as mb
2016-03-31 20:25:45 +00:00
import embydb_functions as embydb
import read_embyserver as embyserver
import userclient
import views
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
##################################################################################################
log = logging.getLogger("EMBY."+__name__)
##################################################################################################
2016-03-31 20:25:45 +00:00
class LibrarySync(threading.Thread):
_shared_state = {}
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
2017-11-25 23:33:44 +00:00
incremental_count = 0
2016-03-31 20:25:45 +00:00
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.kodi_version = int(xbmc.getInfoLabel('System.BuildVersion')[:2])
2016-03-31 20:25:45 +00:00
threading.Thread.__init__(self)
def progressDialog(self, title):
2016-03-31 20:25:45 +00:00
dialog = None
dialog = xbmcgui.DialogProgressBG()
dialog.create("Emby for Kodi", title)
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"
try:
result = self.doUtils(url)
except Exception as error:
log.info("Error getting plugin list form server: " + str(error))
result = []
2016-03-31 20:25:45 +00:00
for plugin in result:
2018-03-23 05:12:00 +00:00
if plugin['Name'] in ("Emby.Kodi Sync Queue", "Kodi companion"):
log.debug("Found server plugin.")
self.isFastSync = True
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
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)
log.info("Last sync run: %s" % lastSyncTime)
2016-03-31 20:25:45 +00:00
# get server RetentionDateTime
try:
2016-11-30 20:26:26 +00:00
result = self.doUtils("{server}/emby/Emby.Kodi.SyncQueue/GetServerDateTime?format=json")
2016-03-31 20:25:45 +00:00
retention_time = result['RetentionDateTime']
2016-11-30 20:26:26 +00:00
except Exception as error:
log.error(error)
2016-03-31 20:25:45 +00:00
retention_time = "2010-01-01T00:00:00Z"
2016-06-18 03:03:28 +00:00
retention_time = utils.convertDate(retention_time)
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:
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}
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"
2016-03-31 20:25:45 +00:00
try:
result = self.doUtils(url, parameters=params)
2016-03-31 20:25:45 +00:00
processlist = {
'added': result['ItemsAdded'],
'update': result['ItemsUpdated'],
'userdata': result['UserDataChanged'],
'remove': result['ItemsRemoved']
}
except Exception as error: # To be reviewed to only catch specific errors.
log.error(error)
log.error("Failed to retrieve latest updates using fast sync.")
2016-11-28 08:03:27 +00:00
xbmcgui.Dialog().ok(lang(29999), lang(33095))
2016-03-31 20:25:45 +00:00
return False
else:
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:
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.
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')
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')
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"
count = 0
2016-03-31 20:25:45 +00:00
while kodidb_scan:
log.info("Kodi scan is running. Waiting...")
2016-03-31 20:25:45 +00:00
kodidb_scan = window('emby_kodiScan') == "true"
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():
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
log.info("Commit unsuccessful.")
2016-03-31 20:25:45 +00:00
break
count += 1
try:
2016-03-31 20:25:45 +00:00
connection.commit()
log.info("Commit successful.")
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
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-11-05 02:19:57 +00:00
# use emby and video DBs
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
2016-11-05 02:19:57 +00:00
# content sync: movies, tvshows, musicvideos, music
if manualrun:
message = "Manual sync"
elif repair:
message = "Repair sync"
repair_list = []
choices = ['all', 'movies', 'musicvideos', 'tvshows']
if music_enabled:
choices.append('music')
if self.kodi_version > 15:
# Jarvis or higher
types = xbmcgui.Dialog().multiselect(lang(33094), choices)
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:
resp = xbmcgui.Dialog().select(lang(33094), choices)
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)
else:
2016-11-05 02:19:57 +00:00
message = "Initial sync"
window('emby_initialScan', value="true")
2016-11-05 02:19:57 +00:00
pDialog = self.progressDialog("%s" % message)
starttotal = datetime.now()
2016-03-31 20:25:45 +00:00
2016-11-05 02:19:57 +00:00
# Set views
views.Views(cursor_emby, cursor_video).maintain()
cursor_emby.connection.commit()
#self.maintainViews(cursor_emby, cursor_video)
2016-03-31 20:25:45 +00:00
2016-11-05 02:19:57 +00:00
# Sync video library
process = {
2016-03-31 20:25:45 +00:00
2016-11-05 02:19:57 +00:00
'movies': self.movies,
2017-11-03 03:34:41 +00:00
'boxsets': self.boxsets,
2016-11-05 02:19:57 +00:00
'musicvideos': self.musicvideos,
'tvshows': self.tvshows
}
for itemtype in process:
2016-03-31 20:25:45 +00:00
2016-11-05 02:19:57 +00:00
if repair and itemtype not in repair_list:
continue
2016-11-05 02:19:57 +00:00
startTime = datetime.now()
completed = process[itemtype](cursor_emby, cursor_video, pDialog)
2016-11-05 02:19:57 +00:00
if not completed:
xbmc.executebuiltin('InhibitIdleShutdown(false)')
utils.setScreensaver(value=screensaver)
window('emby_dbScan', clear=True)
if pDialog:
pDialog.close()
2016-11-05 02:19:57 +00:00
return False
else:
elapsedTime = datetime.now() - startTime
log.info("SyncDatabase (finished %s in: %s)"
% (itemtype, str(elapsedTime).split('.')[0]))
2016-03-31 20:25:45 +00:00
2016-11-05 02:19:57 +00:00
2016-03-31 20:25:45 +00:00
# sync music
2016-11-05 02:19:57 +00:00
# use emby and music
if music_enabled:
if repair and 'music' not in repair_list:
pass
2016-03-31 20:25:45 +00:00
else:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('music') as cursor_music:
2016-11-05 02:19:57 +00:00
startTime = datetime.now()
completed = self.music(cursor_emby, cursor_music, pDialog)
if not completed:
xbmc.executebuiltin('InhibitIdleShutdown(false)')
utils.setScreensaver(value=screensaver)
window('emby_dbScan', clear=True)
if pDialog:
pDialog.close()
return False
else:
elapsedTime = datetime.now() - startTime
log.info("SyncDatabase (finished music in: %s)"
% (str(elapsedTime).split('.')[0]))
2016-03-31 20:25:45 +00:00
if pDialog:
pDialog.close()
with database.DatabaseConn('emby') as cursor_emby:
emby_db = embydb.Embydb_Functions(cursor_emby)
current_version = emby_db.get_version(self.clientInfo.get_version())
2016-11-05 02:19:57 +00:00
2016-09-15 10:31:15 +00:00
window('emby_version', current_version)
2016-03-31 20:25:45 +00:00
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)
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):
2016-11-05 02:19:57 +00:00
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn() as cursor_video:
2016-11-05 02:19:57 +00:00
# Compare views, assign correct tags to items
views.Views(cursor_emby, cursor_video).maintain()
2016-03-31 20:25:45 +00:00
2017-10-04 22:34:00 +00:00
def offline_mode_views(self):
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn() as cursor_video:
views.Views(cursor_emby, cursor_video).offline_mode()
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')
log.info("Media folders: %s" % views)
2016-03-31 20:25:45 +00:00
##### PROCESS MOVIES #####
for view in views:
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
2018-03-23 08:08:54 +00:00
movies.count = 0
2018-03-13 10:32:45 +00:00
for all_movies in mb.get_items(view['id'], "Movie"):
2018-03-23 08:08:54 +00:00
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.")
2017-11-03 03:34:41 +00:00
return True
def boxsets(self, embycursor, kodicursor, pdialog):
movies = Movies(embycursor, kodicursor, pdialog)
2016-03-31 20:25:45 +00:00
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
2018-03-23 08:08:54 +00:00
movies.count = 0
2018-03-13 10:32:45 +00:00
for boxsets in mb.get_items(None, "BoxSet"):
movies.add_all("BoxSet", boxsets)
2016-03-31 20:25:45 +00:00
2017-11-03 03:34:41 +00:00
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')
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
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
2018-03-23 08:08:54 +00:00
mvideos.count = 0
2018-03-13 10:32:45 +00:00
for all_mvideos in mb.get_items(view['id'], "MusicVideo"):
2018-03-23 08:08:54 +00:00
mvideos.add_all("MusicVideo", all_mvideos, view)
2016-03-31 20:25:45 +00:00
else:
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')
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
2018-03-23 08:08:54 +00:00
tvshows.count = 0
2018-03-13 10:32:45 +00:00
for all_tvshows in mb.get_items(view['id'], "Series"):
2018-03-23 08:08:54 +00:00
tvshows.add_all("Series", all_tvshows, view)
2016-03-31 20:25:45 +00:00
else:
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:
2018-03-15 10:32:56 +00:00
2018-03-23 08:08:54 +00:00
music.count = 0
2018-03-15 10:32:56 +00:00
for all_artists in mb.get_artists(view['id']):
2018-03-23 08:08:54 +00:00
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
log.info("Queue %s: %s" % (process, items))
2016-03-31 20:25:45 +00:00
processlist[process].extend(items)
def incrementalSync(self):
2017-11-25 23:33:44 +00:00
self.incremental_count += 1
update_embydb = False
pDialog = None
# do a view update if needed
if self.refresh_views:
2016-11-05 21:39:01 +00:00
self.refreshViews()
self.refresh_views = False
self.forceLibraryUpdate = True
2016-03-31 20:25:45 +00:00
# do a lib update if any items in list
totalUpdates = len(self.addedItems) + len(self.updateItems) + len(self.userdataItems) + len(self.removeItems)
2017-11-25 23:33:44 +00:00
if totalUpdates > 0 and window('emby_kodiScan') != "true":
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
xbmc.executebuiltin('InhibitIdleShutdown(true)')
screensaver = utils.getScreensaver()
utils.setScreensaver(value="")
emby_db = embydb.Embydb_Functions(cursor_emby)
incSyncIndicator = int(settings('incSyncIndicator') or 10)
if incSyncIndicator != -1 and totalUpdates > incSyncIndicator:
# Only present dialog if we are going to process items
pDialog = self.progressDialog('Incremental sync')
log.info("incSyncIndicator=" + str(incSyncIndicator) + " totalUpdates=" + str(totalUpdates))
process = {
'added': self.addedItems,
'update': self.updateItems,
'userdata': self.userdataItems,
'remove': self.removeItems
}
for process_type in ['added', 'update', 'userdata', 'remove']:
2017-11-25 23:33:44 +00:00
if process[process_type]:
listItems = list(process[process_type])
del process[process_type][:] # Reset class list
items_process = itemtypes.Items(cursor_emby, cursor_video)
update = False
# Prepare items according to process process_type
if process_type == "added":
2018-04-03 23:38:53 +00:00
items = mb.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'):
2018-04-03 23:38:53 +00:00
sorted_items = mb.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
xbmc.executebuiltin('InhibitIdleShutdown(false)')
utils.setScreensaver(value=screensaver)
# if stuff happened then do some stuff
if update_embydb:
update_embydb = False
log.info("Updating emby database.")
self.saveLastSync()
if self.forceLibraryUpdate:
# Force update the Kodi library
self.forceLibraryUpdate = False
log.info("Updating video library.")
2017-11-25 23:33:44 +00:00
self.incremental_count = 0
window('emby_kodiScan', value="true")
xbmc.executebuiltin('UpdateLibrary(video)')
2016-03-31 20:25:45 +00:00
if pDialog:
pDialog.close()
def compareDBVersion(self, current, minimum):
# It returns True is database is up to date. False otherwise.
log.info("current: %s minimum: %s" % (current, minimum))
2016-09-15 10:31:15 +00:00
try:
currMajor, currMinor, currPatch = current.split(".")
minMajor, minMinor, minPatch = minimum.split(".")
except ValueError as error:
raise ValueError("Unable to compare versions: %s, %s" % (current, minimum))
2016-03-31 20:25:45 +00:00
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
def run(self):
try:
self.run_internal()
except Warning as e:
if "restricted" in e:
2016-09-05 03:58:46 +00:00
pass
elif "401" in e:
pass
2016-03-31 20:25:45 +00:00
except Exception as e:
ga = GoogleAnalytics()
errStrings = ga.formatException()
if not (hasattr(e, 'quiet') and e.quiet):
ga.sendEventData("Exception", errStrings[0], errStrings[1])
2016-06-18 03:03:28 +00:00
window('emby_dbScan', clear=True)
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. "
"Please report this on the forum."),
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
log.warn("---===### Starting LibrarySync ###===---")
2017-07-28 07:11:41 +00:00
if utils.verify_advancedsettings():
# Advancedsettings was modified, Kodi needs to restart
log.warn("###===--- LibrarySync Aborted ---===###")
return
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-11-05 02:19:57 +00:00
log.info("Doing DB Version Check")
with database.DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
currentVersion = emby_db.get_version()
###$ Begin migration $###
if not currentVersion:
currentVersion = emby_db.get_version(settings('dbCreatedWithVersion') or self.clientInfo.get_version())
log.info("Migration of database version completed")
###$ End migration $###
2016-09-15 10:31:15 +00:00
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:
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:
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:
database.db_reset()
2016-03-31 20:25:45 +00:00
break
window('emby_dbCheck', value="true")
if not startupComplete:
# Verify the video database can be found
videoDb = database.video_database()
2016-03-31 20:25:45 +00:00
if not xbmcvfs.exists(videoDb):
# Database does not exists
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 "
"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'))
log.info("SyncDatabase (started)")
2016-03-31 20:25:45 +00:00
startTime = datetime.now()
librarySync = self.startSync()
elapsedTime = datetime.now() - startTime
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
# 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
2017-11-25 23:33:44 +00:00
if self.incremental_count > 5:
self.incremental_count = 0
window('emby_kodiScan', clear=True)
2018-03-15 10:32:56 +00:00
if ((not xbmc.Player().isPlayingVideo() or xbmc.getCondVisibility('VideoPlayer.Content(livetv)')) and
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":
log.info("SyncDatabase onWake (started)")
2016-03-31 20:25:45 +00:00
librarySync = self.startSync()
log.info("SyncDatabase onWake (finished) %s" % librarySync)
2016-03-31 20:25:45 +00:00
if self.stop_thread:
# Set in service.py
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
log.warn("###===--- LibrarySync Stopped ---===###")
2016-03-31 20:25:45 +00:00
def stopThread(self):
self.stop_thread = True
log.debug("Ending thread...")
2016-03-31 20:25:45 +00:00
def suspendThread(self):
self.suspend_thread = True
log.debug("Pausing thread...")
2016-03-31 20:25:45 +00:00
def resumeThread(self):
self.suspend_thread = False
log.debug("Resuming thread...")
2016-03-31 20:25:45 +00:00
class ManualSync(LibrarySync):
def __init__(self):
LibrarySync.__init__(self)
2017-11-03 03:34:41 +00:00
def sync(self, mediatype=None):
if mediatype in ('movies', 'boxsets', 'musicvideos', 'tvshows'):
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
pDialog = self.progressDialog("Manual Sync: %s" % mediatype)
if mediatype == 'movies':
2017-11-09 02:58:59 +00:00
self.movies(cursor_emby, cursor_video, pDialog)
2017-11-03 03:34:41 +00:00
elif mediatype == "boxsets":
2017-11-09 02:58:59 +00:00
self.boxsets(cursor_emby, cursor_video, pDialog)
2017-11-03 03:34:41 +00:00
elif mediatype =='musicvideos':
2017-11-09 02:58:59 +00:00
self.musicvideos(cursor_emby, cursor_video, pDialog)
2017-11-03 03:34:41 +00:00
elif mediatype == 'tvshows':
2017-11-09 02:58:59 +00:00
self.tvshows(cursor_emby, cursor_video, pDialog)
pDialog.close()
return
2017-11-03 03:34:41 +00:00
elif mediatype == 'music':
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('music') as cursor_music:
pDialog = self.progressDialog("Manual Sync: %s" % mediatype)
2017-11-09 02:58:59 +00:00
self.music(cursor_emby, cursor_music, pDialog)
pDialog.close()
return
2017-11-03 03:34:41 +00:00
else:
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
2017-11-03 03:34:41 +00:00
def boxsets(self, embycursor, kodicursor, pdialog):
return Movies(embycursor, kodicursor, pdialog).force_refresh_boxsets()
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()