PlexKodiConnect/resources/lib/librarysync.py

1735 lines
64 KiB
Python
Raw Normal View History

2015-12-25 07:07:00 +11:00
# -*- coding: utf-8 -*-
##################################################################################################
import threading
from datetime import datetime
import Queue
2015-12-25 07:07:00 +11:00
import xbmc
import xbmcgui
import xbmcvfs
import api
import utils
import clientinfo
import downloadutils
import itemtypes
import embydb_functions as embydb
import kodidb_functions as kodidb
import read_embyserver as embyserver
import userclient
import videonodes
2015-12-29 04:47:16 +11:00
import PlexAPI
import PlexFunctions
2015-12-29 04:47:16 +11:00
2015-12-25 07:07:00 +11:00
##################################################################################################
2016-01-30 06:07:21 +11:00
@utils.ThreadMethodsAdditionalStop('emby_shouldStop')
2016-01-27 01:13:03 +11:00
@utils.ThreadMethods
class ThreadedGetMetadata(threading.Thread):
"""
Threaded download of Plex XML metadata for a certain library item.
Fills the out_queue with the downloaded etree XML objects
Input:
queue Queue.Queue() object that you'll need to fill up
with Plex itemIds
out_queue Queue.Queue() object where this thread will store
the downloaded metadata XMLs as etree objects
lock threading.Lock(), used for counting where we are
"""
2016-01-30 18:43:28 +11:00
def __init__(self, queue, out_queue, lock):
self.queue = queue
self.out_queue = out_queue
self.lock = lock
threading.Thread.__init__(self)
def run(self):
2016-01-28 01:14:30 +11:00
# cache local variables because it's faster
queue = self.queue
out_queue = self.out_queue
lock = self.lock
threadStopped = self.threadStopped
global getMetadataCount
2016-01-30 18:43:28 +11:00
while threadStopped() is False:
# grabs Plex item from queue
try:
updateItem = queue.get(block=False)
# Empty queue
except Queue.Empty:
2016-02-10 19:14:31 +11:00
xbmc.sleep(100)
2016-01-30 18:43:28 +11:00
continue
# Download Metadata
plexXML = PlexFunctions.GetPlexMetadata(updateItem['itemId'])
2016-02-07 22:38:50 +11:00
if plexXML is None:
# Did not receive a valid XML - skip that item for now
2016-01-30 06:07:21 +11:00
queue.task_done()
2016-01-30 18:43:28 +11:00
continue
updateItem['XML'] = plexXML
# place item into out queue
out_queue.put(updateItem)
# Keep track of where we are at
with lock:
getMetadataCount += 1
# signals to queue job is done
queue.task_done()
2016-01-30 06:07:21 +11:00
@utils.ThreadMethodsAdditionalStop('emby_shouldStop')
2016-01-27 01:13:03 +11:00
@utils.ThreadMethods
class ThreadedProcessMetadata(threading.Thread):
"""
Not yet implemented - if ever. Only to be called by ONE thread!
Processes the XML metadata in the queue
Input:
queue: Queue.Queue() object that you'll need to fill up with
the downloaded XML eTree objects
2016-01-11 17:55:22 +11:00
itemType: as used to call functions in itemtypes.py
e.g. 'Movies' => itemtypes.Movies()
lock: threading.Lock(), used for counting where we are
"""
2016-01-30 18:43:28 +11:00
def __init__(self, queue, itemType, lock):
self.queue = queue
self.lock = lock
2016-01-11 17:55:22 +11:00
self.itemType = itemType
threading.Thread.__init__(self)
def run(self):
# Constructs the method name, e.g. itemtypes.Movies
2016-01-11 17:55:22 +11:00
itemFkt = getattr(itemtypes, self.itemType)
2016-01-28 01:14:30 +11:00
# cache local variables because it's faster
queue = self.queue
lock = self.lock
threadStopped = self.threadStopped
global processMetadataCount
2016-01-11 17:55:22 +11:00
global processingViewName
2016-01-30 18:43:28 +11:00
with itemFkt() as item:
while threadStopped() is False:
# grabs item from queue
try:
updateItem = queue.get(block=False)
except Queue.Empty:
2016-02-10 19:14:31 +11:00
xbmc.sleep(100)
2016-01-30 18:43:28 +11:00
continue
# Do the work; lock to be sure we've only got 1 Thread
plexitem = updateItem['XML']
method = updateItem['method']
viewName = updateItem['viewName']
viewId = updateItem['viewId']
title = updateItem['title']
itemSubFkt = getattr(item, method)
with lock:
# Get the one child entry in the xml and process
for child in plexitem:
itemSubFkt(child,
viewtag=viewName,
viewid=viewId)
2016-01-30 18:43:28 +11:00
# Keep track of where we are at
processMetadataCount += 1
processingViewName = title
del plexitem
del updateItem
# signals to queue job is done
queue.task_done()
2016-01-30 06:07:21 +11:00
@utils.ThreadMethodsAdditionalStop('emby_shouldStop')
2016-01-27 01:13:03 +11:00
@utils.ThreadMethods
class ThreadedShowSyncInfo(threading.Thread):
"""
Threaded class to show the Kodi statusbar of the metadata download.
Input:
dialog xbmcgui.DialogProgressBG() object to show progress
locks = [downloadLock, processLock] Locks() to the other threads
total: Total number of items to get
"""
2016-01-27 01:13:03 +11:00
def __init__(self, dialog, locks, total, itemType):
self.locks = locks
self.total = total
self.addonName = clientinfo.ClientInfo().getAddonName()
self.dialog = dialog
2016-01-11 17:55:22 +11:00
self.itemType = itemType
threading.Thread.__init__(self)
def run(self):
2016-01-28 01:14:30 +11:00
# cache local variables because it's faster
total = self.total
2016-01-28 01:14:30 +11:00
dialog = self.dialog
threadStopped = self.threadStopped
downloadLock = self.locks[0]
processLock = self.locks[1]
2016-01-28 01:14:30 +11:00
dialog.create("%s: Sync %s: %s items"
% (self.addonName, self.itemType, str(total)),
"Starting")
global getMetadataCount
global processMetadataCount
2016-01-11 17:55:22 +11:00
global processingViewName
total = 2 * total
totalProgress = 0
2016-01-28 01:14:30 +11:00
while threadStopped() is False:
with downloadLock:
getMetadataProgress = getMetadataCount
2016-01-11 17:55:22 +11:00
with processLock:
processMetadataProgress = processMetadataCount
viewName = processingViewName
totalProgress = getMetadataProgress + processMetadataProgress
try:
percentage = int(float(totalProgress) / float(total)*100.0)
except ZeroDivisionError:
percentage = 0
2016-01-30 06:07:21 +11:00
try:
dialog.update(
percentage,
2016-02-10 19:14:31 +11:00
message="Downloaded: %s. Processed: %s: %s"
% (getMetadataProgress, processMetadataProgress,
viewName))
2016-01-30 06:07:21 +11:00
except:
2016-01-30 18:43:28 +11:00
# Wierd formating of the string viewName?!?
2016-01-30 06:07:21 +11:00
pass
# Sleep for x milliseconds
2016-01-12 06:24:14 +11:00
xbmc.sleep(500)
2016-01-28 01:14:30 +11:00
dialog.close()
2016-01-27 03:20:13 +11:00
@utils.logging
2016-01-27 22:18:54 +11:00
@utils.ThreadMethodsAdditionalSuspend('suspend_LibraryThread')
2016-01-30 06:07:21 +11:00
@utils.ThreadMethodsAdditionalStop('emby_shouldStop')
2016-01-27 01:13:03 +11:00
@utils.ThreadMethods
2015-12-25 07:07:00 +11:00
class LibrarySync(threading.Thread):
_shared_state = {}
# Track websocketclient updates
addedItems = []
updateItems = []
userdataItems = []
removeItems = []
forceLibraryUpdate = False
refresh_views = False
def __init__(self):
self.__dict__ = self._shared_state
2016-01-30 06:07:21 +11:00
# How long should we look into the past for fast syncing items (in s)
self.syncPast = 60
2015-12-25 07:07:00 +11:00
self.clientInfo = clientinfo.ClientInfo()
self.doUtils = downloadutils.DownloadUtils()
self.user = userclient.UserClient()
self.emby = embyserver.Read_EmbyServer()
self.vnodes = videonodes.VideoNodes()
self.syncThreadNumber = int(utils.settings('syncThreadNumber'))
2015-12-25 07:07:00 +11:00
2016-01-28 06:41:28 +11:00
self.installSyncDone = True if \
utils.settings('SyncInstallRunDone') == 'true' else False
2015-12-25 07:07:00 +11:00
threading.Thread.__init__(self)
def progressDialog(self, title, forced=False):
dialog = None
if utils.settings('dbSyncIndicator') == "true" or forced:
dialog = xbmcgui.DialogProgressBG()
2016-01-23 03:03:24 +11:00
dialog.create(self.addonName, title)
2015-12-25 07:07:00 +11:00
self.logMsg("Show progress dialog: %s" % title, 2)
return dialog
def startSync(self):
2016-01-28 06:41:28 +11:00
# Only run fastSync AFTER startup when SyncInstallRunDone has already
# been set
utils.window('emby_dbScan', value="true")
completed = self.fastSync()
if not completed:
# Fast sync failed or server plugin is not found
self.logMsg("Something went wrong, starting full sync", 0)
completed = self.fullSync(manualrun=True)
utils.window('emby_dbScan', value="false")
2015-12-25 07:07:00 +11:00
return completed
2016-01-12 03:37:01 +11:00
def fastSync(self):
2016-01-28 01:14:30 +11:00
"""
Fast incremential lib sync
2016-01-12 03:37:01 +11:00
2016-01-28 01:14:30 +11:00
Using /library/recentlyAdded is NOT working as changes to lib items are
not reflected
"""
2016-01-28 06:41:28 +11:00
self.compare = True
2016-01-28 01:14:30 +11:00
# Get last sync time
2016-01-30 06:07:21 +11:00
lastSync = self.lastSync - self.syncPast
2016-01-28 01:14:30 +11:00
if not lastSync:
# Original Emby format:
# lastSync = "2016-01-01T00:00:00Z"
# January 1, 2015 at midnight:
2016-01-30 06:07:21 +11:00
lastSync = 1420070400
# Set new timestamp NOW because sync might take a while
self.saveLastSync()
2016-01-28 01:14:30 +11:00
2016-01-28 06:41:28 +11:00
# Get all PMS items already saved in Kodi
# Also get checksums of every Plex items already saved in Kodi
allKodiElementsId = {}
2016-02-10 21:00:32 +11:00
with embydb.GetEmbyDB() as emby_db:
for itemtype in PlexFunctions.EmbyItemtypes():
try:
allKodiElementsId.update(
dict(emby_db.getChecksum(itemtype)))
except ValueError:
pass
2016-01-28 06:41:28 +11:00
self.allKodiElementsId = allKodiElementsId
2016-01-28 01:14:30 +11:00
# Run through views and get latest changed elements using time diff
2016-01-28 06:41:28 +11:00
self.updatelist = []
self.allPlexElementsId = {}
updateKodiVideoLib = False
for view in self.views:
if self.threadStopped():
2016-01-28 06:41:28 +11:00
return True
# Get items per view
2016-01-30 06:07:21 +11:00
items = PlexFunctions.GetAllPlexLeaves(
view['id'], updatedAt=lastSync)
2016-01-28 06:41:28 +11:00
if not items:
continue
# Get one itemtype, because they're the same in the PMS section
plexType = items[0].attrib['type']
2016-01-28 06:41:28 +11:00
# Populate self.updatelist
self.GetUpdatelist(items,
PlexFunctions.GetItemClassFromType(plexType),
2016-02-07 22:38:50 +11:00
PlexFunctions.GetMethodFromPlexType(plexType),
2016-01-28 06:41:28 +11:00
view['name'],
view['id'],
dontCheck=True)
2016-01-28 06:41:28 +11:00
# Process self.updatelist
if self.updatelist:
if self.updatelist[0]['itemType'] in ['Movies', 'TVShows']:
updateKodiVideoLib = True
2016-02-04 00:44:11 +11:00
self.GetAndProcessXMLs(
PlexFunctions.GetItemClassFromType(plexType))
2016-01-28 06:41:28 +11:00
self.updatelist = []
# Let Kodi grab the artwork now
if updateKodiVideoLib:
xbmc.executebuiltin('UpdateLibrary(video)')
# Update userdata
for view in self.views:
self.PlexUpdateWatched(
view['id'],
2016-01-30 06:07:21 +11:00
PlexFunctions.GetItemClassFromType(view['itemtype']),
lastViewedAt=lastSync)
2016-01-28 06:41:28 +11:00
# Reset and return
self.allKodiElementsId = {}
self.allPlexElementsId = {}
return True
2016-01-12 03:37:01 +11:00
2015-12-25 07:07:00 +11:00
def saveLastSync(self):
# Save last sync time
2016-01-30 06:07:21 +11:00
self.lastSync = utils.getUnixTimestamp()
2015-12-25 07:07:00 +11:00
def initializeDBs(self):
"""
Run once during startup to verify that emby db exists.
"""
2015-12-25 07:07:00 +11:00
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
# Create the tables for the emby database
# emby, view, version
embycursor.execute(
"""CREATE TABLE IF NOT EXISTS emby(
emby_id TEXT UNIQUE, media_folder TEXT, emby_type TEXT, media_type TEXT, kodi_id INTEGER,
kodi_fileid INTEGER, kodi_pathid INTEGER, parent_id INTEGER, checksum INTEGER)""")
embycursor.execute(
"""CREATE TABLE IF NOT EXISTS view(
view_id TEXT UNIQUE, view_name TEXT, media_type TEXT, kodi_tagid INTEGER)""")
embycursor.execute("CREATE TABLE IF NOT EXISTS version(idVersion TEXT)")
embyconn.commit()
# content sync: movies, tvshows, musicvideos, music
embyconn.close()
return
def fullSync(self, manualrun=False, repair=False):
# Only run once when first setting up. Can be run manually.
2016-01-30 06:07:21 +11:00
self.compare = manualrun or repair
music_enabled = utils.settings('enableMusic') == "true"
# Add sources
utils.sourcesXML()
2015-12-25 07:07:00 +11:00
if manualrun:
message = "Manual sync"
elif repair:
message = "Repair sync"
else:
message = "Initial sync"
utils.window('emby_initialScan', value="true")
2016-01-30 06:07:21 +11:00
# Set new timestamp NOW because sync might take a while
self.saveLastSync()
2015-12-25 07:07:00 +11:00
starttotal = datetime.now()
2016-01-11 19:57:45 +11:00
# Ensure that DBs exist if called for very first time
self.initializeDBs()
2016-01-11 19:57:45 +11:00
# Set views
self.maintainViews()
2015-12-25 07:07:00 +11:00
# Sync video library
2016-01-15 20:04:47 +11:00
# process = {
2015-12-25 07:07:00 +11:00
2016-01-15 20:04:47 +11:00
# 'movies': self.movies,
# 'musicvideos': self.musicvideos,
# 'tvshows': self.tvshows
# }
2015-12-28 23:10:05 +11:00
process = {
2016-01-10 02:14:02 +11:00
'movies': self.PlexMovies,
'tvshows': self.PlexTVShows
2015-12-28 23:10:05 +11:00
}
2015-12-25 07:07:00 +11:00
for itemtype in process:
startTime = datetime.now()
completed = process[itemtype]()
2015-12-25 07:07:00 +11:00
if not completed:
return False
else:
elapsedTime = datetime.now() - startTime
self.logMsg(
"SyncDatabase (finished %s in: %s)"
% (itemtype, str(elapsedTime).split('.')[0]), 1)
# # sync music
# if music_enabled:
2015-12-25 07:07:00 +11:00
# musicconn = utils.kodiSQL('music')
# musiccursor = musicconn.cursor()
2015-12-25 07:07:00 +11:00
# startTime = datetime.now()
# completed = self.music(embycursor, musiccursor, pDialog)
# if not completed:
# utils.window('emby_dbScan', clear=True)
# embycursor.close()
# musiccursor.close()
# return False
# else:
# musicconn.commit()
# embyconn.commit()
# elapsedTime = datetime.now() - startTime
# self.logMsg(
# "SyncDatabase (finished music in: %s)"
# % (str(elapsedTime).split('.')[0]), 1)
# musiccursor.close()
2015-12-25 07:07:00 +11:00
xbmc.executebuiltin('UpdateLibrary(video)')
elapsedtotal = datetime.now() - starttotal
utils.window('emby_initialScan', clear=True)
2015-12-25 07:07:00 +11:00
xbmcgui.Dialog().notification(
heading=self.addonName,
message="%s completed in: %s"
% (message, str(elapsedtotal).split('.')[0]),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
sound=False)
2015-12-25 07:07:00 +11:00
return True
2016-01-11 19:57:45 +11:00
def maintainViews(self):
2015-12-28 23:10:05 +11:00
"""
2016-01-11 19:57:45 +11:00
Compare the views to Plex
2015-12-28 23:10:05 +11:00
"""
2016-01-11 19:57:45 +11:00
# Open DB links
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
kodiconn = utils.kodiSQL('video')
kodicursor = kodiconn.cursor()
2015-12-25 07:07:00 +11:00
emby_db = embydb.Embydb_Functions(embycursor)
kodi_db = kodidb.Kodidb_Functions(kodicursor)
doUtils = self.doUtils
vnodes = self.vnodes
2016-01-11 19:57:45 +11:00
2015-12-25 07:07:00 +11:00
# Get views
result = doUtils.downloadUrl("{server}/library/sections")
if not result:
self.logMsg("Error download PMS views, abort maintainViews", -1)
return False
2015-12-25 07:07:00 +11:00
# total nodes for window properties
vnodes.clearProperties()
totalnodes = 0
# Set views for supported media type
2016-01-11 19:57:45 +11:00
mediatypes = [
'movie',
'show'
]
for folderItem in result:
folder = folderItem.attrib
2016-01-11 19:57:45 +11:00
mediatype = folder['type']
if mediatype in mediatypes:
folderid = folder['key']
foldername = folder['title']
2015-12-25 07:07:00 +11:00
viewtype = folder['type']
# Get current media folders from emby database
view = emby_db.getView_byId(folderid)
try:
current_viewname = view[0]
current_viewtype = view[1]
current_tagid = view[2]
except TypeError:
self.logMsg("Creating viewid: %s in Emby database." % folderid, 1)
tagid = kodi_db.createTag(foldername)
# Create playlist for the video library
2016-01-19 06:17:14 +11:00
if mediatype in ['movies', 'tvshows', 'musicvideos']:
2015-12-25 07:07:00 +11:00
utils.playlistXSP(mediatype, foldername, viewtype)
2016-01-19 06:17:14 +11:00
# Create the video node
if mediatype in ['movies', 'tvshows', 'musicvideos', 'homevideos']:
vnodes.viewNode(totalnodes, foldername, mediatype, viewtype)
totalnodes += 1
2015-12-25 07:07:00 +11:00
# Add view to emby database
emby_db.addView(folderid, foldername, viewtype, tagid)
else:
self.logMsg(' '.join((
"Found viewid: %s" % folderid,
"viewname: %s" % current_viewname,
"viewtype: %s" % current_viewtype,
"tagid: %s" % current_tagid)), 2)
# View was modified, update with latest info
if current_viewname != foldername:
self.logMsg("viewid: %s new viewname: %s" % (folderid, foldername), 1)
tagid = kodi_db.createTag(foldername)
# Update view with new info
emby_db.updateView(foldername, tagid, folderid)
if mediatype != "music":
if emby_db.getView_byName(current_viewname) is None:
# The tag could be a combined view. Ensure there's no other tags
# with the same name before deleting playlist.
utils.playlistXSP(
mediatype, current_viewname, current_viewtype, True)
# Delete video node
if mediatype != "musicvideos":
vnodes.viewNode(
2016-01-12 00:38:01 +11:00
totalnodes,
current_viewname,
mediatype,
current_viewtype,
2015-12-25 07:07:00 +11:00
delete=True)
# Added new playlist
if mediatype in ['movies', 'tvshows', 'musicvideos']:
utils.playlistXSP(mediatype, foldername, viewtype)
2015-12-25 07:07:00 +11:00
# Add new video node
if mediatype != "musicvideos":
vnodes.viewNode(totalnodes, foldername, mediatype, viewtype)
totalnodes += 1
# Update items with new tag
items = emby_db.getItem_byView(folderid)
for item in items:
# Remove the "s" from viewtype for tags
kodi_db.updateTag(
current_tagid, tagid, item[0], current_viewtype[:-1])
else:
if mediatype != "music":
# Validate the playlist exists or recreate it
if mediatype in ['movies', 'tvshows', 'musicvideos']:
utils.playlistXSP(mediatype, foldername, viewtype)
2015-12-25 07:07:00 +11:00
# Create the video node if not already exists
if mediatype != "musicvideos":
vnodes.viewNode(totalnodes, foldername, mediatype, viewtype)
totalnodes += 1
else:
# Add video nodes listings
2016-01-11 19:57:45 +11:00
# vnodes.singleNode(totalnodes, "Favorite movies", "movies", "favourites")
# totalnodes += 1
# vnodes.singleNode(totalnodes, "Favorite tvshows", "tvshows", "favourites")
# totalnodes += 1
# vnodes.singleNode(totalnodes, "channels", "movies", "channels")
# totalnodes += 1
2015-12-25 07:07:00 +11:00
# Save total
utils.window('Emby.nodes.total', str(totalnodes))
2016-01-28 06:41:28 +11:00
# update views for all:
self.views = emby_db.getAllViewInfo()
self.logMsg("views saved: %s" % self.views, 1)
2016-01-11 19:57:45 +11:00
# commit changes to DB
embyconn.commit()
kodiconn.commit()
embyconn.close()
kodiconn.close()
2015-12-25 07:07:00 +11:00
def GetUpdatelist(self, xml, itemType, method, viewName, viewId,
dontCheck=False):
"""
Adds items to self.updatelist as well as self.allPlexElementsId dict
2015-12-25 07:07:00 +11:00
Input:
xml: PMS answer for section items
itemType: 'Movies', 'TVShows', ...
method: Method name to be called with this itemtype
see itemtypes.py
viewName: Name of the Plex view (e.g. 'My TV shows')
viewId: Id/Key of Plex library (e.g. '1')
dontCheck: If True, skips checksum check but assumes
that all items in xml must be processed
Output: self.updatelist, self.allPlexElementsId
self.updatelist APPENDED(!!) list itemids (Plex Keys as
2016-01-30 06:07:21 +11:00
as received from API.getRatingKey())
One item in this list is of the form:
'itemId': xxx,
'itemType': 'Movies','TVShows', ...
'method': 'add_update', 'add_updateSeason', ...
'viewName': xxx,
2016-01-28 06:41:28 +11:00
'viewId': xxx,
'title': xxx
self.allPlexElementsId APPENDED(!!) dict
= {itemid: checksum}
"""
if self.compare or not dontCheck:
# Manual sync
for item in xml:
# Skipping items 'title=All episodes' without a 'ratingKey'
if not item.attrib.get('ratingKey', False):
2016-01-12 20:30:28 +11:00
continue
API = PlexAPI.API(item)
2016-01-30 06:07:21 +11:00
itemId = API.getRatingKey()
2016-01-12 20:30:28 +11:00
title, sorttitle = API.getTitle()
plex_checksum = API.getChecksum()
2016-01-12 20:30:28 +11:00
self.allPlexElementsId[itemId] = plex_checksum
kodi_checksum = self.allKodiElementsId.get(itemId)
if kodi_checksum != plex_checksum:
# Only update if movie is not in Kodi or checksum is
# different
self.updatelist.append({'itemId': itemId,
'itemType': itemType,
'method': method,
'viewName': viewName,
'viewId': viewId,
'title': title})
else:
# Initial or repair sync: get all Plex movies
for item in xml:
2016-01-10 02:14:02 +11:00
# Only look at valid items = Plex library items
if not item.attrib.get('ratingKey', False):
2016-01-12 20:30:28 +11:00
continue
API = PlexAPI.API(item)
2016-01-30 06:07:21 +11:00
itemId = API.getRatingKey()
2016-01-12 20:30:28 +11:00
title, sorttitle = API.getTitle()
plex_checksum = API.getChecksum()
self.allPlexElementsId[itemId] = plex_checksum
self.updatelist.append({'itemId': itemId,
'itemType': itemType,
'method': method,
'viewName': viewName,
'viewId': viewId,
'title': title})
2016-01-11 17:55:22 +11:00
def GetAndProcessXMLs(self, itemType):
"""
Downloads all XMLs for itemType (e.g. Movies, TV-Shows). Processes them
by then calling itemtypes.<itemType>()
2015-12-29 04:47:16 +11:00
Input:
itemType: 'Movies', 'TVShows', ...
2016-01-11 17:55:22 +11:00
self.updatelist
"""
# Some logging, just in case.
self.logMsg("self.updatelist: %s" % self.updatelist, 2)
2016-01-28 06:41:28 +11:00
itemNumber = len(self.updatelist)
if itemNumber == 0:
2016-01-12 03:42:42 +11:00
return True
# Run through self.updatelist, get XML metadata per item
# Initiate threads
self.logMsg("Starting sync threads", 1)
getMetadataQueue = Queue.Queue()
2016-01-12 19:50:15 +11:00
processMetadataQueue = Queue.Queue(maxsize=100)
getMetadataLock = threading.Lock()
processMetadataLock = threading.Lock()
# To keep track
global getMetadataCount
getMetadataCount = 0
global processMetadataCount
processMetadataCount = 0
2016-01-11 17:55:22 +11:00
global processingViewName
processingViewName = ''
2016-01-10 02:14:02 +11:00
# Populate queue: GetMetadata
2016-01-11 17:55:22 +11:00
for updateItem in self.updatelist:
getMetadataQueue.put(updateItem)
# Spawn GetMetadata threads for downloading
threads = []
2016-01-28 06:41:28 +11:00
for i in range(min(self.syncThreadNumber, itemNumber)):
2016-01-11 17:55:22 +11:00
thread = ThreadedGetMetadata(getMetadataQueue,
processMetadataQueue,
2016-01-30 18:43:28 +11:00
getMetadataLock)
2016-01-11 17:55:22 +11:00
thread.setDaemon(True)
thread.start()
threads.append(thread)
self.logMsg("%s download threads spawned" % len(threads), 1)
2016-01-30 18:43:28 +11:00
# Spawn one more thread to process Metadata, once downloaded
thread = ThreadedProcessMetadata(processMetadataQueue,
itemType,
processMetadataLock)
thread.setDaemon(True)
thread.start()
threads.append(thread)
self.logMsg("Processing thread spawned", 1)
# Start one thread to show sync progress
dialog = xbmcgui.DialogProgressBG()
2016-01-27 01:13:03 +11:00
thread = ThreadedShowSyncInfo(dialog,
[getMetadataLock, processMetadataLock],
2016-01-28 06:41:28 +11:00
itemNumber,
2016-01-27 01:13:03 +11:00
itemType)
thread.setDaemon(True)
thread.start()
threads.append(thread)
self.logMsg("Kodi Infobox thread spawned", 1)
2016-01-30 06:07:21 +11:00
# Wait until finished
2016-01-30 18:43:28 +11:00
getMetadataQueue.join()
processMetadataQueue.join()
# Kill threads
self.logMsg("Waiting to kill threads", 1)
for thread in threads:
thread.stopThread()
self.logMsg("Stop sent to all threads", 1)
# Wait till threads are indeed dead
for thread in threads:
2016-01-27 01:13:03 +11:00
thread.join(5.0)
2016-01-12 19:50:15 +11:00
if thread.isAlive():
self.logMsg("Could not terminate thread", -1)
2016-01-28 01:14:30 +11:00
try:
del threads
except:
self.logMsg("Could not delete threads", -1)
2016-01-27 01:13:03 +11:00
# Make sure dialog window is closed
2016-01-11 17:55:22 +11:00
if dialog:
dialog.close()
self.logMsg("Sync threads finished", 1)
2016-01-28 06:41:28 +11:00
self.updatelist = []
2015-12-29 04:47:16 +11:00
return True
def PlexMovies(self):
# Initialize
self.allPlexElementsId = {}
2016-01-12 00:38:01 +11:00
2016-01-11 17:55:22 +11:00
itemType = 'Movies'
2016-01-28 06:41:28 +11:00
views = [x for x in self.views if x['itemtype'] == 'movie']
2016-01-11 17:55:22 +11:00
self.logMsg("Processing Plex %s. Libraries: %s" % (itemType, views), 1)
self.allKodiElementsId = {}
if self.compare:
2016-02-10 21:00:32 +11:00
with embydb.GetEmbyDB() as emby_db:
# Get movies from Plex server
# Pull the list of movies and boxsets in Kodi
try:
self.allKodiElementsId = dict(emby_db.getChecksum('Movie'))
except ValueError:
self.allKodiElementsId = {}
##### PROCESS MOVIES #####
2016-01-11 17:55:22 +11:00
self.updatelist = []
for view in views:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
return False
# Get items per view
viewId = view['id']
viewName = view['name']
all_plexmovies = PlexFunctions.GetPlexSectionResults(viewId)
if not all_plexmovies:
self.logMsg("Couldnt get section items, aborting for view.", 1)
continue
# Populate self.updatelist and self.allPlexElementsId
2016-01-11 17:55:22 +11:00
self.GetUpdatelist(all_plexmovies,
itemType,
'add_update',
viewName,
viewId)
self.GetAndProcessXMLs(itemType)
self.logMsg("Processed view %s with ID %s" % (viewName, viewId), 1)
# Update viewstate
for view in views:
if self.threadStopped():
return False
self.PlexUpdateWatched(view['id'], itemType)
2016-01-11 01:16:59 +11:00
##### PROCESS DELETES #####
if self.compare:
# Manual sync, process deletes
with itemtypes.Movies() as Movie:
for kodimovie in self.allKodiElementsId:
if kodimovie not in self.allPlexElementsId:
Movie.remove(kodimovie)
2016-01-11 18:10:36 +11:00
self.logMsg("%s sync is finished." % itemType, 1)
2016-01-11 01:16:59 +11:00
return True
2016-01-30 06:07:21 +11:00
def PlexUpdateWatched(self, viewId, itemType,
lastViewedAt=None, updatedAt=None):
"""
2016-01-30 06:07:21 +11:00
Updates plex elements' view status ('watched' or 'unwatched') and
also updates resume times.
This is done by downloading one XML for ALL elements with viewId
"""
2016-02-04 00:44:11 +11:00
xml = PlexFunctions.GetAllPlexLeaves(viewId,
lastViewedAt=lastViewedAt,
updatedAt=updatedAt)
if xml:
2016-01-30 06:07:21 +11:00
itemMth = getattr(itemtypes, itemType)
with itemMth() as method:
2016-02-04 00:44:11 +11:00
method.updateUserdata(xml)
def musicvideos(self, embycursor, kodicursor, pdialog):
2015-12-25 07:07:00 +11:00
# Get musicvideos from emby
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
mvideos = itemtypes.MusicVideos(embycursor, kodicursor)
views = emby_db.getView_byType('musicvideos')
self.logMsg("Media folders: %s" % views, 1)
for view in views:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
2015-12-25 07:07:00 +11:00
return False
# Get items per view
viewId = view['id']
viewName = view['name']
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Gathering musicvideos from view: %s..." % viewName)
# Initial or repair sync
all_embymvideos = emby.getMusicVideos(viewId, dialog=pdialog)
total = all_embymvideos['TotalRecordCount']
embymvideos = all_embymvideos['Items']
2015-12-25 07:07:00 +11:00
if pdialog:
pdialog.update(heading="Processing %s / %s items" % (viewName, total))
count = 0
for embymvideo in embymvideos:
# Process individual musicvideo
2016-01-27 01:13:03 +11:00
if self.threadStopped():
2015-12-25 07:07:00 +11:00
return False
title = embymvideo['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
mvideos.add_update(embymvideo, viewName, viewId)
else:
self.logMsg("MusicVideos finished.", 2)
return True
2016-01-10 02:14:02 +11:00
def PlexTVShows(self):
# Initialize
self.allPlexElementsId = {}
2016-01-11 17:55:22 +11:00
itemType = 'TVShows'
2016-01-28 06:41:28 +11:00
views = [x for x in self.views if x['itemtype'] == 'show']
2016-01-11 18:10:36 +11:00
self.logMsg("Media folders for %s: %s" % (itemType, views), 1)
self.allKodiElementsId = {}
2016-01-10 02:14:02 +11:00
if self.compare:
2016-02-10 21:00:32 +11:00
with embydb.GetEmbyDB() as emby_db:
# Get movies from Plex server
# Pull the list of TV shows already in Kodi
try:
all_koditvshows = dict(emby_db.getChecksum('Series'))
self.allKodiElementsId.update(all_koditvshows)
except ValueError:
pass
# Same for seasons
try:
all_kodiseasons = dict(emby_db.getChecksum('Season'))
self.allKodiElementsId.update(all_kodiseasons)
except ValueError:
pass
# Same for the episodes (sub-element of shows/series)
try:
all_kodiepisodes = dict(emby_db.getChecksum('Episode'))
self.allKodiElementsId.update(all_kodiepisodes)
except ValueError:
pass
2016-01-10 02:14:02 +11:00
##### PROCESS TV Shows #####
2016-01-11 17:55:22 +11:00
self.updatelist = []
for view in views:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
return False
# Get items per view
viewId = view['id']
viewName = view['name']
allPlexTvShows = PlexFunctions.GetPlexSectionResults(viewId)
if not allPlexTvShows:
self.logMsg(
"Error downloading show view xml for view %s" % viewId, -1)
continue
2016-01-10 02:14:02 +11:00
# Populate self.updatelist and self.allPlexElementsId
2016-01-11 17:55:22 +11:00
self.GetUpdatelist(allPlexTvShows,
itemType,
'add_update',
viewName,
viewId)
2016-01-11 18:10:36 +11:00
self.logMsg("Analyzed view %s with ID %s" % (viewName, viewId), 1)
2016-01-11 01:16:59 +11:00
# COPY for later use
allPlexTvShowsId = self.allPlexElementsId.copy()
2016-01-10 02:14:02 +11:00
##### PROCESS TV Seasons #####
# Cycle through tv shows
for tvShowId in allPlexTvShowsId:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
2016-01-11 17:55:22 +11:00
return False
2016-01-10 02:14:02 +11:00
# Grab all seasons to tvshow from PMS
seasons = PlexFunctions.GetAllPlexChildren(tvShowId)
if not seasons:
self.logMsg(
"Error downloading season xml for show %s" % tvShowId, -1)
continue
2016-01-10 02:14:02 +11:00
# Populate self.updatelist and self.allPlexElementsId
2016-01-11 17:55:22 +11:00
self.GetUpdatelist(seasons,
itemType,
'add_updateSeason',
None,
tvShowId) # send showId instead of viewid
self.logMsg("Analyzed all seasons of TV show with Plex Id %s"
% tvShowId, 1)
2016-01-10 02:14:02 +11:00
##### PROCESS TV Episodes #####
# Cycle through tv shows
for view in views:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
2016-01-11 17:55:22 +11:00
return False
2016-01-10 02:14:02 +11:00
# Grab all episodes to tvshow from PMS
episodes = PlexFunctions.GetAllPlexLeaves(view['id'])
if not episodes:
self.logMsg(
"Error downloading episod xml for view %s"
% view.get('name'), -1)
continue
2016-01-10 02:14:02 +11:00
# Populate self.updatelist and self.allPlexElementsId
2016-01-11 17:55:22 +11:00
self.GetUpdatelist(episodes,
itemType,
'add_updateEpisode',
None,
None)
self.logMsg("Analyzed all episodes of TV show with Plex Id %s"
% tvShowId, 1)
2016-01-11 17:55:22 +11:00
# Process self.updatelist
self.GetAndProcessXMLs(itemType)
2016-01-11 18:10:36 +11:00
self.logMsg("GetAndProcessXMLs completed", 1)
2016-01-11 17:55:22 +11:00
# Refresh season info
# Cycle through tv shows
with itemtypes.TVShows() as TVshow:
for tvShowId in allPlexTvShowsId:
XMLtvshow = PlexFunctions.GetPlexMetadata(tvShowId)
2016-01-11 17:55:22 +11:00
TVshow.refreshSeasonEntry(XMLtvshow, tvShowId)
2016-01-11 18:10:36 +11:00
self.logMsg("Season info refreshed", 1)
2016-01-11 17:55:22 +11:00
# Update viewstate:
for view in views:
self.PlexUpdateWatched(view['id'], itemType)
2016-01-11 01:16:59 +11:00
if self.compare:
# Manual sync, process deletes
with itemtypes.TVShows() as TVShow:
for kodiTvElement in self.allKodiElementsId:
if kodiTvElement not in self.allPlexElementsId:
TVShow.remove(kodiTvElement)
2016-01-11 18:10:36 +11:00
self.logMsg("%s sync is finished." % itemType, 1)
return True
def music(self, embycursor, kodicursor, pdialog):
2015-12-25 07:07:00 +11:00
# Get music from emby
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
music = itemtypes.Music(embycursor, kodicursor)
process = {
'artists': [emby.getArtists, music.add_updateArtist],
'albums': [emby.getAlbums, music.add_updateAlbum],
'songs': [emby.getSongs, music.add_updateSong]
}
types = ['artists', 'albums', 'songs']
for type in types:
if pdialog:
2016-01-23 22:05:56 +11:00
pdialog.update(
heading="Emby for Kodi",
message="Gathering %s..." % type)
all_embyitems = process[type][0](dialog=pdialog)
total = all_embyitems['TotalRecordCount']
embyitems = all_embyitems['Items']
2015-12-25 07:07:00 +11:00
if pdialog:
2016-01-23 22:05:56 +11:00
pdialog.update(heading="Processing %s / %s items" % (type, total))
2015-12-25 07:07:00 +11:00
count = 0
for embyitem in embyitems:
# Process individual item
2016-01-27 01:13:03 +11:00
if self.threadStopped():
2015-12-25 07:07:00 +11:00
return False
title = embyitem['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
process[type][1](embyitem)
else:
self.logMsg("%s finished." % type, 2)
return True
# Reserved for websocket_client.py and fast start
def triage_items(self, process, items):
processlist = {
'added': self.addedItems,
'update': self.updateItems,
'userdata': self.userdataItems,
'remove': self.removeItems
}
if items:
if process == "userdata":
itemids = []
for item in items:
itemids.append(item['ItemId'])
items = itemids
self.logMsg("Queue %s: %s" % (process, items), 1)
processlist[process].extend(items)
def incrementalSync(self):
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
kodiconn = utils.kodiSQL('video')
kodicursor = kodiconn.cursor()
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
pDialog = None
update_embydb = False
2015-12-25 07:07:00 +11:00
if self.refresh_views:
# Received userconfig update
self.refresh_views = False
self.maintainViews(embycursor, kodicursor)
self.forceLibraryUpdate = True
update_embydb = True
2015-12-25 07:07:00 +11:00
if self.addedItems or self.updateItems or self.userdataItems or self.removeItems:
# Only present dialog if we are going to process items
pDialog = self.progressDialog('Incremental sync')
process = {
'added': self.addedItems,
'update': self.updateItems,
'userdata': self.userdataItems,
'remove': self.removeItems
}
types = ['added', 'update', 'userdata', 'remove']
for type in types:
if process[type] and utils.window('emby_kodiScan') != "true":
listItems = list(process[type])
del process[type][:] # Reset class list
items_process = itemtypes.Items(embycursor, kodicursor)
update = False
# Prepare items according to process type
if type == "added":
items = emby.sortby_mediatype(listItems)
elif type in ("userdata", "remove"):
items = emby_db.sortby_mediaType(listItems, unsorted=False)
else:
items = emby_db.sortby_mediaType(listItems)
if items.get('Unsorted'):
sorted_items = emby.sortby_mediatype(items['Unsorted'])
doupdate = items_process.itemsbyId(sorted_items, "added", pDialog)
if doupdate:
embyupdate, kodiupdate_video = doupdate
if embyupdate:
update_embydb = True
if kodiupdate_video:
self.forceLibraryUpdate = True
2015-12-25 07:07:00 +11:00
del items['Unsorted']
doupdate = items_process.itemsbyId(items, type, pDialog)
if doupdate:
embyupdate, kodiupdate_video = doupdate
if embyupdate:
update_embydb = True
if kodiupdate_video:
self.forceLibraryUpdate = True
if update_embydb:
update_embydb = False
self.logMsg("Updating emby database.", 1)
embyconn.commit()
self.saveLastSync()
2015-12-25 07:07:00 +11:00
if self.forceLibraryUpdate:
# Force update the Kodi library
self.forceLibraryUpdate = False
self.dbCommit(kodiconn)
self.logMsg("Updating video library.", 1)
utils.window('emby_kodiScan', value="true")
xbmc.executebuiltin('UpdateLibrary(video)')
if pDialog:
pDialog.close()
kodicursor.close()
embycursor.close()
def compareDBVersion(self, current, minimum):
# It returns True is database is up to date. False otherwise.
self.logMsg("current: %s minimum: %s" % (current, minimum), 1)
currMajor, currMinor, currPatch = current.split(".")
minMajor, minMinor, minPatch = minimum.split(".")
if currMajor > minMajor:
return True
elif currMajor == minMajor and (currMinor > minMinor or
(currMinor == minMinor and currPatch >= minPatch)):
return True
else:
# Database out of date.
return False
def run(self):
try:
self.run_internal()
except Exception as e:
utils.window('emby_dbScan', clear=True)
2015-12-25 07:07:00 +11:00
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=("Library sync thread has exited! "
"You should restart Kodi now. "
"Please report this on the forum."))
2015-12-25 07:07:00 +11:00
raise
def run_internal(self):
startupComplete = False
2016-01-28 06:41:28 +11:00
self.views = []
count = 0
2015-12-25 07:07:00 +11:00
self.logMsg("---===### Starting LibrarySync ###===---", 0)
2016-01-27 01:13:03 +11:00
while not self.threadStopped():
2015-12-25 07:07:00 +11:00
# In the event the server goes offline
2016-01-27 01:13:03 +11:00
while self.threadSuspended():
2015-12-25 07:07:00 +11:00
# Set in service.py
2016-01-27 01:13:03 +11:00
if self.threadStopped():
2015-12-25 07:07:00 +11:00
# Abort was requested while waiting. We should exit
2016-01-28 06:41:28 +11:00
return
2016-01-27 01:13:03 +11:00
xbmc.sleep(3000)
2015-12-25 07:07:00 +11:00
if (utils.window('emby_dbCheck') != "true" and
2016-01-28 06:41:28 +11:00
self.installSyncDone):
2015-12-25 07:07:00 +11:00
# Verify the validity of the database
currentVersion = utils.settings('dbCreatedWithVersion')
minVersion = utils.window('emby_minDBVersion')
uptoDate = self.compareDBVersion(currentVersion, minVersion)
if not uptoDate:
self.logMsg(
"Db version out of date: %s minimum version required: %s"
% (currentVersion, minVersion), 0)
resp = xbmcgui.Dialog().yesno(
heading="Db Version",
line1=("Detected the database needs to be recreated "
"for this version of " + self.addonName +
"Proceed?"))
2015-12-25 07:07:00 +11:00
if not resp:
self.logMsg("Db version out of date! USER IGNORED!", 0)
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=(self.addonName + " may not work correctly "
"until the database is reset."))
2015-12-25 07:07:00 +11:00
else:
utils.reset()
utils.window('emby_dbCheck', value="true")
if not startupComplete:
2016-01-28 06:41:28 +11:00
# Also runs when installed first
2015-12-25 07:07:00 +11:00
# Verify the video database can be found
videoDb = utils.getKodiVideoDBPath()
if not xbmcvfs.exists(videoDb):
# Database does not exists
self.logMsg(
"The current Kodi version is incompatible "
"with the" + self.addonName + " add-on. Please visit "
"https://github.com/croneter/PlexKodiConnect "
"to know which Kodi versions are supported.", 0)
2015-12-25 07:07:00 +11:00
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=("Cancelling the database syncing process. "
"Current Kodi version: %s is unsupported. "
"Please verify your logs for more info."
% xbmc.getInfoLabel('System.BuildVersion')))
2015-12-25 07:07:00 +11:00
break
# Run start up sync
2016-01-28 06:41:28 +11:00
utils.window('emby_dbScan', value="true")
2015-12-25 07:07:00 +11:00
self.logMsg("Db version: %s" % utils.settings('dbCreatedWithVersion'), 0)
self.logMsg("SyncDatabase (started)", 1)
startTime = datetime.now()
2016-01-28 06:41:28 +11:00
librarySync = self.fullSync(manualrun=True)
2015-12-25 07:07:00 +11:00
elapsedTime = datetime.now() - startTime
self.logMsg(
"SyncDatabase (finished in: %s) %s"
% (str(elapsedTime).split('.')[0], librarySync), 1)
# Only try the initial sync once per kodi session regardless
# This will prevent an infinite loop in case something goes wrong.
startupComplete = True
2016-01-28 06:41:28 +11:00
utils.settings(
'SyncInstallRunDone', value="true")
utils.settings(
"dbCreatedWithVersion", self.clientInfo.getVersion())
self.installSyncDone = True
utils.window('emby_dbScan', value="false")
# Currently no db scan, so we can start a new scan
elif utils.window('emby_dbScan') != "true":
# Full scan was requested from somewhere else, e.g. userclient
2016-01-27 22:18:54 +11:00
if utils.window('plex_runLibScan') == "true":
self.logMsg('Full library scan requested, starting', 1)
2016-01-28 06:41:28 +11:00
utils.window('emby_dbScan', value="true")
2016-01-27 22:18:54 +11:00
utils.window('plex_runLibScan', value='false')
self.fullSync(manualrun=True)
2016-01-28 06:41:28 +11:00
utils.window('emby_dbScan', value="false")
count = 0
2016-01-27 22:18:54 +11:00
else:
2016-01-28 06:41:28 +11:00
# Run full lib scan approx every 10min
if count % 600 == 0:
utils.window('emby_dbScan', value="true")
2016-02-04 00:44:11 +11:00
self.logMsg('Running maintainViews() scan', 1)
2016-01-28 06:41:28 +11:00
self.fullSync(manualrun=True)
count = 0
2016-02-04 00:44:11 +11:00
utils.window('emby_dbScan', value="false")
# Update views / PMS libraries approx. every 5
2016-01-28 06:41:28 +11:00
elif count % 120 == 0:
self.logMsg('Running maintainViews() scan', 1)
utils.window('emby_dbScan', value="true")
self.maintainViews()
self.startSync()
2016-02-04 00:44:11 +11:00
# Run fast sync otherwise (ever second or so)
else:
self.startSync()
2015-12-25 07:07:00 +11:00
2016-01-27 22:18:54 +11:00
xbmc.sleep(1000)
2016-01-28 06:41:28 +11:00
count += 1
2015-12-25 07:07:00 +11:00
self.logMsg("###===--- LibrarySync Stopped ---===###", 0)
class ManualSync(LibrarySync):
def __init__(self):
LibrarySync.__init__(self)
self.fullSync(manualrun=True)
def movies(self, embycursor, kodicursor, pdialog):
# Get movies from emby
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
movies = itemtypes.Movies(embycursor, kodicursor)
views = emby_db.getView_byType('movies')
views += emby_db.getView_byType('mixed')
self.logMsg("Media folders: %s" % views, 1)
# Pull the list of movies and boxsets in Kodi
try:
all_kodimovies = dict(emby_db.getChecksum('Movie'))
except ValueError:
all_kodimovies = {}
try:
all_kodisets = dict(emby_db.getChecksum('BoxSet'))
except ValueError:
all_kodisets = {}
all_embymoviesIds = set()
all_embyboxsetsIds = set()
updatelist = []
##### PROCESS MOVIES #####
for view in views:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
return False
# Get items per view
viewId = view['id']
viewName = view['name']
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Comparing movies from view: %s..." % viewName)
all_embymovies = emby.getMovies(viewId, basic=True, dialog=pdialog)
for embymovie in all_embymovies['Items']:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
return False
API = api.API(embymovie)
itemid = embymovie['Id']
all_embymoviesIds.add(itemid)
if all_kodimovies.get(itemid) != API.getChecksum():
# Only update if movie is not in Kodi or checksum is different
updatelist.append(itemid)
self.logMsg("Movies to update for %s: %s" % (viewName, updatelist), 1)
embymovies = emby.getFullItems(updatelist)
total = len(updatelist)
del updatelist[:]
if pdialog:
pdialog.update(heading="Processing %s / %s items" % (viewName, total))
count = 0
for embymovie in embymovies:
# Process individual movies
2016-01-27 01:13:03 +11:00
if self.threadStopped():
return False
title = embymovie['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
movies.add_update(embymovie, viewName, viewId)
##### PROCESS BOXSETS #####
boxsets = emby.getBoxset(dialog=pdialog)
embyboxsets = []
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Comparing boxsets...")
for boxset in boxsets['Items']:
2016-01-27 01:13:03 +11:00
if self.threadStopped():
return False
# Boxset has no real userdata, so using etag to compare
checksum = boxset['Etag']
itemid = boxset['Id']
all_embyboxsetsIds.add(itemid)
if all_kodisets.get(itemid) != checksum:
# Only update if boxset is not in Kodi or checksum is different
updatelist.append(itemid)
embyboxsets.append(boxset)
self.logMsg("Boxsets to update: %s" % updatelist, 1)
total = len(updatelist)
if pdialog:
pdialog.update(heading="Processing Boxsets / %s items" % total)
count = 0
for boxset in embyboxsets:
# Process individual boxset
if self.shouldStop():
return False
title = boxset['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
movies.add_updateBoxset(boxset)
##### PROCESS DELETES #####
for kodimovie in all_kodimovies:
if kodimovie not in all_embymoviesIds:
movies.remove(kodimovie)
else:
self.logMsg("Movies compare finished.", 1)
for boxset in all_kodisets:
if boxset not in all_embyboxsetsIds:
movies.remove(boxset)
else:
self.logMsg("Boxsets compare finished.", 1)
return True
def musicvideos(self, embycursor, kodicursor, pdialog):
# Get musicvideos from emby
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
mvideos = itemtypes.MusicVideos(embycursor, kodicursor)
views = emby_db.getView_byType('musicvideos')
self.logMsg("Media folders: %s" % views, 1)
# Pull the list of musicvideos in Kodi
try:
all_kodimvideos = dict(emby_db.getChecksum('MusicVideo'))
except ValueError:
all_kodimvideos = {}
all_embymvideosIds = set()
updatelist = []
for view in views:
if self.shouldStop():
return False
# Get items per view
viewId = view['id']
viewName = view['name']
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Comparing musicvideos from view: %s..." % viewName)
all_embymvideos = emby.getMusicVideos(viewId, basic=True, dialog=pdialog)
for embymvideo in all_embymvideos['Items']:
if self.shouldStop():
return False
API = api.API(embymvideo)
itemid = embymvideo['Id']
all_embymvideosIds.add(itemid)
if all_kodimvideos.get(itemid) != API.getChecksum():
# Only update if musicvideo is not in Kodi or checksum is different
updatelist.append(itemid)
self.logMsg("MusicVideos to update for %s: %s" % (viewName, updatelist), 1)
embymvideos = emby.getFullItems(updatelist)
total = len(updatelist)
del updatelist[:]
if pdialog:
pdialog.update(heading="Processing %s / %s items" % (viewName, total))
count = 0
for embymvideo in embymvideos:
# Process individual musicvideo
if self.shouldStop():
return False
title = embymvideo['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
mvideos.add_update(embymvideo, viewName, viewId)
##### PROCESS DELETES #####
for kodimvideo in all_kodimvideos:
if kodimvideo not in all_embymvideosIds:
mvideos.remove(kodimvideo)
else:
self.logMsg("MusicVideos compare finished.", 1)
return True
def tvshows(self, embycursor, kodicursor, pdialog):
# Get shows from emby
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
tvshows = itemtypes.TVShows(embycursor, kodicursor)
views = emby_db.getView_byType('tvshows')
views += emby_db.getView_byType('mixed')
self.logMsg("Media folders: %s" % views, 1)
# Pull the list of tvshows and episodes in Kodi
try:
all_koditvshows = dict(emby_db.getChecksum('Series'))
except ValueError:
all_koditvshows = {}
try:
all_kodiepisodes = dict(emby_db.getChecksum('Episode'))
except ValueError:
all_kodiepisodes = {}
all_embytvshowsIds = set()
all_embyepisodesIds = set()
updatelist = []
for view in views:
if self.shouldStop():
return False
# Get items per view
viewId = view['id']
viewName = view['name']
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Comparing tvshows from view: %s..." % viewName)
all_embytvshows = emby.getShows(viewId, basic=True, dialog=pdialog)
for embytvshow in all_embytvshows['Items']:
if self.shouldStop():
return False
API = api.API(embytvshow)
itemid = embytvshow['Id']
all_embytvshowsIds.add(itemid)
if all_koditvshows.get(itemid) != API.getChecksum():
# Only update if movie is not in Kodi or checksum is different
updatelist.append(itemid)
self.logMsg("TVShows to update for %s: %s" % (viewName, updatelist), 1)
embytvshows = emby.getFullItems(updatelist)
total = len(updatelist)
del updatelist[:]
if pdialog:
pdialog.update(heading="Processing %s / %s items" % (viewName, total))
count = 0
for embytvshow in embytvshows:
# Process individual show
if self.shouldStop():
return False
itemid = embytvshow['Id']
title = embytvshow['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
tvshows.add_update(embytvshow, viewName, viewId)
else:
# Get all episodes in view
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Comparing episodes from view: %s..." % viewName)
all_embyepisodes = emby.getEpisodes(viewId, basic=True, dialog=pdialog)
for embyepisode in all_embyepisodes['Items']:
if self.shouldStop():
return False
API = api.API(embyepisode)
itemid = embyepisode['Id']
all_embyepisodesIds.add(itemid)
if all_kodiepisodes.get(itemid) != API.getChecksum():
# Only update if movie is not in Kodi or checksum is different
updatelist.append(itemid)
self.logMsg("Episodes to update for %s: %s" % (viewName, updatelist), 1)
embyepisodes = emby.getFullItems(updatelist)
total = len(updatelist)
del updatelist[:]
count = 0
for episode in embyepisodes:
# Process individual episode
if self.shouldStop():
return False
title = episode['SeriesName']
episodetitle = episode['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message="%s - %s" % (title, episodetitle))
count += 1
tvshows.add_updateEpisode(episode)
##### PROCESS DELETES #####
for koditvshow in all_koditvshows:
if koditvshow not in all_embytvshowsIds:
tvshows.remove(koditvshow)
else:
self.logMsg("TVShows compare finished.", 1)
for kodiepisode in all_kodiepisodes:
if kodiepisode not in all_embyepisodesIds:
tvshows.remove(kodiepisode)
else:
self.logMsg("Episodes compare finished.", 1)
return True
def music(self, embycursor, kodicursor, pdialog):
# Get music from emby
emby = self.emby
emby_db = embydb.Embydb_Functions(embycursor)
music = itemtypes.Music(embycursor, kodicursor)
# Pull the list of artists, albums, songs
try:
all_kodiartists = dict(emby_db.getChecksum('MusicArtist'))
except ValueError:
all_kodiartists = {}
try:
all_kodialbums = dict(emby_db.getChecksum('MusicAlbum'))
except ValueError:
all_kodialbums = {}
try:
all_kodisongs = dict(emby_db.getChecksum('Audio'))
except ValueError:
all_kodisongs = {}
all_embyartistsIds = set()
all_embyalbumsIds = set()
all_embysongsIds = set()
updatelist = []
process = {
'artists': [emby.getArtists, music.add_updateArtist],
'albums': [emby.getAlbums, music.add_updateAlbum],
'songs': [emby.getSongs, music.add_updateSong]
}
types = ['artists', 'albums', 'songs']
for type in types:
if pdialog:
pdialog.update(
heading="Emby for Kodi",
message="Comparing %s..." % type)
if type != "artists":
all_embyitems = process[type][0](basic=True, dialog=pdialog)
else:
all_embyitems = process[type][0](dialog=pdialog)
for embyitem in all_embyitems['Items']:
if self.shouldStop():
return False
API = api.API(embyitem)
itemid = embyitem['Id']
if type == "artists":
all_embyartistsIds.add(itemid)
if all_kodiartists.get(itemid) != API.getChecksum():
# Only update if artist is not in Kodi or checksum is different
updatelist.append(itemid)
elif type == "albums":
all_embyalbumsIds.add(itemid)
if all_kodialbums.get(itemid) != API.getChecksum():
# Only update if album is not in Kodi or checksum is different
updatelist.append(itemid)
else:
all_embysongsIds.add(itemid)
if all_kodisongs.get(itemid) != API.getChecksum():
# Only update if songs is not in Kodi or checksum is different
updatelist.append(itemid)
2016-02-04 21:06:28 +11:00
self.logMsg("%s to update: %s" % (type, updatelist), 1)
embyitems = emby.getFullItems(updatelist)
total = len(updatelist)
del updatelist[:]
if pdialog:
pdialog.update(heading="Processing %s / %s items" % (type, total))
count = 0
for embyitem in embyitems:
# Process individual item
if self.shouldStop():
return False
title = embyitem['Name']
if pdialog:
percentage = int((float(count) / float(total))*100)
pdialog.update(percentage, message=title)
count += 1
process[type][1](embyitem)
##### PROCESS DELETES #####
for kodiartist in all_kodiartists:
if kodiartist not in all_embyartistsIds and all_kodiartists[kodiartist] is not None:
music.remove(kodiartist)
else:
self.logMsg("Artist compare finished.", 1)
for kodialbum in all_kodialbums:
if kodialbum not in all_embyalbumsIds:
music.remove(kodialbum)
else:
self.logMsg("Albums compare finished.", 1)
for kodisong in all_kodisongs:
if kodisong not in all_embysongsIds:
music.remove(kodisong)
else:
self.logMsg("Songs compare finished.", 1)
return True