jellyfin-kodi/resources/lib/LibrarySync.py

633 lines
27 KiB
Python
Raw Normal View History

2015-03-13 21:24:59 +00:00
#################################################################################################
# LibrarySync
#################################################################################################
import xbmc
import xbmcgui
import xbmcaddon
import xbmcvfs
import json
import sqlite3
2015-03-16 17:51:49 +00:00
import inspect
2015-03-13 21:24:59 +00:00
import threading
import urllib
from datetime import datetime, timedelta, time
import urllib2
import os
2015-03-13 21:24:59 +00:00
from API import API
import Utils as utils
from DownloadUtils import DownloadUtils
2015-03-17 17:51:45 +00:00
from ReadEmbyDB import ReadEmbyDB
2015-03-17 18:41:26 +00:00
from ReadKodiDB import ReadKodiDB
2015-03-17 19:02:42 +00:00
from WriteKodiDB import WriteKodiDB
2015-03-17 18:18:17 +00:00
from CreateFiles import CreateFiles
2015-03-13 21:24:59 +00:00
2015-03-19 04:38:00 +00:00
addondir = xbmc.translatePath(xbmcaddon.Addon(id='plugin.video.mb3sync').getAddonInfo('profile'))
2015-03-13 21:24:59 +00:00
dataPath = os.path.join(addondir,"library")
movieLibrary = os.path.join(dataPath,'movies')
tvLibrary = os.path.join(dataPath,'tvshows')
2015-03-13 21:24:59 +00:00
class LibrarySync():
def syncDatabase(self):
2015-03-19 04:38:00 +00:00
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
WINDOW = xbmcgui.Window( 10000 )
pDialog = None
#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":
self.MoviesSync(True)
self.TvShowsSync(True)
if syncOption == "Incremental Sync":
self.MoviesSync(False)
self.TvShowsSync(False)
WINDOW.setProperty("startup", "done")
return True
def MoviesSync(self, fullsync=True):
2015-03-19 04:38:00 +00:00
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
WINDOW = xbmcgui.Window( 10000 )
pDialog = None
2015-03-13 21:24:59 +00:00
try:
enableProgress = False
if addon.getSetting("enableProgressFullSync") == 'true':
enableProgress = True
2015-03-19 04:38:00 +00:00
startupStr = WINDOW.getProperty("startup")
if startupStr != "done":
enableProgress = True
if(enableProgress):
2015-03-16 07:43:20 +00:00
pDialog = xbmcgui.DialogProgressBG()
if(pDialog != None):
pDialog.create('Sync DB', 'Sync DB')
allEmbyMovieIds = list()
views = ReadEmbyDB().getCollections("movies")
2015-03-19 04:38:00 +00:00
viewCount = len(views)
viewCurrent = 1
for view in views:
updateNeeded = False
#process new movies
allMB3Movies = ReadEmbyDB().getMovies(view.get('id'), True, fullsync)
allKodiIds = set(ReadKodiDB().getKodiMoviesIds(True))
2015-03-16 14:29:31 +00:00
if(self.ShouldStop()):
return True
if(allMB3Movies == None):
return False
if(pDialog != None):
2015-03-19 04:38:00 +00:00
pDialog.update(0, "Sync DB : Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount))
total = len(allMB3Movies) + 1
count = 1
for item in allMB3Movies:
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-16 14:29:31 +00:00
updateNeeded = True
2015-03-16 14:29:31 +00:00
if(self.ShouldStop()):
return True
if(self.ShouldStop()):
return True
2015-03-16 14:29:31 +00:00
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
2015-03-19 04:38:00 +00:00
pDialog.update(percentage, message = "Adding Movie: " + str(count))
count += 1
#initiate library update and wait for finish before processing any updates
if updateNeeded:
2015-03-19 04:38:00 +00:00
if(pDialog != None):
pDialog.close()
self.doKodiLibraryUpdate()
if(pDialog != None):
pDialog.create('Sync DB', 'Sync DB')
if(self.ShouldStop()):
return True
2015-03-19 04:38:00 +00:00
if(pDialog != None):
pDialog.update(0, "Sync DB : Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount))
total = len(allMB3Movies) + 1
count = 1
#process updates
allKodiMovies = ReadKodiDB().getKodiMovies(True)
for item in allMB3Movies:
if not item.get('IsFolder'):
item['Tag'] = []
item['Tag'].append(view.get('title'))
for kodimovie in allKodiMovies:
if item["Id"] in kodimovie["file"]:
WriteKodiDB().updateMovieToKodiLibrary(item,kodimovie)
break
if(self.ShouldStop()):
return True
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
2015-03-19 04:38:00 +00:00
pDialog.update(percentage, message="Updating Movie: " + str(count))
count += 1
2015-03-19 04:38:00 +00:00
viewCurrent += 1
if(pDialog != None):
pDialog.update(0, message="Removing Deleted Items")
if(self.ShouldStop()):
2015-03-16 07:43:20 +00:00
return True
cleanNeeded = False
# 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-17 19:02:42 +00:00
WriteKodiDB().deleteMovieFromKodiLibrary(dir)
cleanNeeded = True
if(self.ShouldStop()):
return True
#initiate library clean and wait for finish before processing any updates
if cleanNeeded:
2015-03-19 04:38:00 +00:00
self.doKodiLibraryUpdate(True)
finally:
if(pDialog != None):
pDialog.close()
return True
def TvShowsSync(self, fullsync=True):
2015-03-19 04:38:00 +00:00
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
WINDOW = xbmcgui.Window( 10000 )
pDialog = None
try:
enableProgress = False
if addon.getSetting("enableProgressFullSync") == 'true':
enableProgress = True
2015-03-19 04:38:00 +00:00
startupStr = WINDOW.getProperty("startup")
if startupStr != "done":
enableProgress = True
if(enableProgress):
pDialog = xbmcgui.DialogProgressBG()
if(pDialog != None):
pDialog.create('Sync DB', 'Sync DB')
# incremental sync --> new episodes only
if not fullsync:
2015-03-18 21:38:02 +00:00
latestMBEpisodes = ReadEmbyDB().getLatestEpisodes(True)
2015-03-18 21:38:02 +00:00
if latestMBEpisodes != None:
allKodiTvShowsIds = set(ReadKodiDB().getKodiTvShowsIds(True))
updateNeeded = False
2015-03-19 04:38:00 +00:00
if(pDialog != None):
pDialog.update(0, "Sync DB : Processing Episodes")
total = len(latestMBEpisodes) + 1
count = 1
2015-03-18 21:38:02 +00:00
# process new episodes
2015-03-19 08:44:25 +00:00
for episode in latestMBEpisodes:
if episode["SeriesId"] in allKodiTvShowsIds:
2015-03-18 21:38:02 +00:00
#only process tvshows that already exist in the db at incremental updates
2015-03-19 08:44:25 +00:00
kodiEpisodes = ReadKodiDB().getKodiEpisodes(episode["SeriesId"])
2015-03-18 21:38:02 +00:00
if(self.ShouldStop()):
return True
2015-03-18 21:38:02 +00:00
#we have to compare the lists somehow
2015-03-19 08:44:25 +00:00
comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber"))
2015-03-18 21:38:02 +00:00
matchFound = False
if kodiEpisodes != None:
KodiItem = kodiEpisodes.get(comparestring1, None)
if(KodiItem != None):
matchFound = True
2015-03-18 21:38:02 +00:00
if not matchFound:
#no match so we have to create it
2015-03-19 08:44:25 +00:00
WriteKodiDB().addEpisodeToKodiLibrary(episode)
2015-03-18 21:38:02 +00:00
updateNeeded = True
if(self.ShouldStop()):
return True
2015-03-18 21:38:02 +00:00
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
2015-03-19 04:38:00 +00:00
pDialog.update(percentage, message="Adding Episode: " + str(count))
2015-03-18 21:38:02 +00:00
count += 1
#initiate library update and wait for finish before processing any updates
if updateNeeded:
self.doKodiLibraryUpdate()
updateNeeded = False
#process updates
2015-03-19 04:38:00 +00:00
if(pDialog != None):
pDialog.update(0, "Sync DB : Processing Episodes")
total = len(latestMBEpisodes) + 1
count = 1
2015-03-19 08:44:25 +00:00
for episode in latestMBEpisodes:
if episode["SeriesId"] in allKodiTvShowsIds:
2015-03-18 21:38:02 +00:00
#only process tvshows that already exist in the db at incremental updates
2015-03-19 08:44:25 +00:00
kodiEpisodes = ReadKodiDB().getKodiEpisodes(episode["SeriesId"])
2015-03-18 21:38:02 +00:00
if(self.ShouldStop()):
return True
2015-03-18 21:38:02 +00:00
#we have to compare the lists somehow
2015-03-19 08:44:25 +00:00
comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber"))
2015-03-19 04:38:00 +00:00
2015-03-18 21:38:02 +00:00
if kodiEpisodes != None:
KodiItem = kodiEpisodes.get(comparestring1, None)
if(KodiItem != None):
WriteKodiDB().updateEpisodeToKodiLibrary(episode, KodiItem)
2015-03-19 04:38:00 +00:00
2015-03-18 21:38:02 +00:00
if(self.ShouldStop()):
return True
2015-03-18 21:38:02 +00:00
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
2015-03-19 04:38:00 +00:00
pDialog.update(percentage, message="Updating Episode: " + str(count))
2015-03-18 21:38:02 +00:00
count += 1
# full sync --> Tv shows and Episodes
if fullsync:
allTVShows = list()
allEpisodes = list()
#FIXME --> for now pull all tv shows and use the incremental update only at episode level
tvShowData = ReadEmbyDB().getTVShows(True,True)
2015-03-18 17:49:20 +00:00
updateNeeded = False
if(self.ShouldStop()):
return True
if (tvShowData == None):
return
if(pDialog != None):
pDialog.update(0, "Sync DB : Processing TV Shows")
total = len(tvShowData) + 1
2015-03-19 04:38:00 +00:00
count = 1
for item in tvShowData:
if item.get('IsFolder'):
kodiItem = ReadKodiDB().getKodiTVShow(item["Id"])
allTVShows.append(item["Id"])
progMessage = "Processing"
if kodiItem == None:
WriteKodiDB().addTVShowToKodiLibrary(item)
updateNeeded = True
progMessage = "Adding"
else:
WriteKodiDB().updateTVShowToKodiLibrary(item, kodiItem)
progMessage = "Updating"
if(self.ShouldStop()):
return True
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
pDialog.update(percentage, message=progMessage + " Tv Show: " + str(count))
count += 1
#initiate library update and wait for finish before processing any updates
if updateNeeded:
if(pDialog != None):
pDialog.close()
self.doKodiLibraryUpdate()
updateNeeded = False
if(pDialog != None):
pDialog.create('Sync DB', 'Sync DB')
#process episodes (will only be possible when tv show is scanned to library)
#TODO --> maybe pull full info only when needed ?
allEpisodes = list()
2015-03-19 04:38:00 +00:00
showTotal = len(allTVShows)
showCurrent = 1
# do episode adds
for tvshow in allTVShows:
episodeData = ReadEmbyDB().getEpisodes(tvshow,True)
kodiEpisodes = ReadKodiDB().getKodiEpisodes(tvshow)
if(self.ShouldStop()):
return True
if(pDialog != None):
2015-03-19 04:38:00 +00:00
pDialog.update(0, "Sync DB : Processing Tv Show " + str(showCurrent) + " of " + str(showTotal))
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:
KodiItem = kodiEpisodes.get(comparestring1, None)
if(KodiItem != None):
matchFound = True
if not matchFound:
#no match so we have to create it
print "episode not found...creating it: "
2015-03-19 08:44:25 +00:00
WriteKodiDB().addEpisodeToKodiLibrary(item)
updateNeeded = True
if(self.ShouldStop()):
return True
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
pDialog.update(percentage, message="Adding Episode: " + str(count))
2015-03-19 04:38:00 +00:00
count += 1
showCurrent += 1
#initiate library update and wait for finish before processing any updates
if updateNeeded:
if(pDialog != None):
pDialog.close()
self.doKodiLibraryUpdate()
updateNeeded = False
if(pDialog != None):
pDialog.create('Sync DB', 'Sync DB')
# do episode updates
showCurrent = 1
for tvshow in allTVShows:
episodeData = ReadEmbyDB().getEpisodes(tvshow,True)
kodiEpisodes = ReadKodiDB().getKodiEpisodes(tvshow)
if(self.ShouldStop()):
return True
if(pDialog != None):
pDialog.update(0, "Sync DB : Processing Tv Show " + str(showCurrent) + " of " + str(showTotal))
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:
KodiItem = kodiEpisodes.get(comparestring1, None)
if(KodiItem != None):
WriteKodiDB().updateEpisodeToKodiLibrary(item, KodiItem)
if(self.ShouldStop()):
return True
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(total)) * 100))
pDialog.update(percentage, message="Updating Episode: " + str(count))
count += 1
showCurrent += 1
if(pDialog != None):
pDialog.update(0, message="Removing Deleted Items")
if(self.ShouldStop()):
return True
cleanNeeded = False
# process any deletes only at fullsync
# TODO --> process deletes for episodes !!!
if fullsync:
allLocaldirs, filesTVShows = xbmcvfs.listdir(tvLibrary)
allMB3TVShows = set(allTVShows)
for dir in allLocaldirs:
if not dir in allMB3TVShows:
WriteKodiDB().deleteTVShowFromKodiLibrary(dir)
cleanneeded = True
if(self.ShouldStop()):
return True
#initiate library clean and wait for finish before processing any updates
if cleanNeeded:
2015-03-19 04:38:00 +00:00
self.doKodiLibraryUpdate(True)
finally:
if(pDialog != None):
pDialog.close()
return True
def doKodiLibraryUpdate(self,clean=False):
#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")):
if(self.ShouldStop()):
return True
xbmc.sleep(250)
2015-03-13 21:24:59 +00:00
def updatePlayCounts(self):
#update all playcounts from MB3 to Kodi library
2015-03-19 04:38:00 +00:00
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
WINDOW = xbmcgui.Window( 10000 )
pDialog = None
2015-03-19 04:38:00 +00:00
processMovies = True
processTvShows = True
try:
enableProgress = False
if addon.getSetting("enableProgressPlayCountSync") == 'true':
enableProgress = True
if(enableProgress):
2015-03-16 07:43:20 +00:00
pDialog = xbmcgui.DialogProgressBG()
if(pDialog != None):
pDialog.create('Sync PlayCounts', 'Sync PlayCounts')
#process movies
if processMovies:
2015-03-17 17:51:45 +00:00
views = ReadEmbyDB().getCollections("movies")
viewCount = len(views)
viewCurrent = 1
for view in views:
allMB3Movies = ReadEmbyDB().getMovies(view.get('id'), fullinfo = False, fullSync = True)
allKodiMovies = ReadKodiDB().getKodiMovies(False)
if(self.ShouldStop()):
return True
if(allMB3Movies == None):
return False
if(allKodiMovies == None):
return False
2015-03-16 07:43:20 +00:00
if(pDialog != None):
pDialog.update(0, "Sync PlayCounts: Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount))
totalCount = len(allMB3Movies) + 1
count = 1
2015-03-16 07:43:20 +00:00
for item in allMB3Movies:
2015-03-19 09:52:21 +00:00
if not item.get('IsFolder'):
kodiItem = None
for kodimovie in allKodiMovies:
if item["Id"] in kodimovie["file"]:
kodiItem = kodimovie
break
userData=API().getUserData(item)
timeInfo = API().getTimeInfo(item)
if kodiItem != None:
2015-03-17 19:02:42 +00:00
WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"movie")
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-17 19:02:42 +00:00
WriteKodiDB().setKodiResumePoint(kodiItem['movieid'],resume,total,"movie")
if(self.ShouldStop()):
return True
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(totalCount)) * 100))
2015-03-18 15:52:11 +00:00
pDialog.update(percentage, message="Updating Movie: " + str(count))
count += 1
viewCurrent += 1
#process Tv shows
if processTvShows:
tvshowData = ReadEmbyDB().getTVShows(fullinfo = False, fullSync = True)
if(self.ShouldStop()):
return True
if (tvshowData == None):
return False
showTotal = len(tvshowData)
showCurrent = 1
for item in tvshowData:
2015-03-19 09:52:21 +00:00
2015-03-17 17:51:45 +00:00
episodeData = ReadEmbyDB().getEpisodes(item["Id"], False)
2015-03-19 09:52:21 +00:00
kodiEpisodes = ReadKodiDB().getKodiEpisodes(item["Id"],False)
if (episodeData != None):
if(pDialog != None):
pDialog.update(0, "Sync PlayCounts: Processing TV Show " + str(showCurrent) + " of " + str(showTotal))
totalCount = len(episodeData) + 1
count = 1
for episode in episodeData:
2015-03-19 09:52:21 +00:00
kodiItem = None
comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber"))
matchFound = False
if kodiEpisodes != None:
kodiItem = kodiEpisodes.get(comparestring1, None)
2015-03-19 09:52:21 +00:00
userData=API().getUserData(episode)
timeInfo = API().getTimeInfo(episode)
if kodiItem != None:
if kodiItem['playcount'] != int(userData.get("PlayCount")):
2015-03-17 19:02:42 +00:00
WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"episode")
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-17 19:02:42 +00:00
WriteKodiDB().setKodiResumePoint(kodiItem['episodeid'],resume,total,"episode")
if(self.ShouldStop()):
return True
# update progress bar
if(pDialog != None):
percentage = int(((float(count) / float(totalCount)) * 100))
2015-03-18 15:52:11 +00:00
pDialog.update(percentage, message="Updating Episode: " + str(count))
count += 1
showCurrent += 1
finally:
if(pDialog != None):
pDialog.close()
return True
2015-03-13 21:24:59 +00:00
def ShouldStop(self):
if(xbmc.Player().isPlaying() or xbmc.abortRequested):
return True
else:
return False
2015-03-16 17:51:49 +00:00
2015-03-15 17:14:23 +00:00