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
|
2015-05-01 21:30:21 +10:00
|
|
|
from itertools import chain
|
2015-03-14 08:24:59 +11:00
|
|
|
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-14 08:24:59 +11:00
|
|
|
|
2015-03-26 04:37:21 +11:00
|
|
|
addondir = xbmc.translatePath(xbmcaddon.Addon(id='plugin.video.emby').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-27 11:16:45 +11:00
|
|
|
#set some variable to check if this is the first run
|
2015-03-26 04:37:21 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
2015-03-15 09:33:16 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-03-27 11:16:45 +11:00
|
|
|
startupDone = WINDOW.getProperty("startup") == "done"
|
|
|
|
syncInstallRunDone = addon.getSetting("SyncInstallRunDone") == "true"
|
2015-04-03 19:39:16 +11:00
|
|
|
WINDOW.setProperty("SyncDatabaseRunning", "true")
|
2015-03-27 11:16:45 +11:00
|
|
|
|
2015-04-03 19:39:16 +11:00
|
|
|
if(WINDOW.getProperty("SyncDatabaseShouldStop") == "true"):
|
|
|
|
utils.logMsg("Sync Database", "Can not start SyncDatabaseShouldStop=True", 0)
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
|
|
|
completed = True
|
2015-04-05 03:20:48 +10:00
|
|
|
connection = utils.KodiSQL()
|
|
|
|
cursor = connection.cursor()
|
2015-05-01 21:30:21 +10:00
|
|
|
|
|
|
|
#TEMP --> add new columns
|
|
|
|
try:
|
|
|
|
cursor.execute("alter table movie ADD COLUMN 'embyId' TEXT")
|
|
|
|
cursor.execute("alter table tvshow ADD COLUMN 'embyId' TEXT")
|
|
|
|
cursor.execute("alter table episode ADD COLUMN 'embyId' TEXT")
|
|
|
|
cursor.execute("alter table musicvideo ADD COLUMN 'embyId' TEXT")
|
|
|
|
connection.commit()
|
|
|
|
except: pass
|
|
|
|
|
2015-04-03 19:39:16 +11:00
|
|
|
# sync movies
|
2015-05-01 21:30:21 +10:00
|
|
|
self.MoviesSync(connection,cursor,True)
|
|
|
|
#sync Tvshows and episodes
|
|
|
|
self.TvShowsSync(connection,cursor,True)
|
|
|
|
|
2015-04-03 19:39:16 +11:00
|
|
|
# set the install done setting
|
|
|
|
if(syncInstallRunDone == False and completed):
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby') #force a new instance of the addon
|
|
|
|
addon.setSetting("SyncInstallRunDone", "true")
|
|
|
|
|
|
|
|
# set prop to show we have run for the first time
|
|
|
|
WINDOW.setProperty("startup", "done")
|
|
|
|
|
|
|
|
finally:
|
|
|
|
WINDOW.setProperty("SyncDatabaseRunning", "false")
|
2015-04-05 09:59:15 +10:00
|
|
|
utils.logMsg("Sync DB", "syncDatabase Exiting", 0)
|
2015-04-05 03:20:48 +10:00
|
|
|
cursor.close()
|
2015-04-03 19:39:16 +11:00
|
|
|
|
2015-03-19 07:34:52 +11:00
|
|
|
return True
|
2015-05-01 21:30:21 +10:00
|
|
|
|
|
|
|
def MoviesSync(self,connection,cursor,installFirstRun,itemList = []):
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
|
|
|
pDialog.create('Sync DB', 'Sync Movies')
|
|
|
|
|
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
|
|
|
|
|
|
|
allKodiMovieIds = list()
|
|
|
|
allEmbyMovieIds = list()
|
|
|
|
|
|
|
|
for view in views:
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
allMB3Movies = ReadEmbyDB().getMovies(view.get('id'))
|
|
|
|
allKodiMovies = ReadKodiDB().getKodiMovies(connection, cursor)
|
2015-03-19 15:38:00 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
#### PROCESS ADDS AND UPDATES ###
|
|
|
|
for item in allMB3Movies:
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
allEmbyMovieIds.append(item["Id"])
|
2015-03-19 02:47:55 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
kodiMovie = None
|
|
|
|
for kodimovie in allKodiMovies:
|
|
|
|
allKodiMovieIds.append(kodimovie[1])
|
|
|
|
if kodimovie[1] == item["Id"]:
|
|
|
|
kodiMovie = kodimovie
|
|
|
|
|
|
|
|
if kodiMovie == None:
|
|
|
|
allKodiMovieIds.append(item["Id"])
|
|
|
|
WriteKodiDB().addOrUpdateMovieToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
|
|
|
else:
|
|
|
|
# TODO --> compare with eTag
|
|
|
|
if kodiMovie[2] != item["Name"] or item["Id"] in itemList:
|
|
|
|
WriteKodiDB().addOrUpdateMovieToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
|
|
|
|
|
|
|
#### PROCESS DELETES #####
|
|
|
|
allEmbyMovieIds = set(allEmbyMovieIds)
|
|
|
|
for kodiId in allKodiMovieIds:
|
|
|
|
if not kodiId in allEmbyMovieIds:
|
|
|
|
WINDOW.setProperty(kodiId,"deleted")
|
|
|
|
WriteKodiDB().deleteMovieFromKodiLibrary(kodiId, connection, cursor)
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
def TvShowsSync(self,connection,cursor,installFirstRun,itemList = []):
|
2015-03-15 09:33:16 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
|
|
|
pDialog.create('Sync DB', 'Sync TV Shows')
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
views = ReadEmbyDB().getCollections("tvshows")
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
allKodiTvShowIds = list()
|
|
|
|
allEmbyTvShowIds = list()
|
|
|
|
|
|
|
|
for view in views:
|
2015-03-20 19:15:06 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
allEmbyTvShows = ReadEmbyDB().getTvShows(view.get('id'))
|
|
|
|
allKodiTvShows = ReadKodiDB().getKodiTvShows(connection, cursor)
|
2015-04-16 10:44:43 +10:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
#### TVSHOW: PROCESS ADDS AND UPDATES ###
|
|
|
|
for item in allEmbyTvShows:
|
2015-04-05 07:48:02 +10:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
if item.get('IsFolder') and item.get('RecursiveItemCount') != 0:
|
|
|
|
allEmbyTvShowIds.append(item["Id"])
|
2015-03-19 08:38:02 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
#build a list with all Id's and get the existing entry (if exists) in Kodi DB
|
|
|
|
kodiShow = None
|
|
|
|
for kodishow in allKodiTvShows:
|
|
|
|
allKodiTvShowIds.append(kodishow[1])
|
|
|
|
if kodishow[1] == item["Id"]:
|
|
|
|
kodiShow = kodishow
|
|
|
|
|
|
|
|
if kodiShow == None:
|
|
|
|
# Tv show doesn't exist in Kodi yet so proceed and add it
|
|
|
|
allKodiTvShowIds.append(item["Id"])
|
|
|
|
kodiId = WriteKodiDB().addOrUpdateTvShowToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
|
|
|
else:
|
|
|
|
kodiId = kodishow[0]
|
|
|
|
# If there are changes to the item, perform a full sync of the item
|
|
|
|
if kodiShow[2] != item["Name"] or item["Id"] in itemList:
|
|
|
|
WriteKodiDB().addOrUpdateTvShowToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
2015-04-05 07:48:02 +10:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
#### PROCESS EPISODES ######
|
|
|
|
self.EpisodesSync(connection,cursor,installFirstRun, item["Id"], kodiId, itemList)
|
|
|
|
|
|
|
|
#### TVSHOW: PROCESS DELETES #####
|
|
|
|
allEmbyTvShowIds = set(allEmbyTvShowIds)
|
|
|
|
for kodiId in allKodiTvShowIds:
|
|
|
|
if not kodiId in allEmbyTvShowIds:
|
|
|
|
WINDOW.setProperty(kodiId,"deleted")
|
|
|
|
WriteKodiDB().deleteTvShowFromKodiLibrary(kodiId, connection, cursor)
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
def EpisodesSync(self,connection,cursor,installFirstRun, embyShowId, kodiShowId, itemList = []):
|
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
|
|
|
|
allKodiEpisodeIds = list()
|
|
|
|
allEmbyEpisodeIds = list()
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-05-01 21:30:21 +10:00
|
|
|
allEmbyEpisodes = ReadEmbyDB().getEpisodes(embyShowId)
|
|
|
|
allKodiEpisodes = ReadKodiDB().getKodiEpisodes(connection, cursor, kodiShowId)
|
|
|
|
|
|
|
|
#### EPISODES: PROCESS ADDS AND UPDATES ###
|
|
|
|
for item in allEmbyEpisodes:
|
|
|
|
|
|
|
|
allEmbyEpisodeIds.append(item["Id"])
|
|
|
|
|
|
|
|
#build a list with all Id's and get the existing entry (if exists) in Kodi DB
|
|
|
|
kodiEpisode = None
|
|
|
|
for kodiepisode in allKodiEpisodes:
|
|
|
|
allKodiEpisodeIds.append(kodiepisode[1])
|
|
|
|
if kodiepisode[1] == item["Id"]:
|
|
|
|
kodiEpisode = kodiepisode
|
|
|
|
|
|
|
|
if kodiEpisode == None:
|
|
|
|
# Episode doesn't exist in Kodi yet so proceed and add it
|
|
|
|
allKodiEpisodeIds.append(item["Id"])
|
|
|
|
WriteKodiDB().addOrUpdateEpisodeToKodiLibrary(item["Id"], kodiShowId, connection, cursor)
|
|
|
|
else:
|
|
|
|
# If there are changes to the item, perform a full sync of the item
|
|
|
|
if kodiEpisode[2] != item["Name"] or item["Id"] in itemList:
|
2015-05-02 05:15:43 +10:00
|
|
|
WriteKodiDB().addOrUpdateEpisodeToKodiLibrary(item["Id"], kodiShowId, connection, cursor)
|
2015-05-01 21:30:21 +10:00
|
|
|
|
|
|
|
#### EPISODES: PROCESS DELETES #####
|
|
|
|
allEmbyEpisodeIds = set(allEmbyEpisodeIds)
|
|
|
|
print allEmbyEpisodeIds
|
|
|
|
for kodiId in allKodiEpisodeIds:
|
|
|
|
if not kodiId in allEmbyEpisodeIds:
|
|
|
|
WINDOW.setProperty(kodiId,"deleted")
|
|
|
|
print "deleting ???-->" + kodiId
|
|
|
|
#WriteKodiDB().deleteEpisodeFromKodiLibrary(kodiId, connection, cursor)
|
|
|
|
|
|
|
|
|
2015-03-19 04:00:38 +11:00
|
|
|
|
2015-04-05 03:20:48 +10:00
|
|
|
def MusicVideosSync(self, fullsync, installFirstRun,connection, cursor):
|
2015-03-22 00:31:30 +11:00
|
|
|
|
2015-03-26 04:37:21 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
2015-03-22 00:31:30 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
pDialog = None
|
|
|
|
|
|
|
|
try:
|
2015-03-23 14:54:07 +11:00
|
|
|
dbSyncIndication = addon.getSetting("dbSyncIndication")
|
2015-03-22 00:31:30 +11:00
|
|
|
|
2015-03-27 11:16:45 +11:00
|
|
|
if(installFirstRun 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 = ""
|
|
|
|
|
|
|
|
#process new musicvideos
|
|
|
|
allMB3MusicVideos = ReadEmbyDB().getMusicVideos(True, fullsync)
|
|
|
|
allKodiIds = set(ReadKodiDB().getKodiMusicVideoIds(True))
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
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:
|
2015-04-05 03:20:48 +10:00
|
|
|
WriteKodiDB().addMusicVideoToKodiLibrary(item, connection, cursor)
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
|
|
|
pDialog.update(percentage, progressTitle, "Adding Musicvideo: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
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'):
|
2015-03-30 08:09:02 +11:00
|
|
|
|
|
|
|
if allKodiMusicVideos != None:
|
|
|
|
kodimusicvideo = allKodiMusicVideos.get(item["Id"], None)
|
|
|
|
else:
|
|
|
|
kodimusicvideo = None
|
|
|
|
|
2015-03-22 00:31:30 +11:00
|
|
|
if(kodimusicvideo != None):
|
|
|
|
WriteKodiDB().updateMusicVideoToKodiLibrary_Batched(item, kodimusicvideo)
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
# 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)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-22 00:31:30 +11:00
|
|
|
|
|
|
|
finally:
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
|
|
|
return True
|
2015-04-02 06:07:29 +11:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
def updatePlayCounts(self):
|
|
|
|
#update all playcounts from MB3 to Kodi library
|
|
|
|
|
2015-03-26 04:37:21 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
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-04-03 19:39:16 +11:00
|
|
|
if(WINDOW.getProperty("SyncDatabaseShouldStop") == "true"):
|
|
|
|
utils.logMsg("Sync PlayCount", "Can not start SyncDatabaseShouldStop=True", 0)
|
|
|
|
return True
|
|
|
|
|
2015-03-29 13:34:59 +11:00
|
|
|
if(WINDOW.getProperty("updatePlayCounts_Running") == "true"):
|
|
|
|
utils.logMsg("Sync PlayCount", "updatePlayCounts Already Running", 0)
|
2015-03-30 15:08:47 +11:00
|
|
|
return False
|
2015-04-03 19:39:16 +11:00
|
|
|
|
2015-03-29 13:34:59 +11:00
|
|
|
WINDOW.setProperty("updatePlayCounts_Running", "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-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-16 13:32:45 +11:00
|
|
|
|
2015-03-30 15:08:47 +11:00
|
|
|
if(allMB3Movies != None and allKodiMovies != None):
|
2015-03-19 20:06:05 +11:00
|
|
|
|
2015-03-30 15:08:47 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Sync PlayCounts: Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount)
|
|
|
|
pDialog.update(0, progressTitle)
|
|
|
|
totalCount = len(allMB3Movies) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for item in allMB3Movies:
|
2015-03-17 10:04:29 +11:00
|
|
|
|
2015-03-30 15:08:47 +11:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
kodiItem = allKodiMovies.get(item["Id"], None)
|
|
|
|
|
|
|
|
userData = API().getUserData(item)
|
|
|
|
timeInfo = API().getTimeInfo(item)
|
|
|
|
|
|
|
|
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['movieid'],resume,total,"movie")
|
|
|
|
totalPositionsUpdated += 1
|
|
|
|
updated = WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")), "movie")
|
|
|
|
updated |= WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "movie")
|
|
|
|
if(updated):
|
|
|
|
totalCountsUpdated += 1
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(totalCount)) * 100))
|
|
|
|
pDialog.update(percentage, progressTitle, "Updating Movie: " + str(count))
|
|
|
|
count += 1
|
2015-03-19 19:42:25 +11:00
|
|
|
|
|
|
|
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-04-05 07:48:02 +10:00
|
|
|
views = ReadEmbyDB().getCollections("tvshows")
|
|
|
|
viewCount = len(views)
|
|
|
|
viewCurrent = 1
|
|
|
|
progressTitle = ""
|
|
|
|
for view in views:
|
|
|
|
|
|
|
|
tvshowData = ReadEmbyDB().getTVShows(id = view.get('id'), fullinfo = False, fullSync = True)
|
2015-03-19 20:52:21 +11:00
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if (tvshowData != None):
|
2015-03-30 15:08:47 +11:00
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
showTotal = len(tvshowData)
|
|
|
|
showCurrent = 1
|
2015-03-30 15:08:47 +11:00
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
for item in tvshowData:
|
|
|
|
|
|
|
|
episodeData = ReadEmbyDB().getEpisodes(item["Id"], False)
|
|
|
|
allKodiTVShows = ReadKodiDB().getKodiTvShows(False)
|
|
|
|
kodishow = allKodiTVShows.get(item["Id"],None)
|
|
|
|
if kodishow != None:
|
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(kodishow["tvshowid"],False,True)
|
|
|
|
else:
|
|
|
|
kodiEpisodes = None
|
|
|
|
|
|
|
|
if (episodeData != None):
|
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Sync PlayCounts: Processing TV Show " + str(showCurrent) + " of " + str(showTotal)
|
|
|
|
pDialog.update(0, progressTitle)
|
|
|
|
totalCount = len(episodeData) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for episode in episodeData:
|
|
|
|
|
|
|
|
kodiItem = None
|
|
|
|
matchFound = False
|
|
|
|
if kodiEpisodes != None:
|
2015-04-17 22:26:28 +10:00
|
|
|
kodiItem = kodiEpisodes.get(episode.get("Id"), None)
|
2015-04-05 07:48:02 +10:00
|
|
|
|
|
|
|
userData=API().getUserData(episode)
|
|
|
|
timeInfo = API().getTimeInfo(episode)
|
2015-03-30 15:08:47 +11:00
|
|
|
|
2015-04-12 02:15:28 +10:00
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
if kodiItem != None:
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
WINDOW.setProperty("episodeid" + str(kodiItem['episodeid']), episode.get('Name') + ";;" + episode.get('Id'))
|
2015-04-12 02:15:28 +10:00
|
|
|
WINDOW.setProperty(episode.get('Id'), "episode;;" + str(kodishow["tvshowid"]) + ";;" +str(kodiItem['episodeid']))
|
2015-04-05 07:48:02 +10: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['episodeid'],resume,total,"episode")
|
|
|
|
totalPositionsUpdated += 1
|
2015-03-30 15:08:47 +11:00
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
updated = WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"episode")
|
|
|
|
updated |= WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "episode")
|
|
|
|
if(updated):
|
|
|
|
totalCountsUpdated += 1
|
|
|
|
|
|
|
|
if(self.ShouldStop(pDialog)):
|
|
|
|
return False
|
2015-03-30 15:08:47 +11:00
|
|
|
|
2015-04-05 07:48:02 +10:00
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(totalCount)) * 100))
|
|
|
|
pDialog.update(percentage, progressTitle, "Updating Episode: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
showCurrent += 1
|
2015-03-25 18:47:22 +11:00
|
|
|
|
|
|
|
if(playCountSyncFirstRun != "true"):
|
2015-03-26 04:37:21 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
2015-03-25 18:47:22 +11:00
|
|
|
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:
|
2015-03-29 13:34:59 +11:00
|
|
|
WINDOW.setProperty("updatePlayCounts_Running", "false")
|
2015-03-16 13:32:45 +11:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
2015-03-15 09:33:16 +11:00
|
|
|
|
|
|
|
return True
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-04-18 12:28:39 +10:00
|
|
|
def updatePlayCount(self, itemID):
|
2015-03-20 04:40:29 +11:00
|
|
|
#update playcount of the itemID from MB3 to Kodi library
|
|
|
|
|
2015-03-26 04:37:21 +11:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
2015-03-20 04:40:29 +11:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-04-18 12:28:39 +10:00
|
|
|
|
|
|
|
embyItem = ReadEmbyDB().getItem(itemID)
|
|
|
|
if(embyItem == None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
type = embyItem.get("Type")
|
|
|
|
|
2015-03-20 04:40:29 +11:00
|
|
|
#process movie
|
2015-04-18 12:28:39 +10:00
|
|
|
if type == 'Movie':
|
|
|
|
kodiItem = ReadKodiDB().getKodiMovie(itemID)
|
|
|
|
|
|
|
|
if(kodiItem == None):
|
|
|
|
return False
|
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-04-18 12:28:39 +10:00
|
|
|
|
|
|
|
userData = API().getUserData(embyItem)
|
|
|
|
timeInfo = API().getTimeInfo(embyItem)
|
|
|
|
|
|
|
|
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")
|
|
|
|
#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)
|
|
|
|
WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "movie")
|
2015-03-20 10:24:29 +11:00
|
|
|
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-20 04:40:29 +11:00
|
|
|
|
|
|
|
#process episode
|
2015-04-18 12:28:39 +10:00
|
|
|
elif type == 'Episode':
|
2015-03-20 19:15:06 +11:00
|
|
|
if(self.ShouldStop(None)):
|
2015-03-27 11:16:45 +11:00
|
|
|
return False
|
2015-03-20 04:40:29 +11:00
|
|
|
|
2015-04-18 12:28:39 +10:00
|
|
|
kodiItem = ReadKodiDB().getKodiEpisodeByMbItem(embyItem["Id"], embyItem["SeriesId"])
|
|
|
|
|
|
|
|
userData = API().getUserData(embyItem)
|
|
|
|
timeInfo = API().getTimeInfo(embyItem)
|
|
|
|
|
|
|
|
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")
|
|
|
|
#write property forced will refresh the item in the list so playcount change is immediately visible
|
|
|
|
WriteKodiDB().updateProperty(kodiItem,"playcount",int(userData.get("PlayCount")),"episode",True)
|
|
|
|
WriteKodiDB().updateProperty(kodiItem,"lastplayed",userData.get("LastPlayedDate"), "episode")
|
2015-03-20 04:40:29 +11:00
|
|
|
|
|
|
|
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
|
2015-04-03 19:39:16 +11:00
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
if(WINDOW.getProperty("SyncDatabaseShouldStop") == "true"):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2015-03-17 04:51:49 +11:00
|
|
|
|
2015-03-16 04:14:23 +11:00
|
|
|
|
|
|
|
|
|
|
|
|