2015-03-13 21:24:59 +00:00
|
|
|
#################################################################################################
|
|
|
|
# LibrarySync
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
import json
|
2015-03-14 00:46:54 +00:00
|
|
|
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
|
2015-05-01 11:30:21 +00:00
|
|
|
from itertools import chain
|
2015-03-13 21:24:59 +00:00
|
|
|
import urllib2
|
|
|
|
import os
|
2015-03-13 22:39:35 +00:00
|
|
|
|
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-13 21:24:59 +00:00
|
|
|
|
2015-03-25 17:37:21 +00:00
|
|
|
addondir = xbmc.translatePath(xbmcaddon.Addon(id='plugin.video.emby').getAddonInfo('profile'))
|
2015-03-13 21:24:59 +00:00
|
|
|
dataPath = os.path.join(addondir,"library")
|
2015-03-14 22:33:16 +00:00
|
|
|
movieLibrary = os.path.join(dataPath,'movies')
|
|
|
|
tvLibrary = os.path.join(dataPath,'tvshows')
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
class LibrarySync():
|
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
def FullLibrarySync(self):
|
|
|
|
|
|
|
|
#show the progress dialog
|
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
|
|
|
pDialog.create('Emby for Kodi', 'Performing full sync')
|
2015-03-14 22:33:16 +00:00
|
|
|
|
2015-03-27 00:16:45 +00:00
|
|
|
#set some variable to check if this is the first run
|
2015-03-25 17:37:21 +00:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
2015-05-02 09:56:31 +00:00
|
|
|
|
2015-03-18 15:47:55 +00:00
|
|
|
|
2015-03-27 00:16:45 +00:00
|
|
|
startupDone = WINDOW.getProperty("startup") == "done"
|
|
|
|
syncInstallRunDone = addon.getSetting("SyncInstallRunDone") == "true"
|
2015-04-03 08:39:16 +00:00
|
|
|
WINDOW.setProperty("SyncDatabaseRunning", "true")
|
2015-03-27 00:16:45 +00:00
|
|
|
|
2015-04-03 08:39:16 +00:00
|
|
|
if(WINDOW.getProperty("SyncDatabaseShouldStop") == "true"):
|
|
|
|
utils.logMsg("Sync Database", "Can not start SyncDatabaseShouldStop=True", 0)
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
|
|
|
completed = True
|
2015-04-04 17:20:48 +00:00
|
|
|
connection = utils.KodiSQL()
|
|
|
|
cursor = connection.cursor()
|
2015-05-01 11:30:21 +00:00
|
|
|
|
2015-05-02 01:47:05 +00:00
|
|
|
#Add the special emby table
|
|
|
|
if not startupDone:
|
2015-05-02 00:26:06 +00:00
|
|
|
cursor.execute("CREATE TABLE IF NOT EXISTS emby(emby_id TEXT, kodi_id INTEGER, media_type TEXT, checksum TEXT, parent_id INTEGER)")
|
2015-05-01 11:30:21 +00:00
|
|
|
connection.commit()
|
|
|
|
|
2015-04-03 08:39:16 +00:00
|
|
|
# sync movies
|
2015-05-02 09:56:31 +00:00
|
|
|
self.MoviesFullSync(connection,cursor,pDialog)
|
2015-05-01 11:30:21 +00:00
|
|
|
#sync Tvshows and episodes
|
2015-05-02 09:56:31 +00:00
|
|
|
self.TvShowsFullSync(connection,cursor,pDialog)
|
2015-05-01 11:30:21 +00:00
|
|
|
|
2015-04-03 08:39:16 +00: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")
|
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
# Force refresh the library
|
|
|
|
xbmc.executebuiltin("UpdateLibrary(video)")
|
|
|
|
xbmc.executebuiltin("Container.Refresh")
|
|
|
|
xbmc.executebuiltin("Container.Update")
|
|
|
|
|
2015-04-03 08:39:16 +00:00
|
|
|
# set prop to show we have run for the first time
|
|
|
|
WINDOW.setProperty("startup", "done")
|
|
|
|
|
|
|
|
finally:
|
|
|
|
WINDOW.setProperty("SyncDatabaseRunning", "false")
|
2015-04-04 23:59:15 +00:00
|
|
|
utils.logMsg("Sync DB", "syncDatabase Exiting", 0)
|
2015-04-04 17:20:48 +00:00
|
|
|
cursor.close()
|
2015-05-02 09:56:31 +00:00
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
2015-03-18 20:34:52 +00:00
|
|
|
return True
|
2015-05-01 11:30:21 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
def MoviesFullSync(self,connection,cursor, pDialog):
|
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
|
|
|
|
|
|
|
allKodiMovieIds = list()
|
|
|
|
allEmbyMovieIds = list()
|
|
|
|
|
|
|
|
for view in views:
|
2015-03-18 15:47:55 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
allEmbyMovies = ReadEmbyDB().getMovies(view.get('id'))
|
2015-05-01 11:30:21 +00:00
|
|
|
allKodiMovies = ReadKodiDB().getKodiMovies(connection, cursor)
|
2015-05-02 01:47:05 +00:00
|
|
|
|
|
|
|
for kodimovie in allKodiMovies:
|
|
|
|
allKodiMovieIds.append(kodimovie[1])
|
2015-05-02 09:56:31 +00:00
|
|
|
|
|
|
|
total = len(allEmbyMovies) + 1
|
|
|
|
count = 1
|
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
#### PROCESS ADDS AND UPDATES ###
|
2015-05-02 09:56:31 +00:00
|
|
|
for item in allEmbyMovies:
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
allEmbyMovieIds.append(item["Id"])
|
2015-03-18 15:47:55 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Processing " + view.get('title') + " (" + str(count) + " of " + str(total) + ")"
|
|
|
|
pDialog.update(0, "Emby for Kodi - Running Sync", progressTitle)
|
|
|
|
count = 1
|
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
kodiMovie = None
|
|
|
|
for kodimovie in allKodiMovies:
|
|
|
|
if kodimovie[1] == item["Id"]:
|
|
|
|
kodiMovie = kodimovie
|
|
|
|
|
|
|
|
if kodiMovie == None:
|
|
|
|
WriteKodiDB().addOrUpdateMovieToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
|
|
|
else:
|
2015-05-02 09:56:31 +00:00
|
|
|
if kodiMovie[2] != API().getChecksum(item):
|
2015-05-01 11:30:21 +00:00
|
|
|
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")
|
2015-05-02 09:56:31 +00:00
|
|
|
WriteKodiDB().deleteItemFromKodiLibrary(kodiId, connection, cursor)
|
2015-05-01 11:30:21 +00:00
|
|
|
|
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
def TvShowsFullSync(self,connection,cursor,pDialog):
|
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
views = ReadEmbyDB().getCollections("tvshows")
|
2015-03-18 17:00:38 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
allKodiTvShowIds = list()
|
|
|
|
allEmbyTvShowIds = list()
|
2015-05-02 01:47:05 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
for view in views:
|
2015-03-20 08:15:06 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
allEmbyTvShows = ReadEmbyDB().getTvShows(view.get('id'))
|
|
|
|
allKodiTvShows = ReadKodiDB().getKodiTvShows(connection, cursor)
|
2015-05-02 01:47:05 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
total = len(allEmbyTvShows) + 1
|
|
|
|
count = 1
|
|
|
|
|
2015-05-02 01:47:05 +00:00
|
|
|
for kodishow in allKodiTvShows:
|
|
|
|
allKodiTvShowIds.append(kodishow[1])
|
|
|
|
|
2015-04-16 00:44:43 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
#### TVSHOW: PROCESS ADDS AND UPDATES ###
|
|
|
|
for item in allEmbyTvShows:
|
2015-04-04 21:48:02 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
if item.get('IsFolder') and item.get('RecursiveItemCount') != 0:
|
|
|
|
allEmbyTvShowIds.append(item["Id"])
|
2015-03-18 21:38:02 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
if(pDialog != None):
|
|
|
|
progressTitle = "Processing " + view.get('title') + " (" + str(count) + " of " + str(total) + ")"
|
|
|
|
pDialog.update(0, "Emby for Kodi - Running Sync", progressTitle)
|
|
|
|
count = 1
|
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
#build a list with all Id's and get the existing entry (if exists) in Kodi DB
|
|
|
|
kodiShow = None
|
|
|
|
for kodishow in allKodiTvShows:
|
|
|
|
if kodishow[1] == item["Id"]:
|
|
|
|
kodiShow = kodishow
|
|
|
|
|
|
|
|
if kodiShow == None:
|
|
|
|
# Tv show doesn't exist in Kodi yet so proceed and add it
|
|
|
|
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
|
2015-05-02 09:56:31 +00:00
|
|
|
if kodiShow[2] != API().getChecksum(item):
|
2015-05-01 11:30:21 +00:00
|
|
|
WriteKodiDB().addOrUpdateTvShowToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
2015-04-04 21:48:02 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
#### PROCESS EPISODES ######
|
2015-05-02 09:56:31 +00:00
|
|
|
self.EpisodesFullSync(connection,cursor,item["Id"], kodiId)
|
2015-05-01 11:30:21 +00:00
|
|
|
|
|
|
|
#### TVSHOW: PROCESS DELETES #####
|
|
|
|
allEmbyTvShowIds = set(allEmbyTvShowIds)
|
|
|
|
for kodiId in allKodiTvShowIds:
|
|
|
|
if not kodiId in allEmbyTvShowIds:
|
|
|
|
WINDOW.setProperty(kodiId,"deleted")
|
2015-05-02 09:56:31 +00:00
|
|
|
WriteKodiDB().deleteItemFromKodiLibrary(kodiId, connection, cursor)
|
2015-05-01 11:30:21 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
|
|
|
|
def EpisodesFullSync(self,connection,cursor,embyShowId, kodiShowId):
|
2015-05-01 11:30:21 +00:00
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
|
|
|
|
allKodiEpisodeIds = list()
|
|
|
|
allEmbyEpisodeIds = list()
|
2015-03-18 17:00:38 +00:00
|
|
|
|
2015-05-01 11:30:21 +00:00
|
|
|
allEmbyEpisodes = ReadEmbyDB().getEpisodes(embyShowId)
|
|
|
|
allKodiEpisodes = ReadKodiDB().getKodiEpisodes(connection, cursor, kodiShowId)
|
2015-05-02 01:47:05 +00:00
|
|
|
|
|
|
|
for kodiepisode in allKodiEpisodes:
|
|
|
|
allKodiEpisodeIds.append(kodiepisode[1])
|
2015-05-01 11:30:21 +00:00
|
|
|
|
|
|
|
#### EPISODES: PROCESS ADDS AND UPDATES ###
|
|
|
|
for item in allEmbyEpisodes:
|
|
|
|
|
|
|
|
allEmbyEpisodeIds.append(item["Id"])
|
|
|
|
|
2015-05-02 01:47:05 +00:00
|
|
|
#get the existing entry (if exists) in Kodi DB
|
2015-05-01 11:30:21 +00:00
|
|
|
kodiEpisode = None
|
|
|
|
for kodiepisode in allKodiEpisodes:
|
|
|
|
if kodiepisode[1] == item["Id"]:
|
|
|
|
kodiEpisode = kodiepisode
|
|
|
|
|
|
|
|
if kodiEpisode == None:
|
|
|
|
# Episode doesn't exist in Kodi yet so proceed and add it
|
|
|
|
WriteKodiDB().addOrUpdateEpisodeToKodiLibrary(item["Id"], kodiShowId, connection, cursor)
|
|
|
|
else:
|
|
|
|
# If there are changes to the item, perform a full sync of the item
|
2015-05-02 09:56:31 +00:00
|
|
|
if kodiEpisode[2] != API().getChecksum(item):
|
2015-05-01 19:15:43 +00:00
|
|
|
WriteKodiDB().addOrUpdateEpisodeToKodiLibrary(item["Id"], kodiShowId, connection, cursor)
|
2015-05-01 11:30:21 +00:00
|
|
|
|
|
|
|
#### EPISODES: PROCESS DELETES #####
|
|
|
|
allEmbyEpisodeIds = set(allEmbyEpisodeIds)
|
|
|
|
for kodiId in allKodiEpisodeIds:
|
2015-05-02 01:47:05 +00:00
|
|
|
if (not kodiId in allEmbyEpisodeIds):
|
2015-05-01 11:30:21 +00:00
|
|
|
WINDOW.setProperty(kodiId,"deleted")
|
2015-05-02 09:56:31 +00:00
|
|
|
WriteKodiDB().deleteItemFromKodiLibrary(kodiId, connection, cursor)
|
2015-03-18 17:00:38 +00:00
|
|
|
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
def IncrementalSync(self, itemList):
|
|
|
|
#this will only perform sync for items received by the websocket
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
|
|
|
pDialog.create('Emby for Kodi', 'Performing incremental sync...')
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
connection = utils.KodiSQL()
|
|
|
|
cursor = connection.cursor()
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
#### PROCESS MOVIES ####
|
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
|
|
|
for view in views:
|
|
|
|
allEmbyMovies = ReadEmbyDB().getMovies(view.get('id'), itemList)
|
|
|
|
for item in allEmbyMovies:
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
if not item.get('IsFolder'):
|
|
|
|
WriteKodiDB().addOrUpdateMovieToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
#### PROCESS TV SHOWS ####
|
|
|
|
views = ReadEmbyDB().getCollections("tvshows")
|
|
|
|
for view in views:
|
|
|
|
allEmbyTvShows = ReadEmbyDB().getTvShows(view.get('id'),itemList)
|
|
|
|
for item in allEmbyTvShows:
|
|
|
|
if item.get('IsFolder') and item.get('RecursiveItemCount') != 0:
|
|
|
|
kodiId = WriteKodiDB().addOrUpdateTvShowToKodiLibrary(item["Id"],connection, cursor, view.get('title'))
|
|
|
|
|
|
|
|
#### PROCESS EPISODES ######
|
|
|
|
for item in itemList:
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
MBitem = ReadEmbyDB().getItem(item)
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
if MBitem["Type"] == "Episode":
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
#get the tv show
|
|
|
|
cursor.execute("SELECT kodi_id FROM emby WHERE media_type='tvshow' AND emby_id=?", (MBitem["SeriesId"],))
|
|
|
|
result = cursor.fetchall()
|
|
|
|
if result:
|
|
|
|
kodi_show_id = result[0]
|
|
|
|
else:
|
|
|
|
kodi_show_id = None
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
if kodi_show_id:
|
|
|
|
WriteKodiDB().addOrUpdateEpisodeToKodiLibrary(item["Id"], kodi_show_id, connection, cursor)
|
2015-03-21 13:31:30 +00:00
|
|
|
|
2015-05-02 09:56:31 +00:00
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
|
|
|
|
2015-03-20 08:15:06 +00:00
|
|
|
def ShouldStop(self, prog):
|
|
|
|
|
|
|
|
if(prog != None and type(prog) == xbmcgui.DialogProgress):
|
|
|
|
if(prog.iscanceled() == True):
|
|
|
|
return True
|
|
|
|
|
2015-03-16 03:10:41 +00:00
|
|
|
if(xbmc.Player().isPlaying() or xbmc.abortRequested):
|
|
|
|
return True
|
2015-04-03 08:39:16 +00:00
|
|
|
|
|
|
|
if(WINDOW.getProperty("SyncDatabaseShouldStop") == "true"):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2015-03-16 17:51:49 +00:00
|
|
|
|
2015-03-15 17:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|