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
|
|
|
|
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-17 18:18:17 +00:00
|
|
|
from CreateFiles import CreateFiles
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.mb3sync')
|
|
|
|
addondir = xbmc.translatePath(addon.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-03-16 23:54:30 +00:00
|
|
|
sleepVal = 10
|
2015-03-16 07:43:20 +00:00
|
|
|
showProgress = True
|
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
processMovies = True
|
|
|
|
processTvShows = True
|
|
|
|
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
class LibrarySync():
|
|
|
|
|
|
|
|
def syncDatabase(self):
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-16 02:32:45 +00:00
|
|
|
WINDOW.setProperty("librarysync", "busy")
|
|
|
|
pDialog = None
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
try:
|
2015-03-14 21:25:08 +00:00
|
|
|
|
2015-03-16 07:43:20 +00:00
|
|
|
if(showProgress):
|
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
2015-03-16 02:32:45 +00:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.create('Sync DB', 'Sync DB')
|
|
|
|
|
|
|
|
updateNeeded = False
|
2015-03-14 21:25:08 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
#process full movies sync
|
2015-03-16 23:04:29 +00:00
|
|
|
if processMovies:
|
|
|
|
allMovies = list()
|
|
|
|
|
2015-03-17 17:51:45 +00:00
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
2015-03-16 23:04:29 +00:00
|
|
|
for view in views:
|
2015-03-16 03:10:41 +00:00
|
|
|
|
2015-03-17 17:51:45 +00:00
|
|
|
movieData = ReadEmbyDB().getMovies(view.get('id'), True)
|
2015-03-16 23:04:29 +00:00
|
|
|
|
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if(movieData == None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Sync DB : Processing " + view.get('title'))
|
|
|
|
total = len(movieData) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for item in movieData:
|
2015-03-16 23:54:30 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-16 23:04:29 +00:00
|
|
|
if not item.get('IsFolder'):
|
2015-03-17 18:41:26 +00:00
|
|
|
kodiItem = ReadKodiDB().getKodiMovie(item["Id"])
|
2015-03-16 23:04:29 +00:00
|
|
|
allMovies.append(item["Id"])
|
|
|
|
progMessage = "Processing"
|
|
|
|
item['Tag'] = []
|
|
|
|
item['Tag'].append(view.get('title'))
|
|
|
|
if kodiItem == None:
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().addMovieToKodiLibrary(item)
|
2015-03-16 23:04:29 +00:00
|
|
|
updateNeeded = True
|
|
|
|
progMessage = "Adding"
|
|
|
|
else:
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().updateMovieToKodiLibrary(item, kodiItem)
|
2015-03-16 23:04:29 +00:00
|
|
|
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 + " Movie: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
#process full tv shows sync
|
|
|
|
if processTvShows:
|
|
|
|
allTVShows = list()
|
|
|
|
allEpisodes = list()
|
2015-03-17 17:51:45 +00:00
|
|
|
tvShowData = ReadEmbyDB().getTVShows(True)
|
2015-03-16 23:04:29 +00:00
|
|
|
|
2015-03-16 14:29:31 +00:00
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
if (tvShowData == None):
|
|
|
|
return
|
|
|
|
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Sync DB : Processing TV Shows")
|
|
|
|
total = len(tvShowData) + 1
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
for item in tvShowData:
|
2015-03-16 23:54:30 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-16 23:04:29 +00:00
|
|
|
if item.get('IsFolder'):
|
2015-03-17 18:41:26 +00:00
|
|
|
kodiItem = ReadKodiDB().getKodiTVShow(item["Id"])
|
2015-03-16 23:04:29 +00:00
|
|
|
allTVShows.append(item["Id"])
|
2015-03-16 14:29:31 +00:00
|
|
|
progMessage = "Processing"
|
|
|
|
if kodiItem == None:
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().addTVShowToKodiLibrary(item)
|
2015-03-16 14:29:31 +00:00
|
|
|
updateNeeded = True
|
|
|
|
progMessage = "Adding"
|
|
|
|
else:
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().updateTVShowToKodiLibrary(item, kodiItem)
|
2015-03-16 14:29:31 +00:00
|
|
|
progMessage = "Updating"
|
2015-03-16 23:04:29 +00:00
|
|
|
|
2015-03-16 14:29:31 +00:00
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
2015-03-16 23:04:29 +00:00
|
|
|
|
2015-03-16 14:29:31 +00:00
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
2015-03-16 23:04:29 +00:00
|
|
|
pDialog.update(percentage, message=progMessage + " Tv Show: " + str(count))
|
|
|
|
count += 1
|
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
#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-16 02:32:45 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
for tvshow in allTVShows:
|
|
|
|
|
2015-03-17 17:51:45 +00:00
|
|
|
episodeData = ReadEmbyDB().getEpisodes(tvshow,True)
|
2015-03-17 18:41:26 +00:00
|
|
|
kodiEpisodes = ReadKodiDB().getKodiEpisodes(tvshow)
|
2015-03-16 23:04:29 +00:00
|
|
|
|
2015-03-16 03:10:41 +00:00
|
|
|
if(self.ShouldStop()):
|
2015-03-16 23:04:29 +00:00
|
|
|
return True
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, "Sync DB : Processing Episodes")
|
|
|
|
total = len(episodeData) + 1
|
|
|
|
count = 0
|
2015-03-15 17:04:01 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
#we have to compare the lists somehow
|
|
|
|
for item in episodeData:
|
2015-03-16 23:54:30 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-16 23:04:29 +00:00
|
|
|
comparestring1 = str(item.get("ParentIndexNumber")) + "-" + str(item.get("IndexNumber"))
|
|
|
|
matchFound = False
|
|
|
|
progMessage = "Processing"
|
|
|
|
if kodiEpisodes != None:
|
|
|
|
for KodiItem in kodiEpisodes:
|
|
|
|
|
|
|
|
allEpisodes.append(KodiItem["episodeid"])
|
|
|
|
comparestring2 = str(KodiItem["season"]) + "-" + str(KodiItem["episode"])
|
|
|
|
if comparestring1 == comparestring2:
|
|
|
|
#match found - update episode
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().updateEpisodeToKodiLibrary(item,KodiItem,tvshow)
|
2015-03-16 23:04:29 +00:00
|
|
|
matchFound = True
|
|
|
|
progMessage = "Updating"
|
2015-03-15 17:04:01 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
if not matchFound:
|
|
|
|
#no match so we have to create it
|
|
|
|
print "episode not found...creating it: "
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().addEpisodeToKodiLibrary(item,tvshow)
|
2015-03-16 23:04:29 +00:00
|
|
|
updateNeeded = True
|
|
|
|
progMessage = "Adding"
|
|
|
|
|
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(total)) * 100))
|
|
|
|
pDialog.update(percentage, message=progMessage + " Episode: " + str(count))
|
|
|
|
count += 1
|
2015-03-16 05:27:23 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
|
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.update(0, message="Removing Deleted Items")
|
|
|
|
|
2015-03-16 03:10:41 +00:00
|
|
|
if(self.ShouldStop()):
|
2015-03-16 07:43:20 +00:00
|
|
|
return True
|
2015-03-16 03:10:41 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
cleanNeeded = False
|
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
# process deletes for movies
|
|
|
|
if processMovies:
|
|
|
|
allLocaldirs, filesMovies = xbmcvfs.listdir(movieLibrary)
|
|
|
|
allMB3Movies = set(allMovies)
|
|
|
|
for dir in allLocaldirs:
|
|
|
|
if not dir in allMB3Movies:
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().deleteMovieFromKodiLibrary(dir)
|
2015-03-16 23:04:29 +00:00
|
|
|
cleanneeded = True
|
|
|
|
|
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
2015-03-16 03:10:41 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
# process deletes for episodes
|
|
|
|
if processTvShows:
|
|
|
|
# TODO --> process deletes for episodes !!!
|
|
|
|
allLocaldirs, filesTVShows = xbmcvfs.listdir(tvLibrary)
|
|
|
|
allMB3TVShows = set(allTVShows)
|
|
|
|
for dir in allLocaldirs:
|
|
|
|
if not dir in allMB3TVShows:
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().deleteTVShowFromKodiLibrary(dir)
|
2015-03-16 23:04:29 +00:00
|
|
|
cleanneeded = True
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-03-16 03:10:41 +00:00
|
|
|
if(self.ShouldStop()):
|
2015-03-16 07:43:20 +00:00
|
|
|
return True
|
2015-03-16 03:10:41 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
if cleanNeeded:
|
2015-03-16 05:27:23 +00:00
|
|
|
WINDOW.setProperty("cleanNeeded", "true")
|
2015-03-16 02:32:45 +00:00
|
|
|
|
|
|
|
if updateNeeded:
|
2015-03-16 05:27:23 +00:00
|
|
|
WINDOW.setProperty("updateNeeded", "true")
|
2015-03-15 17:04:01 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
finally:
|
|
|
|
WINDOW.clearProperty("librarysync")
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
return True
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
def updatePlayCounts(self):
|
|
|
|
#update all playcounts from MB3 to Kodi library
|
|
|
|
|
2015-03-14 22:33:16 +00:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2015-03-13 21:24:59 +00:00
|
|
|
WINDOW.setProperty("librarysync", "busy")
|
2015-03-16 02:32:45 +00:00
|
|
|
pDialog = None
|
2015-03-15 17:04:01 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
try:
|
2015-03-16 07:43:20 +00:00
|
|
|
if(showProgress):
|
|
|
|
pDialog = xbmcgui.DialogProgressBG()
|
2015-03-16 02:32:45 +00:00
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.create('Sync PlayCounts', 'Sync PlayCounts')
|
2015-03-15 17:04:01 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
#process movies
|
2015-03-16 23:04:29 +00:00
|
|
|
if processMovies:
|
2015-03-17 17:51:45 +00:00
|
|
|
views = ReadEmbyDB().getCollections("movies")
|
2015-03-16 23:04:29 +00:00
|
|
|
for view in views:
|
2015-03-17 17:51:45 +00:00
|
|
|
movieData = ReadEmbyDB().getMovies(view.get('id'),False)
|
2015-03-16 23:04:29 +00:00
|
|
|
|
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
if(movieData == None):
|
|
|
|
return False
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-03-16 07:43:20 +00:00
|
|
|
if(pDialog != None):
|
2015-03-16 23:04:29 +00:00
|
|
|
pDialog.update(0, "Sync PlayCounts: Processing Movies")
|
|
|
|
totalCount = len(movieData) + 1
|
|
|
|
count = 1
|
2015-03-16 07:43:20 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
for item in movieData:
|
2015-03-17 17:25:52 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-16 23:04:29 +00:00
|
|
|
if not item.get('IsFolder'):
|
2015-03-17 18:41:26 +00:00
|
|
|
kodiItem = ReadKodiDB().getKodiMovie(item["Id"])
|
2015-03-16 23:04:29 +00:00
|
|
|
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")
|
2015-03-16 23:04:29 +00: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:
|
|
|
|
print "updating resumepoint for movie " + str(kodiItem['movieid'])
|
2015-03-17 19:02:42 +00:00
|
|
|
WriteKodiDB().setKodiResumePoint(kodiItem['movieid'],resume,total,"movie")
|
2015-03-16 02:32:45 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(totalCount)) * 100))
|
|
|
|
pDialog.update(percentage, message="Updating Movie: " + str(count))
|
|
|
|
count += 1
|
2015-03-16 03:10:41 +00:00
|
|
|
|
2015-03-16 23:04:29 +00:00
|
|
|
#process Tv shows
|
|
|
|
if processTvShows:
|
2015-03-17 17:51:45 +00:00
|
|
|
tvshowData = ReadEmbyDB().getTVShows(False)
|
2015-03-16 23:04:29 +00:00
|
|
|
|
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if (tvshowData == None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
for item in tvshowData:
|
2015-03-17 17:25:52 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-17 17:51:45 +00:00
|
|
|
episodeData = ReadEmbyDB().getEpisodes(item["Id"], False)
|
2015-03-16 23:04:29 +00:00
|
|
|
|
|
|
|
if (episodeData != None):
|
2015-03-16 02:32:45 +00:00
|
|
|
if(pDialog != None):
|
2015-03-16 23:04:29 +00:00
|
|
|
pDialog.update(0, "Sync PlayCounts: Processing Episodes")
|
|
|
|
totalCount = len(episodeData) + 1
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
for episode in episodeData:
|
2015-03-17 17:25:52 +00:00
|
|
|
xbmc.sleep(sleepVal)
|
2015-03-17 18:41:26 +00:00
|
|
|
kodiItem = ReadKodiDB().getKodiEpisodeByMbItem(episode)
|
2015-03-16 23:04:29 +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")
|
2015-03-16 23:04:29 +00: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-17 19:02:42 +00:00
|
|
|
WriteKodiDB().setKodiResumePoint(kodiItem['episodeid'],resume,total,"episode")
|
2015-03-16 23:04:29 +00:00
|
|
|
|
|
|
|
if(self.ShouldStop()):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# update progress bar
|
|
|
|
if(pDialog != None):
|
|
|
|
percentage = int(((float(count) / float(totalCount)) * 100))
|
|
|
|
pDialog.update(percentage, message="Updating Episode: " + str(count))
|
|
|
|
count += 1
|
2015-03-15 17:04:01 +00:00
|
|
|
|
2015-03-16 02:32:45 +00:00
|
|
|
finally:
|
|
|
|
WINDOW.clearProperty("librarysync")
|
|
|
|
if(pDialog != None):
|
|
|
|
pDialog.close()
|
2015-03-14 22:33:16 +00:00
|
|
|
|
|
|
|
return True
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-03-16 03:10:41 +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
|
|
|
|
|
|
|
|
|
|
|
|