2015-03-14 08:24:59 +11:00
|
|
|
#################################################################################################
|
|
|
|
# LibrarySync
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
import json
|
2015-03-14 11:46:54 +11:00
|
|
|
import sqlite3
|
2015-03-17 04:51:49 +11:00
|
|
|
import inspect
|
2015-03-14 08:24:59 +11:00
|
|
|
import threading
|
|
|
|
import urllib
|
|
|
|
from datetime import datetime, timedelta, time
|
|
|
|
import urllib2
|
|
|
|
import os
|
2015-03-14 09:39:35 +11:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
from API import API
|
|
|
|
import Utils as utils
|
|
|
|
from DownloadUtils import DownloadUtils
|
2015-03-18 04:51:45 +11:00
|
|
|
from ReadEmbyDB import ReadEmbyDB
|
2015-03-18 05:41:26 +11:00
|
|
|
from ReadKodiDB import ReadKodiDB
|
2015-03-18 06:02:42 +11:00
|
|
|
from WriteKodiDB import WriteKodiDB
|
2015-03-18 05:18:17 +11:00
|
|
|
from CreateFiles import CreateFiles
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-03-19 15:38:00 +11:00
|
|
|
addondir = xbmc.translatePath(xbmcaddon.Addon(id='plugin.video.mb3sync').getAddonInfo('profile'))
|
2015-03-14 08:24:59 +11:00
|
|
|
dataPath = os.path.join(addondir,"library")
|
2015-03-15 09:33:16 +11:00
|
|
|
movieLibrary = os.path.join(dataPath,'movies')
|
|
|
|
tvLibrary = os.path.join(dataPath,'tvshows')
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
class LibrarySync():
|
|
|
|
|
|
|
|
def syncDatabase(self):
|
2015-03-15 09:33:16 +11:00
|
|
|
|
2015-03-19 15:38:00 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
2015-03-15 09:33:16 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-16 13:32:45 +11:00
|
|
|
pDialog = None
|
2015-03-19 02:47:55 +11:00
|
|
|
|
|
|
|
#set some variable to check if this is the first run
|
|
|
|
startupDone = False
|
|
|
|
startupStr = WINDOW.getProperty("startup")
|
|
|
|
if startupStr == "done":
|
|
|
|
startupDone = True
|
|
|
|
|
|
|
|
#are we running startup sync or background sync ?
|
|
|
|
if not startupDone:
|
|
|
|
syncOption = addon.getSetting("syncSettingStartup")
|
|
|
|
else:
|
|
|
|
syncOption = addon.getSetting("syncSettingBackground")
|
|
|
|
|
|
|
|
#what sync method to perform ?
|
|
|
|
if syncOption == "Full Sync":
|
2015-03-20 10:48:59 +11:00
|
|
|
|
2015-03-20 11:21:43 +11:00
|
|
|
#pr = utils.startProfiling()
|
2015-03-19 02:47:55 +11:00
|
|
|
self.MoviesSync(True)
|
2015-03-20 11:21:43 +11:00
|
|
|
#utils.stopProfiling(pr, "MoviesSync(True)")
|
2015-03-20 10:48:59 +11:00
|
|
|
|
2015-03-20 11:21:43 +11:00
|
|
|
#pr = utils.startProfiling()
|
2015-03-19 04:00:38 +11:00
|
|
|
self.TvShowsSync(True)
|
2015-03-20 11:21:43 +11:00
|
|
|
#utils.stopProfiling(pr, "TvShowsSync(True)")
|
|
|
|
|
2015-03-22 00:31:30 +11:00
|
|
|
#pr = utils.startProfiling()
|
|
|
|
self.MusicVideosSync(True)
|
|
|
|
#utils.stopProfiling(pr, "MusicVideosSync(True)")
|
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
if syncOption == "Incremental Sync":
|
|
|
|
self.MoviesSync(False)
|
2015-03-19 04:00:38 +11:00
|
|
|
self.TvShowsSync(False)
|
2015-03-22 00:31:30 +11:00
|
|
|
self.MusicVideosSync(False)
|
2015-03-19 02:47:55 +11:00
|
|
|
|
|
|
|
WINDOW.setProperty("startup", "done")
|
|
|
|
|
2015-03-19 07:34:52 +11:00
|
|
|
return True
|
2015-03-19 02:47:55 +11:00
|
|
|
|
|
|
|
def MoviesSync(self, fullsync=True):
|
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
pDialog = None
|
2015-03-24 10:02:46 +11:00
|
|
|
startedSync = datetime.today()
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
try:
|
2015-03-25 18:47:22 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
2015-03-23 14:54:07 +11:00
|
|
|
dbSyncIndication = addon.getSetting("dbSyncIndication")
|
2015-03-25 18:47:22 +11:00
|
|
|
dbSyncFirstRun = addon.getSetting("SyncFirstMovieRunDone")
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(dbSyncFirstRun != "true" or dbSyncIndication == "Dialog Progress"):
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog = xbmcgui.DialogProgress()
|
2015-03-24 10:02:46 +11:00
|
|
|
elif(dbSyncIndication == "BG Progress"):
|
2015-03-16 18:43:20 +11:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
|
|
|
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsAdded = 0
|
|
|
|
totalItemsUpdated = 0
|
|
|
|
totalItemsDeleted = 0
|
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
allEmbyMovieIds = list()
|
2015-03-17 10:04:29 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
2015-03-19 15:38:00 +11:00
|
|
|
viewCount = len(views)
|
|
|
|
viewCurrent = 1
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = ""
|
2015-03-19 15:38:00 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
for view in views:
|
2015-03-17 10:04:29 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
updateNeeded = False
|
2015-03-17 10:04:29 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
#process new movies
|
|
|
|
allMB3Movies = ReadEmbyDB().getMovies(view.get('id'), True, fullsync)
|
|
|
|
allKodiIds = set(ReadKodiDB().getKodiMoviesIds(True))
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-17 01:29:31 +11:00
|
|
|
return True
|
2015-03-19 02:47:55 +11:00
|
|
|
|
|
|
|
if(allMB3Movies == None):
|
|
|
|
return False
|
|
|
|
|
2015-03-17 10:04:29 +11:00
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount)
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 02:47:55 +11:00
|
|
|
total = len(allMB3Movies) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for item in allMB3Movies:
|
2015-03-17 10:04:29 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
allEmbyMovieIds.append(item["Id"])
|
|
|
|
item['Tag'] = []
|
|
|
|
item['Tag'].append(view.get('title'))
|
|
|
|
|
|
|
|
if item["Id"] not in allKodiIds:
|
|
|
|
WriteKodiDB().addMovieToKodiLibrary(item)
|
2015-03-17 01:29:31 +11:00
|
|
|
updateNeeded = True
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsAdded += 1
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 02:47:55 +11:00
|
|
|
return True
|
|
|
|
|
2015-03-17 01:29:31 +11:00
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Adding Movie: " + str(count))
|
2015-03-19 02:47:55 +11:00
|
|
|
count += 1
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
#initiate library update and wait for finish before processing any updates
|
|
|
|
if updateNeeded:
|
2015-03-19 15:38:00 +11:00
|
|
|
if(pDialog != None):
|
2015-03-21 14:15:34 +11:00
|
|
|
pDialog.update(0, "Processing New Items", "Importing STRM Files")
|
2015-03-20 19:15:06 +11:00
|
|
|
|
|
|
|
if(pDialog != None and type(pDialog) == xbmcgui.DialogProgressBG):
|
2015-03-19 15:38:00 +11:00
|
|
|
pDialog.close()
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
self.doKodiLibraryUpdate(False, pDialog)
|
2015-03-19 15:38:00 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(pDialog != None and type(pDialog) == xbmcgui.DialogProgressBG):
|
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 02:47:55 +11:00
|
|
|
return True
|
2015-03-19 15:38:00 +11:00
|
|
|
|
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount)
|
|
|
|
pDialog.update(0, progressTitle, "")
|
2015-03-19 15:38:00 +11:00
|
|
|
total = len(allMB3Movies) + 1
|
|
|
|
count = 1
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-03-25 03:47:26 +11:00
|
|
|
# process box sets - TODO cope with movies removed from a set
|
|
|
|
boxsets = ReadEmbyDB().getBoxSets()
|
|
|
|
for boxset in boxsets:
|
|
|
|
boxsetMovies = ReadEmbyDB().getMoviesInBoxSet(boxset["Id"])
|
|
|
|
WriteKodiDB().addBoxsetToKodiLibrary(boxset)
|
|
|
|
for boxsetMovie in boxsetMovies:
|
|
|
|
WriteKodiDB().updateBoxsetToKodiLibrary(boxsetMovie,boxset)
|
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
#process updates
|
|
|
|
allKodiMovies = ReadKodiDB().getKodiMovies(True)
|
|
|
|
for item in allMB3Movies:
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
item['Tag'] = []
|
|
|
|
item['Tag'].append(view.get('title'))
|
|
|
|
|
2015-03-20 14:04:54 +11:00
|
|
|
kodimovie = allKodiMovies.get(item["Id"], None)
|
|
|
|
if(kodimovie != None):
|
2015-03-21 10:33:04 +11:00
|
|
|
#WriteKodiDB().updateMovieToKodiLibrary(item, kodimovie)
|
2015-03-23 14:54:07 +11:00
|
|
|
updated = WriteKodiDB().updateMovieToKodiLibrary_Batched(item, kodimovie)
|
|
|
|
if(updated):
|
|
|
|
totalItemsUpdated += 1
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 02:47:55 +11:00
|
|
|
return True
|
|
|
|
|
2015-03-17 10:04:29 +11:00
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Updating Movie: " + str(count))
|
2015-03-19 02:47:55 +11:00
|
|
|
count += 1
|
2015-03-19 15:38:00 +11:00
|
|
|
|
|
|
|
viewCurrent += 1
|
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Removing Deleted Items"
|
|
|
|
pDialog.update(0, progressTitle, "")
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-16 18:43:20 +11:00
|
|
|
return True
|
2015-03-16 14:10:41 +11:00
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
cleanNeeded = False
|
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
# process any deletes only at fullsync
|
|
|
|
if fullsync:
|
|
|
|
allKodiIds = ReadKodiDB().getKodiMoviesIds(True)
|
|
|
|
allEmbyMovieIds = set(allEmbyMovieIds)
|
|
|
|
for kodiId in allKodiIds:
|
|
|
|
if not kodiId in allEmbyMovieIds:
|
2015-03-22 00:31:30 +11:00
|
|
|
WriteKodiDB().deleteMovieFromKodiLibrary(kodiId)
|
2015-03-19 02:47:55 +11:00
|
|
|
cleanNeeded = True
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsDeleted += 1
|
2015-03-16 14:10:41 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 02:47:55 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
#initiate library clean and wait for finish before processing any updates
|
|
|
|
if cleanNeeded:
|
2015-03-20 19:15:06 +11:00
|
|
|
self.doKodiLibraryUpdate(True, pDialog)
|
2015-03-16 04:04:01 +11:00
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(dbSyncFirstRun != "true"):
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync') #force a new instance of the addon
|
|
|
|
addon.setSetting("SyncFirstMovieRunDone", "true")
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-24 10:02:46 +11:00
|
|
|
# display notification if set up
|
|
|
|
notificationString = ""
|
|
|
|
if(totalItemsAdded > 0):
|
|
|
|
notificationString += "Added:" + str(totalItemsAdded) + " "
|
|
|
|
if(totalItemsUpdated > 0):
|
|
|
|
notificationString += "Updated:" + str(totalItemsUpdated) + " "
|
|
|
|
if(totalItemsDeleted > 0):
|
|
|
|
notificationString += "Deleted:" + str(totalItemsDeleted) + " "
|
|
|
|
|
|
|
|
timeTaken = datetime.today() - startedSync
|
|
|
|
timeTakenString = str(int(timeTaken.seconds / 60)) + ":" + str(timeTaken.seconds % 60)
|
|
|
|
utils.logMsg("Sync Movies", "Finished " + timeTakenString + " " + notificationString, 0)
|
|
|
|
|
|
|
|
if(dbSyncIndication == "Notify OnChange" and notificationString != ""):
|
|
|
|
notificationString = "(" + timeTakenString + ") " + notificationString
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(Movie Sync: " + notificationString + ",)")
|
|
|
|
elif(dbSyncIndication == "Notify OnFinish"):
|
2015-03-23 14:54:07 +11:00
|
|
|
if(notificationString == ""):
|
|
|
|
notificationString = "Done"
|
2015-03-24 10:02:46 +11:00
|
|
|
notificationString = "(" + timeTakenString + ") " + notificationString
|
2015-03-23 14:54:07 +11:00
|
|
|
xbmc.executebuiltin("XBMC.Notification(Movie Sync: " + notificationString + ",)")
|
2015-03-24 10:02:46 +11:00
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
finally:
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
2015-03-15 09:33:16 +11:00
|
|
|
|
|
|
|
return True
|
2015-03-19 04:00:38 +11:00
|
|
|
|
|
|
|
def TvShowsSync(self, fullsync=True):
|
|
|
|
|
2015-03-19 15:38:00 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
2015-03-19 04:00:38 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
pDialog = None
|
2015-03-24 10:02:46 +11:00
|
|
|
startedSync = datetime.today()
|
2015-03-19 04:00:38 +11:00
|
|
|
|
|
|
|
try:
|
2015-03-23 14:54:07 +11:00
|
|
|
dbSyncIndication = addon.getSetting("dbSyncIndication")
|
2015-03-25 18:47:22 +11:00
|
|
|
dbSyncFirstRun = addon.getSetting("SyncFirstTVRunDone")
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(dbSyncFirstRun != "true" or dbSyncIndication == "Dialog Progress"):
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog = xbmcgui.DialogProgress()
|
2015-03-24 10:02:46 +11:00
|
|
|
elif(dbSyncIndication == "BG Progress"):
|
2015-03-19 04:00:38 +11:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
2015-03-23 14:54:07 +11:00
|
|
|
|
|
|
|
totalItemsAdded = 0
|
|
|
|
totalItemsUpdated = 0
|
|
|
|
totalItemsDeleted = 0
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing Episodes"
|
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
# incremental sync --> new episodes only
|
|
|
|
if not fullsync:
|
2015-03-19 04:43:57 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
latestMBEpisodes = ReadEmbyDB().getLatestEpisodes(True)
|
2015-03-19 04:43:57 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
if latestMBEpisodes != None:
|
|
|
|
allKodiTvShowsIds = set(ReadKodiDB().getKodiTvShowsIds(True))
|
|
|
|
|
|
|
|
updateNeeded = False
|
|
|
|
|
2015-03-19 15:38:00 +11:00
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 15:38:00 +11:00
|
|
|
total = len(latestMBEpisodes) + 1
|
|
|
|
count = 1
|
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
# process new episodes
|
2015-03-19 19:44:25 +11:00
|
|
|
for episode in latestMBEpisodes:
|
|
|
|
if episode["SeriesId"] in allKodiTvShowsIds:
|
2015-03-19 08:38:02 +11:00
|
|
|
#only process tvshows that already exist in the db at incremental updates
|
2015-03-21 06:26:37 +11:00
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(episode["SeriesId"],True,True)
|
2015-03-19 08:38:02 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 08:38:02 +11:00
|
|
|
return True
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
#we have to compare the lists somehow
|
2015-03-19 19:44:25 +11:00
|
|
|
comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber"))
|
2015-03-19 08:38:02 +11:00
|
|
|
matchFound = False
|
|
|
|
if kodiEpisodes != None:
|
2015-03-20 00:18:20 +11:00
|
|
|
KodiItem = kodiEpisodes.get(comparestring1, None)
|
|
|
|
if(KodiItem != None):
|
|
|
|
matchFound = True
|
2015-03-20 19:15:06 +11:00
|
|
|
|
|
|
|
progressAction = "Checking"
|
2015-03-19 08:38:02 +11:00
|
|
|
if not matchFound:
|
|
|
|
#no match so we have to create it
|
2015-03-19 19:44:25 +11:00
|
|
|
WriteKodiDB().addEpisodeToKodiLibrary(episode)
|
2015-03-19 08:38:02 +11:00
|
|
|
updateNeeded = True
|
2015-03-20 19:15:06 +11:00
|
|
|
progressAction = "Adding"
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsAdded += 1
|
2015-03-19 08:38:02 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 08:38:02 +11:00
|
|
|
return True
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, progressAction + " Episode: " + str(count))
|
2015-03-19 08:38:02 +11:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
#initiate library update and wait for finish before processing any updates
|
|
|
|
if updateNeeded:
|
2015-03-20 19:15:06 +11:00
|
|
|
self.doKodiLibraryUpdate(False, pDialog)
|
2015-03-19 08:38:02 +11:00
|
|
|
updateNeeded = False
|
|
|
|
|
|
|
|
#process updates
|
2015-03-19 15:38:00 +11:00
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing Episodes"
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 15:38:00 +11:00
|
|
|
total = len(latestMBEpisodes) + 1
|
|
|
|
count = 1
|
|
|
|
|
2015-03-19 19:44:25 +11:00
|
|
|
for episode in latestMBEpisodes:
|
|
|
|
if episode["SeriesId"] in allKodiTvShowsIds:
|
2015-03-19 08:38:02 +11:00
|
|
|
#only process tvshows that already exist in the db at incremental updates
|
2015-03-21 06:26:37 +11:00
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(episode["SeriesId"],True,True)
|
2015-03-19 08:38:02 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 08:38:02 +11:00
|
|
|
return True
|
2015-03-19 04:43:57 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
#we have to compare the lists somehow
|
2015-03-19 19:44:25 +11:00
|
|
|
comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber"))
|
2015-03-19 15:38:00 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
if kodiEpisodes != None:
|
2015-03-20 00:18:20 +11:00
|
|
|
KodiItem = kodiEpisodes.get(comparestring1, None)
|
|
|
|
if(KodiItem != None):
|
|
|
|
WriteKodiDB().updateEpisodeToKodiLibrary(episode, KodiItem)
|
2015-03-19 15:38:00 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 08:38:02 +11:00
|
|
|
return True
|
2015-03-19 04:43:57 +11:00
|
|
|
|
2015-03-19 08:38:02 +11:00
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Updating Episode: " + str(count))
|
2015-03-19 08:38:02 +11:00
|
|
|
count += 1
|
|
|
|
|
2015-03-19 04:43:57 +11:00
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
# full sync --> Tv shows and Episodes
|
|
|
|
if fullsync:
|
|
|
|
allTVShows = list()
|
2015-03-21 06:26:37 +11:00
|
|
|
allMB3EpisodeIds = list() #for use with deletions
|
|
|
|
allKodiEpisodeIds = [] # for use with deletions
|
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
tvShowData = ReadEmbyDB().getTVShows(True,True)
|
2015-03-20 21:08:22 +11:00
|
|
|
allKodiIds = set(ReadKodiDB().getKodiTvShowsIds(True))
|
2015-03-19 04:49:20 +11:00
|
|
|
updateNeeded = False
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 04:00:38 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
if (tvShowData == None):
|
|
|
|
return
|
|
|
|
|
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing TV Shows"
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 04:00:38 +11:00
|
|
|
total = len(tvShowData) + 1
|
2015-03-19 15:38:00 +11:00
|
|
|
count = 1
|
2015-03-19 04:00:38 +11:00
|
|
|
|
|
|
|
for item in tvShowData:
|
|
|
|
if item.get('IsFolder'):
|
|
|
|
allTVShows.append(item["Id"])
|
|
|
|
progMessage = "Processing"
|
2015-03-20 21:08:22 +11:00
|
|
|
if item["Id"] not in allKodiIds:
|
2015-03-20 22:03:35 +11:00
|
|
|
WriteKodiDB().addTVShowToKodiLibrary(item)
|
2015-03-19 04:00:38 +11:00
|
|
|
updateNeeded = True
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsAdded += 1
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 04:00:38 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 21:08:22 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Adding Tv Show: " + str(count))
|
2015-03-19 04:00:38 +11:00
|
|
|
count += 1
|
|
|
|
|
2015-03-21 00:37:34 +11:00
|
|
|
#process episodes first before updating tvshows
|
2015-03-19 04:00:38 +11:00
|
|
|
allEpisodes = list()
|
|
|
|
|
2015-03-19 15:38:00 +11:00
|
|
|
showTotal = len(allTVShows)
|
|
|
|
showCurrent = 1
|
|
|
|
|
2015-03-19 19:42:25 +11:00
|
|
|
# do episode adds
|
2015-03-19 04:00:38 +11:00
|
|
|
for tvshow in allTVShows:
|
|
|
|
|
|
|
|
episodeData = ReadEmbyDB().getEpisodes(tvshow,True)
|
2015-03-21 06:26:37 +11:00
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(tvshow,True,True)
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-25 23:26:56 +11:00
|
|
|
if episodeData == None:
|
|
|
|
return
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 04:00:38 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing Tv Show " + str(showCurrent) + " of " + str(showTotal)
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 04:00:38 +11:00
|
|
|
total = len(episodeData) + 1
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
#we have to compare the lists somehow
|
|
|
|
for item in episodeData:
|
|
|
|
comparestring1 = str(item.get("ParentIndexNumber")) + "-" + str(item.get("IndexNumber"))
|
|
|
|
matchFound = False
|
|
|
|
if kodiEpisodes != None:
|
2015-03-20 00:18:20 +11:00
|
|
|
KodiItem = kodiEpisodes.get(comparestring1, None)
|
|
|
|
if(KodiItem != None):
|
|
|
|
matchFound = True
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
progressAction = "Checking"
|
2015-03-19 04:00:38 +11:00
|
|
|
if not matchFound:
|
|
|
|
#no match so we have to create it
|
2015-03-19 19:44:25 +11:00
|
|
|
WriteKodiDB().addEpisodeToKodiLibrary(item)
|
2015-03-19 04:00:38 +11:00
|
|
|
updateNeeded = True
|
2015-03-20 19:15:06 +11:00
|
|
|
progressAction = "Adding"
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsAdded += 1
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 04:00:38 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, progressAction + " Episode: " + str(count))
|
2015-03-19 15:38:00 +11:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
showCurrent += 1
|
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
#initiate library update and wait for finish before processing any updates
|
|
|
|
if updateNeeded:
|
2015-03-21 14:15:34 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Processing New Items", "Importing STRM Files")
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(pDialog != None and type(pDialog) == xbmcgui.DialogProgressBG):
|
2015-03-19 19:42:25 +11:00
|
|
|
pDialog.close()
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
self.doKodiLibraryUpdate(False, pDialog)
|
2015-03-19 19:42:25 +11:00
|
|
|
updateNeeded = False
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(pDialog != None and type(pDialog) == xbmcgui.DialogProgressBG):
|
2015-03-19 19:42:25 +11:00
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
2015-03-21 14:15:34 +11:00
|
|
|
|
2015-03-21 00:37:34 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Sync DB : Processing TV Shows"
|
|
|
|
pDialog.update(0, progressTitle, "")
|
|
|
|
total = len(allTVShows) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
#process updates at TV Show level
|
|
|
|
allKodiTVShows = ReadKodiDB().getKodiTvShows(True)
|
|
|
|
for item in tvShowData:
|
|
|
|
if item.get('IsFolder'):
|
|
|
|
|
|
|
|
kodishow = allKodiTVShows.get(item["Id"],None)
|
|
|
|
|
|
|
|
if(kodishow != None):
|
2015-03-23 14:54:07 +11:00
|
|
|
updated = WriteKodiDB().updateTVShowToKodiLibrary(item,kodishow)
|
|
|
|
if(updated):
|
|
|
|
totalItemsUpdated += 1
|
2015-03-21 00:37:34 +11:00
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
|
|
|
pDialog.update(percentage, progressTitle, "Updating Tv Show: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
2015-03-19 19:42:25 +11:00
|
|
|
# do episode updates
|
|
|
|
showCurrent = 1
|
|
|
|
for tvshow in allTVShows:
|
|
|
|
|
|
|
|
episodeData = ReadEmbyDB().getEpisodes(tvshow,True)
|
2015-03-21 06:26:37 +11:00
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(tvshow,True,True)
|
2015-03-19 19:42:25 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 19:42:25 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync DB : Processing Tv Show " + str(showCurrent) + " of " + str(showTotal)
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 19:42:25 +11:00
|
|
|
total = len(episodeData) + 1
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
#we have to compare the lists somehow
|
|
|
|
for item in episodeData:
|
2015-03-21 06:26:37 +11:00
|
|
|
|
|
|
|
#add episodeId to the list of all episodes for use later on the deletes
|
|
|
|
allMB3EpisodeIds.append(item["Id"])
|
|
|
|
|
2015-03-19 19:42:25 +11:00
|
|
|
comparestring1 = str(item.get("ParentIndexNumber")) + "-" + str(item.get("IndexNumber"))
|
|
|
|
matchFound = False
|
|
|
|
if kodiEpisodes != None:
|
2015-03-20 00:18:20 +11:00
|
|
|
KodiItem = kodiEpisodes.get(comparestring1, None)
|
|
|
|
if(KodiItem != None):
|
2015-03-23 14:54:07 +11:00
|
|
|
updated = WriteKodiDB().updateEpisodeToKodiLibrary(item, KodiItem)
|
|
|
|
if(updated):
|
|
|
|
totalItemsUpdated += 1
|
2015-03-19 19:42:25 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 19:42:25 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Updating Episode: " + str(count))
|
2015-03-19 19:42:25 +11:00
|
|
|
count += 1
|
2015-03-21 06:26:37 +11:00
|
|
|
|
|
|
|
|
|
|
|
#add all kodi episodes to a list with episodes for use later on to delete episodes
|
|
|
|
#the mediabrowser ID is set as uniqueID in the NFO... for some reason this has key 'unknown' in the json response
|
2015-03-21 07:42:12 +11:00
|
|
|
show = ReadKodiDB().getKodiEpisodes(tvshow,False,False)
|
|
|
|
if show != None:
|
|
|
|
for episode in show:
|
2015-03-21 22:33:59 +11:00
|
|
|
dict = {'episodeid': str(episode["uniqueid"]["unknown"]),'tvshowid': tvshow}
|
2015-03-21 07:42:12 +11:00
|
|
|
allKodiEpisodeIds.append(dict)
|
2015-03-21 06:26:37 +11:00
|
|
|
|
2015-03-19 19:42:25 +11:00
|
|
|
showCurrent += 1
|
2015-03-19 04:00:38 +11:00
|
|
|
|
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Removing Deleted Items"
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 04:00:38 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
cleanNeeded = False
|
|
|
|
|
2015-03-21 21:32:21 +11:00
|
|
|
# DELETES -- EPISODES
|
2015-03-19 04:00:38 +11:00
|
|
|
# process any deletes only at fullsync
|
2015-03-21 06:26:37 +11:00
|
|
|
allMB3EpisodeIds = set(allMB3EpisodeIds)
|
|
|
|
for episode in allKodiEpisodeIds:
|
2015-03-21 22:33:59 +11:00
|
|
|
if episode.get('episodeid') not in allMB3EpisodeIds:
|
|
|
|
WriteKodiDB().deleteEpisodeFromKodiLibrary(episode.get('episodeid'),episode.get('tvshowid'))
|
2015-03-23 14:54:07 +11:00
|
|
|
cleanneeded = True
|
|
|
|
totalItemsDeleted += 1
|
2015-03-21 06:26:37 +11:00
|
|
|
|
2015-03-21 21:32:21 +11:00
|
|
|
# DELETES -- TV SHOWS
|
2015-03-19 04:00:38 +11:00
|
|
|
if fullsync:
|
|
|
|
allLocaldirs, filesTVShows = xbmcvfs.listdir(tvLibrary)
|
|
|
|
allMB3TVShows = set(allTVShows)
|
|
|
|
for dir in allLocaldirs:
|
|
|
|
if not dir in allMB3TVShows:
|
|
|
|
WriteKodiDB().deleteTVShowFromKodiLibrary(dir)
|
|
|
|
cleanneeded = True
|
2015-03-23 14:54:07 +11:00
|
|
|
totalItemsDeleted += 1
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-19 04:00:38 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
#initiate library clean and wait for finish before processing any updates
|
|
|
|
if cleanNeeded:
|
2015-03-20 19:15:06 +11:00
|
|
|
self.doKodiLibraryUpdate(True, pDialog)
|
2015-03-19 19:42:25 +11:00
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(dbSyncFirstRun != "true"):
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync') #force a new instance of the addon
|
|
|
|
addon.setSetting("SyncFirstTVRunDone", "true")
|
2015-03-24 10:02:46 +11:00
|
|
|
|
|
|
|
# display notification if set up
|
|
|
|
notificationString = ""
|
|
|
|
if(totalItemsAdded > 0):
|
|
|
|
notificationString += "Added:" + str(totalItemsAdded) + " "
|
|
|
|
if(totalItemsUpdated > 0):
|
|
|
|
notificationString += "Updated:" + str(totalItemsUpdated) + " "
|
|
|
|
if(totalItemsDeleted > 0):
|
|
|
|
notificationString += "Deleted:" + str(totalItemsDeleted) + " "
|
|
|
|
|
|
|
|
timeTaken = datetime.today() - startedSync
|
|
|
|
timeTakenString = str(int(timeTaken.seconds / 60)) + ":" + str(timeTaken.seconds % 60)
|
|
|
|
utils.logMsg("Sync Episodes", "Finished " + timeTakenString + " " + notificationString, 0)
|
|
|
|
|
|
|
|
if(dbSyncIndication == "Notify OnChange" and notificationString != ""):
|
|
|
|
notificationString = "(" + timeTakenString + ") " + notificationString
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(Episode Sync: " + notificationString + ",)")
|
|
|
|
elif(dbSyncIndication == "Notify OnFinish"):
|
2015-03-23 14:54:07 +11:00
|
|
|
if(notificationString == ""):
|
|
|
|
notificationString = "Done"
|
2015-03-24 10:02:46 +11:00
|
|
|
notificationString = "(" + timeTakenString + ") " + notificationString
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(Episode Sync: " + notificationString + ",)")
|
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
finally:
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-03-22 00:31:30 +11:00
|
|
|
def MusicVideosSync(self, fullsync=True):
|
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
pDialog = None
|
|
|
|
|
|
|
|
try:
|
2015-03-23 14:54:07 +11:00
|
|
|
dbSyncIndication = addon.getSetting("dbSyncIndication")
|
2015-03-25 18:47:22 +11:00
|
|
|
dbSyncFirstRun = addon.getSetting("SyncFirstMusicVideoRunDone")
|
2015-03-22 00:31:30 +11:00
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(dbSyncFirstRun != "true" or dbSyncIndication == "Dialog Progress"):
|
2015-03-22 00:31:30 +11:00
|
|
|
pDialog = xbmcgui.DialogProgress()
|
2015-03-24 10:02:46 +11:00
|
|
|
elif(dbSyncIndication == "BG Progress"):
|
2015-03-22 00:31:30 +11:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
|
|
|
|
|
|
|
allEmbyMusicVideoIds = list()
|
|
|
|
|
|
|
|
progressTitle = ""
|
|
|
|
|
|
|
|
updateNeeded = False
|
|
|
|
|
|
|
|
#process new musicvideos
|
|
|
|
allMB3MusicVideos = ReadEmbyDB().getMusicVideos(True, fullsync)
|
|
|
|
allKodiIds = set(ReadKodiDB().getKodiMusicVideoIds(True))
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if(allMB3MusicVideos == None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Sync DB : Processing Musicvideos"
|
|
|
|
pDialog.update(0, progressTitle)
|
|
|
|
total = len(allMB3MusicVideos) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for item in allMB3MusicVideos:
|
|
|
|
|
|
|
|
if not item.get('IsFolder'):
|
|
|
|
allEmbyMusicVideoIds.append(item["Id"])
|
|
|
|
|
|
|
|
if item["Id"] not in allKodiIds:
|
|
|
|
WriteKodiDB().addMusicVideoToKodiLibrary(item)
|
|
|
|
updateNeeded = True
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
|
|
|
pDialog.update(percentage, progressTitle, "Adding Musicvideo: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
#initiate library update and wait for finish before processing any updates
|
|
|
|
if updateNeeded:
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Processing New Items", "Importing STRM Files")
|
|
|
|
|
|
|
|
if(pDialog != None and type(pDialog) == xbmcgui.DialogProgressBG):
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
self.doKodiLibraryUpdate(False, pDialog)
|
|
|
|
|
|
|
|
if(pDialog != None and type(pDialog) == xbmcgui.DialogProgressBG):
|
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Sync DB : Processing musicvideos"
|
|
|
|
pDialog.update(0, progressTitle, "")
|
|
|
|
total = len(allMB3MusicVideos) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
#process updates
|
|
|
|
allKodiMusicVideos = ReadKodiDB().getKodiMusicVideos(True)
|
|
|
|
for item in allMB3MusicVideos:
|
|
|
|
|
|
|
|
if not item.get('IsFolder'):
|
|
|
|
|
|
|
|
kodimusicvideo = allKodiMusicVideos.get(item["Id"], None)
|
|
|
|
if(kodimusicvideo != None):
|
|
|
|
#WriteKodiDB().updateMusicVideoToKodiLibrary(item, kodimusicvideo)
|
|
|
|
WriteKodiDB().updateMusicVideoToKodiLibrary_Batched(item, kodimusicvideo)
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
|
|
|
pDialog.update(percentage, progressTitle, "Updating MusicVideo: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Removing Deleted Items"
|
|
|
|
pDialog.update(0, progressTitle, "")
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
cleanNeeded = False
|
|
|
|
|
|
|
|
# process any deletes only at fullsync
|
|
|
|
if fullsync:
|
|
|
|
allKodiIds = ReadKodiDB().getKodiMusicVideoIds(True)
|
|
|
|
allEmbyMusicVideoIds = set(allEmbyMusicVideoIds)
|
|
|
|
for kodiId in allKodiIds:
|
|
|
|
if not kodiId in allEmbyMusicVideoIds:
|
|
|
|
WriteKodiDB().deleteMusicVideoFromKodiLibrary(kodiId)
|
|
|
|
cleanNeeded = True
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return True
|
|
|
|
|
|
|
|
#initiate library clean and wait for finish before processing any updates
|
|
|
|
if cleanNeeded:
|
|
|
|
self.doKodiLibraryUpdate(True, pDialog)
|
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(dbSyncFirstRun != "true"):
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
addon.setSetting("SyncFirstMusicVideoRunDone", "true")
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
finally:
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
def doKodiLibraryUpdate(self, clean, prog):
|
2015-03-19 02:47:55 +11:00
|
|
|
#initiate library update and wait for finish before processing any updates
|
|
|
|
if clean:
|
|
|
|
xbmc.executebuiltin("CleanLibrary(video)")
|
|
|
|
else:
|
|
|
|
xbmc.executebuiltin("UpdateLibrary(video)")
|
|
|
|
xbmc.sleep(1000)
|
|
|
|
while (xbmc.getCondVisibility("Library.IsScanningVideo")):
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(prog)):
|
2015-03-19 02:47:55 +11:00
|
|
|
return True
|
2015-03-20 19:15:06 +11:00
|
|
|
xbmc.sleep(1000)
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
def updatePlayCounts(self):
|
|
|
|
#update all playcounts from MB3 to Kodi library
|
|
|
|
|
2015-03-19 15:38:00 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
2015-03-15 09:33:16 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-16 13:32:45 +11:00
|
|
|
pDialog = None
|
2015-03-24 10:02:46 +11:00
|
|
|
startedSync = datetime.today()
|
2015-03-19 15:38:00 +11:00
|
|
|
processMovies = True
|
|
|
|
processTvShows = True
|
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
try:
|
2015-03-23 14:54:07 +11:00
|
|
|
playCountSyncIndication = addon.getSetting("playCountSyncIndication")
|
2015-03-25 18:47:22 +11:00
|
|
|
playCountSyncFirstRun = addon.getSetting("SyncFirstCountsRunDone")
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-25 18:47:22 +11:00
|
|
|
if(playCountSyncFirstRun != "true" or playCountSyncIndication == "Dialog Progress"):
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog = xbmcgui.DialogProgress()
|
2015-03-24 10:02:46 +11:00
|
|
|
elif(playCountSyncIndication == "BG Progress"):
|
2015-03-16 18:43:20 +11:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.create('Sync PlayCounts', 'Sync PlayCounts')
|
2015-03-16 04:04:01 +11:00
|
|
|
|
2015-03-23 14:54:07 +11:00
|
|
|
totalCountsUpdated = 0
|
|
|
|
totalPositionsUpdated = 0
|
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
#process movies
|
2015-03-17 10:04:29 +11:00
|
|
|
if processMovies:
|
2015-03-20 19:15:06 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Processing Movies", "")
|
|
|
|
|
2015-03-18 04:51:45 +11:00
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
2015-03-19 19:42:25 +11:00
|
|
|
viewCount = len(views)
|
|
|
|
viewCurrent = 1
|
2015-03-17 10:04:29 +11:00
|
|
|
for view in views:
|
2015-03-20 00:28:55 +11:00
|
|
|
allMB3Movies = ReadEmbyDB().getMovies(view.get('id'), fullinfo = False, fullSync = True)
|
2015-03-19 20:06:05 +11:00
|
|
|
allKodiMovies = ReadKodiDB().getKodiMovies(False)
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-17 10:04:29 +11:00
|
|
|
return True
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
if(allMB3Movies == None):
|
2015-03-17 10:04:29 +11:00
|
|
|
return False
|
2015-03-19 20:06:05 +11:00
|
|
|
|
|
|
|
if(allKodiMovies == None):
|
|
|
|
return False
|
|
|
|
|
2015-03-16 18:43:20 +11:00
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync PlayCounts: Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount)
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-19 02:47:55 +11:00
|
|
|
totalCount = len(allMB3Movies) + 1
|
2015-03-23 14:54:07 +11:00
|
|
|
count = 1
|
2015-03-16 18:43:20 +11:00
|
|
|
|
2015-03-19 02:47:55 +11:00
|
|
|
for item in allMB3Movies:
|
2015-03-19 20:52:21 +11:00
|
|
|
|
2015-03-20 14:04:54 +11:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
kodiItem = allKodiMovies.get(item["Id"], None)
|
2015-03-19 20:06:05 +11:00
|
|
|
|
2015-03-20 14:04:54 +11:00
|
|
|
userData = API().getUserData(item)
|
2015-03-17 10:04:29 +11:00
|
|
|
timeInfo = API().getTimeInfo(item)
|
2015-03-20 14:04:54 +11:00
|
|
|
|
2015-03-17 10:04:29 +11:00
|
|
|
if kodiItem != None:
|
|
|
|
kodiresume = int(round(kodiItem['resume'].get("position")))
|
|
|
|
resume = int(round(float(timeInfo.get("ResumeTime"))))*60
|
|
|
|
total = int(round(float(timeInfo.get("TotalTime"))))*60
|
|
|
|
if kodiresume != resume:
|
2015-03-18 06:02:42 +11:00
|
|
|
WriteKodiDB().setKodiResumePoint(kodiItem['movieid'],resume,total,"movie")
|
2015-03-23 14:54:07 +11:00
|
|
|
totalPositionsUpdated += 1
|
2015-03-25 07:29:57 +11:00
|
|
|
updated = WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")), "movie")
|
|
|
|
updated = WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "movie")
|
|
|
|
if(updated):
|
|
|
|
totalCountsUpdated += 1
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-17 10:04:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(totalCount)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Updating Movie: " + str(count))
|
2015-03-19 19:42:25 +11:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
viewCurrent += 1
|
|
|
|
|
2015-03-17 10:04:29 +11:00
|
|
|
#process Tv shows
|
|
|
|
if processTvShows:
|
2015-03-20 19:15:06 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Processing TV Episodes", "")
|
|
|
|
|
2015-03-20 00:28:55 +11:00
|
|
|
tvshowData = ReadEmbyDB().getTVShows(fullinfo = False, fullSync = True)
|
2015-03-17 10:04:29 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-17 10:04:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
if (tvshowData == None):
|
2015-03-19 19:42:25 +11:00
|
|
|
return False
|
|
|
|
|
|
|
|
showTotal = len(tvshowData)
|
|
|
|
showCurrent = 1
|
2015-03-17 10:04:29 +11:00
|
|
|
|
|
|
|
for item in tvshowData:
|
2015-03-19 20:52:21 +11:00
|
|
|
|
2015-03-18 04:51:45 +11:00
|
|
|
episodeData = ReadEmbyDB().getEpisodes(item["Id"], False)
|
2015-03-21 06:26:37 +11:00
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(item["Id"],False,True)
|
2015-03-17 10:04:29 +11:00
|
|
|
|
|
|
|
if (episodeData != None):
|
2015-03-16 13:32:45 +11:00
|
|
|
if(pDialog != None):
|
2015-03-20 19:15:06 +11:00
|
|
|
progressTitle = "Sync PlayCounts: Processing TV Show " + str(showCurrent) + " of " + str(showTotal)
|
|
|
|
pDialog.update(0, progressTitle)
|
2015-03-17 10:04:29 +11:00
|
|
|
totalCount = len(episodeData) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for episode in episodeData:
|
2015-03-19 20:52:21 +11:00
|
|
|
|
|
|
|
kodiItem = None
|
|
|
|
comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber"))
|
|
|
|
matchFound = False
|
|
|
|
if kodiEpisodes != None:
|
2015-03-20 00:18:20 +11:00
|
|
|
kodiItem = kodiEpisodes.get(comparestring1, None)
|
2015-03-19 20:52:21 +11:00
|
|
|
|
2015-03-17 10:04:29 +11:00
|
|
|
userData=API().getUserData(episode)
|
|
|
|
timeInfo = API().getTimeInfo(episode)
|
|
|
|
if kodiItem != None:
|
2015-03-24 11:35:00 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
WINDOW.setProperty("episodeid" + str(kodiItem['episodeid']), episode.get('Name') + ";;" + episode.get('Id'))
|
2015-03-17 10:04:29 +11:00
|
|
|
kodiresume = int(round(kodiItem['resume'].get("position")))
|
|
|
|
resume = int(round(float(timeInfo.get("ResumeTime"))))*60
|
|
|
|
total = int(round(float(timeInfo.get("TotalTime"))))*60
|
|
|
|
if kodiresume != resume:
|
2015-03-18 06:02:42 +11:00
|
|
|
WriteKodiDB().setKodiResumePoint(kodiItem['episodeid'],resume,total,"episode")
|
2015-03-23 14:54:07 +11:00
|
|
|
totalPositionsUpdated += 1
|
2015-03-25 07:29:57 +11:00
|
|
|
|
|
|
|
updated = WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"episode")
|
|
|
|
updated = WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "episode")
|
|
|
|
if(updated):
|
|
|
|
totalCountsUpdated += 1
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-17 10:04:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(totalCount)) * 100))
|
2015-03-20 19:15:06 +11:00
|
|
|
pDialog.update(percentage, progressTitle, "Updating Episode: " + str(count))
|
2015-03-19 19:42:25 +11:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
showCurrent += 1
|
2015-03-25 18:47:22 +11:00
|
|
|
|
|
|
|
if(playCountSyncFirstRun != "true"):
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
addon.setSetting("SyncFirstCountsRunDone", "true")
|
2015-03-24 10:02:46 +11:00
|
|
|
|
|
|
|
# display notification if set up
|
|
|
|
notificationString = ""
|
|
|
|
if(totalPositionsUpdated > 0):
|
|
|
|
notificationString += "Pos:" + str(totalPositionsUpdated) + " "
|
|
|
|
if(totalCountsUpdated > 0):
|
|
|
|
notificationString += "Counts:" + str(totalCountsUpdated) + " "
|
|
|
|
|
|
|
|
timeTaken = datetime.today() - startedSync
|
|
|
|
timeTakenString = str(int(timeTaken.seconds / 60)) + ":" + str(timeTaken.seconds % 60)
|
|
|
|
utils.logMsg("Sync PlayCount", "Finished " + timeTakenString + " " + notificationString, 0)
|
|
|
|
|
|
|
|
if(playCountSyncIndication == "Notify OnChange" and notificationString != ""):
|
|
|
|
notificationString = "(" + timeTakenString + ") " + notificationString
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(PlayCount Sync: " + notificationString + ",)")
|
|
|
|
elif(playCountSyncIndication == "Notify OnFinish"):
|
2015-03-23 14:54:07 +11:00
|
|
|
if(notificationString == ""):
|
|
|
|
notificationString = "Done"
|
2015-03-24 10:02:46 +11:00
|
|
|
notificationString = "(" + timeTakenString + ") " + notificationString
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(PlayCount Sync: " + notificationString + ",)")
|
|
|
|
|
2015-03-16 13:32:45 +11:00
|
|
|
finally:
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
2015-03-15 09:33:16 +11:00
|
|
|
|
|
|
|
return True
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-03-20 04:40:29 +11:00
|
|
|
def updatePlayCount(self,itemID,type):
|
|
|
|
#update playcount of the itemID from MB3 to Kodi library
|
|
|
|
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
#process movie
|
|
|
|
if type=='Movie':
|
|
|
|
MB3Movie = ReadEmbyDB().getItem(itemID)
|
2015-03-20 10:34:56 +11:00
|
|
|
kodiItem = ReadKodiDB().getKodiMovie(itemID)
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-20 04:40:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
if(MB3Movie == None):
|
|
|
|
return False
|
|
|
|
|
2015-03-20 10:24:29 +11:00
|
|
|
if(kodiItem == None):
|
2015-03-20 04:40:29 +11:00
|
|
|
return False
|
|
|
|
|
|
|
|
userData=API().getUserData(MB3Movie)
|
|
|
|
timeInfo = API().getTimeInfo(MB3Movie)
|
|
|
|
if kodiItem != None:
|
2015-03-20 10:24:29 +11:00
|
|
|
|
2015-03-20 04:40:29 +11:00
|
|
|
kodiresume = int(round(kodiItem['resume'].get("position")))
|
|
|
|
resume = int(round(float(timeInfo.get("ResumeTime"))))*60
|
|
|
|
total = int(round(float(timeInfo.get("TotalTime"))))*60
|
|
|
|
if kodiresume != resume:
|
|
|
|
WriteKodiDB().setKodiResumePoint(kodiItem['movieid'],resume,total,"movie")
|
2015-03-20 10:24:29 +11:00
|
|
|
#write property forced will refresh the item in the list so playcount change is immediately visible
|
|
|
|
WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"movie",True)
|
2015-03-25 07:03:07 +11:00
|
|
|
WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "movie")
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-20 04:40:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
#process episode
|
|
|
|
elif type=='Episode':
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-20 04:40:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
MB3Episode = ReadEmbyDB().getItem(itemID)
|
2015-03-21 22:33:59 +11:00
|
|
|
kodiItem = ReadKodiDB().getKodiEpisodeByMbItem(MB3Episode["Id"], MB3Episode["SeriesId"])
|
2015-03-20 04:40:29 +11:00
|
|
|
if (MB3Episode != None):
|
|
|
|
userData=API().getUserData(MB3Episode)
|
|
|
|
timeInfo = API().getTimeInfo(MB3Episode)
|
|
|
|
if kodiItem != None:
|
|
|
|
kodiresume = int(round(kodiItem['resume'].get("position")))
|
|
|
|
resume = int(round(float(timeInfo.get("ResumeTime"))))*60
|
|
|
|
total = int(round(float(timeInfo.get("TotalTime"))))*60
|
|
|
|
if kodiresume != resume:
|
|
|
|
WriteKodiDB().setKodiResumePoint(kodiItem['episodeid'],resume,total,"episode")
|
2015-03-20 10:24:29 +11:00
|
|
|
#write property forced will refresh the item in the list so playcount change is immediately visible
|
2015-03-25 07:03:07 +11:00
|
|
|
WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"episode",True)
|
|
|
|
WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "episode")
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-20 04:40:29 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
def ShouldStop(self, prog):
|
|
|
|
|
|
|
|
if(prog != None and type(prog) == xbmcgui.DialogProgress):
|
|
|
|
if(prog.iscanceled() == True):
|
|
|
|
return True
|
|
|
|
|
2015-03-16 14:10:41 +11:00
|
|
|
if(xbmc.Player().isPlaying() or xbmc.abortRequested):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2015-03-17 04:51:49 +11:00
|
|
|
|
2015-03-16 04:14:23 +11:00
|
|
|
|
|
|
|
|
|
|
|
|