2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
import logging
|
2016-02-12 00:03:04 +11:00
|
|
|
from threading import Thread, Lock
|
2016-01-23 01:37:20 +11:00
|
|
|
import Queue
|
2016-09-11 03:49:03 +10:00
|
|
|
from random import shuffle
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcvfs
|
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
from utils import window, settings, getUnixTimestamp, kodiSQL, sourcesXML,\
|
|
|
|
ThreadMethods, ThreadMethodsAdditionalStop, LogTime, getScreensaver,\
|
|
|
|
setScreensaver, playlistXSP, language as lang, DateToKodi, reset,\
|
|
|
|
advancedSettingsXML, getKodiVideoDBPath, tryDecode, deletePlaylists,\
|
2016-09-05 00:43:39 +10:00
|
|
|
deleteNodes, ThreadMethodsAdditionalSuspend
|
2015-12-25 07:07:00 +11:00
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
|
|
|
import itemtypes
|
|
|
|
import embydb_functions as embydb
|
|
|
|
import kodidb_functions as kodidb
|
|
|
|
import userclient
|
|
|
|
import videonodes
|
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
import PlexFunctions as PF
|
2016-03-28 01:57:35 +11:00
|
|
|
import PlexAPI
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
addonName = 'PlexKodiConnect'
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
@ThreadMethodsAdditionalStop('suspend_LibraryThread')
|
|
|
|
@ThreadMethods
|
2016-02-12 00:03:04 +11:00
|
|
|
class ThreadedGetMetadata(Thread):
|
2016-01-08 03:27:48 +11:00
|
|
|
"""
|
|
|
|
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
|
2016-02-12 00:03:04 +11:00
|
|
|
out_queue Queue() object where this thread will store
|
2016-01-08 03:27:48 +11:00
|
|
|
the downloaded metadata XMLs as etree objects
|
2016-02-12 00:03:04 +11:00
|
|
|
lock Lock(), used for counting where we are
|
2016-01-08 03:27:48 +11:00
|
|
|
"""
|
2016-03-09 03:41:07 +11:00
|
|
|
def __init__(self, queue, out_queue, lock, processlock):
|
2016-01-08 03:27:48 +11:00
|
|
|
self.queue = queue
|
|
|
|
self.out_queue = out_queue
|
|
|
|
self.lock = lock
|
2016-03-09 03:41:07 +11:00
|
|
|
self.processlock = processlock
|
2016-02-12 00:03:04 +11:00
|
|
|
Thread.__init__(self)
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-04-08 17:11:03 +10:00
|
|
|
def terminateNow(self):
|
|
|
|
while not self.queue.empty():
|
|
|
|
# Still try because remaining item might have been taken
|
|
|
|
try:
|
|
|
|
self.queue.get(block=False)
|
|
|
|
except Queue.Empty:
|
2016-09-10 19:27:27 +10:00
|
|
|
xbmc.sleep(10)
|
2016-04-08 17:11:03 +10:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.queue.task_done()
|
2016-04-11 17:21:40 +10:00
|
|
|
if self.threadStopped():
|
|
|
|
# Shutdown from outside requested; purge out_queue as well
|
2016-04-10 00:57:45 +10:00
|
|
|
while not self.out_queue.empty():
|
|
|
|
# Still try because remaining item might have been taken
|
|
|
|
try:
|
|
|
|
self.out_queue.get(block=False)
|
|
|
|
except Queue.Empty:
|
2016-09-10 19:27:27 +10:00
|
|
|
xbmc.sleep(10)
|
2016-04-10 00:57:45 +10:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.out_queue.task_done()
|
2016-04-08 17:11:03 +10:00
|
|
|
|
2016-01-23 01:37:20 +11:00
|
|
|
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
|
2016-03-09 03:41:07 +11:00
|
|
|
processlock = self.processlock
|
2016-01-28 01:14:30 +11:00
|
|
|
threadStopped = self.threadStopped
|
2016-01-08 03:27:48 +11:00
|
|
|
global getMetadataCount
|
2016-03-09 03:41:07 +11:00
|
|
|
global processMetadataCount
|
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-09-10 19:27:27 +10:00
|
|
|
xbmc.sleep(10)
|
2016-01-30 18:43:28 +11:00
|
|
|
continue
|
|
|
|
# Download Metadata
|
2016-03-25 04:52:02 +11:00
|
|
|
plexXML = PF.GetPlexMetadata(updateItem['itemId'])
|
2016-02-07 22:38:50 +11:00
|
|
|
if plexXML is None:
|
2016-02-01 20:33:33 +11:00
|
|
|
# Did not receive a valid XML - skip that item for now
|
2016-09-02 03:07:28 +10:00
|
|
|
log.warn("Could not get metadata for %s. Skipping that item "
|
|
|
|
"for now" % updateItem['itemId'])
|
2016-03-09 03:41:07 +11:00
|
|
|
# Increase BOTH counters - since metadata won't be processed
|
2016-03-09 01:28:59 +11:00
|
|
|
with lock:
|
|
|
|
getMetadataCount += 1
|
2016-03-09 03:41:07 +11:00
|
|
|
with processlock:
|
|
|
|
processMetadataCount += 1
|
2016-01-30 06:07:21 +11:00
|
|
|
queue.task_done()
|
2016-01-30 18:43:28 +11:00
|
|
|
continue
|
2016-04-08 02:29:23 +10:00
|
|
|
elif plexXML == 401:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.warn('HTTP 401 returned by PMS. Too much strain? '
|
|
|
|
'Cancelling sync for now')
|
|
|
|
window('plex_scancrashed', value='401')
|
2016-04-08 02:29:23 +10:00
|
|
|
# Kill remaining items in queue (for main thread to cont.)
|
|
|
|
queue.task_done()
|
|
|
|
break
|
2016-01-30 18:43:28 +11:00
|
|
|
|
|
|
|
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-04-08 17:11:03 +10:00
|
|
|
# Empty queue in case PKC was shut down (main thread hangs otherwise)
|
|
|
|
self.terminateNow()
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Download thread terminated')
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@ThreadMethodsAdditionalStop('suspend_LibraryThread')
|
|
|
|
@ThreadMethods
|
2016-02-12 00:03:04 +11:00
|
|
|
class ThreadedProcessMetadata(Thread):
|
2016-01-08 03:27:48 +11:00
|
|
|
"""
|
|
|
|
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()
|
2016-02-12 00:03:04 +11:00
|
|
|
lock: Lock(), used for counting where we are
|
2016-01-08 03:27:48 +11:00
|
|
|
"""
|
2016-01-30 18:43:28 +11:00
|
|
|
def __init__(self, queue, itemType, lock):
|
2016-01-08 03:27:48 +11:00
|
|
|
self.queue = queue
|
|
|
|
self.lock = lock
|
2016-01-11 17:55:22 +11:00
|
|
|
self.itemType = itemType
|
2016-02-12 00:03:04 +11:00
|
|
|
Thread.__init__(self)
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-04-08 17:11:03 +10:00
|
|
|
def terminateNow(self):
|
|
|
|
while not self.queue.empty():
|
|
|
|
# Still try because remaining item might have been taken
|
|
|
|
try:
|
|
|
|
self.queue.get(block=False)
|
|
|
|
except Queue.Empty:
|
2016-09-10 19:27:27 +10:00
|
|
|
xbmc.sleep(10)
|
2016-04-08 17:11:03 +10:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.queue.task_done()
|
|
|
|
|
2016-01-23 01:37:20 +11:00
|
|
|
def run(self):
|
2016-01-08 03:27:48 +11:00
|
|
|
# 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
|
2016-01-08 03:27:48 +11:00
|
|
|
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-09-10 19:27:27 +10:00
|
|
|
xbmc.sleep(10)
|
2016-01-30 18:43:28 +11:00
|
|
|
continue
|
2016-03-09 03:41:07 +11:00
|
|
|
# Do the work
|
2016-01-30 18:43:28 +11:00
|
|
|
plexitem = updateItem['XML']
|
|
|
|
method = updateItem['method']
|
|
|
|
viewName = updateItem['viewName']
|
|
|
|
viewId = updateItem['viewId']
|
|
|
|
title = updateItem['title']
|
|
|
|
itemSubFkt = getattr(item, method)
|
2016-03-09 03:41:07 +11:00
|
|
|
# Get the one child entry in the xml and process
|
|
|
|
for child in plexitem:
|
|
|
|
itemSubFkt(child,
|
|
|
|
viewtag=viewName,
|
|
|
|
viewid=viewId)
|
|
|
|
# Keep track of where we are at
|
2016-01-30 18:43:28 +11:00
|
|
|
with lock:
|
|
|
|
processMetadataCount += 1
|
|
|
|
processingViewName = title
|
|
|
|
# signals to queue job is done
|
2016-02-10 20:04:49 +11:00
|
|
|
queue.task_done()
|
2016-04-08 17:11:03 +10:00
|
|
|
# Empty queue in case PKC was shut down (main thread hangs otherwise)
|
|
|
|
self.terminateNow()
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Processing thread terminated')
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@ThreadMethodsAdditionalStop('suspend_LibraryThread')
|
|
|
|
@ThreadMethods
|
2016-02-12 00:03:04 +11:00
|
|
|
class ThreadedShowSyncInfo(Thread):
|
2016-01-08 03:27:48 +11:00
|
|
|
"""
|
|
|
|
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):
|
2016-01-08 03:27:48 +11:00
|
|
|
self.locks = locks
|
|
|
|
self.total = total
|
|
|
|
self.dialog = dialog
|
2016-01-11 17:55:22 +11:00
|
|
|
self.itemType = itemType
|
2016-02-12 00:03:04 +11:00
|
|
|
Thread.__init__(self)
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
def run(self):
|
2016-01-28 01:14:30 +11:00
|
|
|
# cache local variables because it's faster
|
2016-01-08 03:27:48 +11:00
|
|
|
total = self.total
|
2016-01-28 01:14:30 +11:00
|
|
|
dialog = self.dialog
|
|
|
|
threadStopped = self.threadStopped
|
2016-01-08 03:27:48 +11:00
|
|
|
downloadLock = self.locks[0]
|
|
|
|
processLock = self.locks[1]
|
2016-03-08 22:13:47 +11:00
|
|
|
dialog.create("%s: Sync %s: %s items"
|
2016-09-02 03:07:28 +10:00
|
|
|
% (addonName, self.itemType, str(total)),
|
2016-01-28 01:14:30 +11:00
|
|
|
"Starting")
|
2016-01-08 03:27:48 +11:00
|
|
|
global getMetadataCount
|
|
|
|
global processMetadataCount
|
2016-01-11 17:55:22 +11:00
|
|
|
global processingViewName
|
2016-01-08 03:27:48 +11:00
|
|
|
total = 2 * total
|
|
|
|
totalProgress = 0
|
2016-01-28 01:14:30 +11:00
|
|
|
while threadStopped() is False:
|
2016-01-08 03:27:48 +11:00
|
|
|
with downloadLock:
|
|
|
|
getMetadataProgress = getMetadataCount
|
2016-01-11 17:55:22 +11:00
|
|
|
with processLock:
|
|
|
|
processMetadataProgress = processMetadataCount
|
|
|
|
viewName = processingViewName
|
2016-01-08 03:27:48 +11:00
|
|
|
totalProgress = getMetadataProgress + processMetadataProgress
|
2016-01-08 06:31:25 +11:00
|
|
|
try:
|
|
|
|
percentage = int(float(totalProgress) / float(total)*100.0)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
percentage = 0
|
2016-03-08 22:13:47 +11:00
|
|
|
dialog.update(percentage,
|
2016-09-02 03:07:28 +10:00
|
|
|
message="%s downloaded. %s processed: %s"
|
2016-03-08 22:13:47 +11:00
|
|
|
% (getMetadataProgress,
|
|
|
|
processMetadataProgress,
|
|
|
|
viewName))
|
2016-01-24 01:53:24 +11:00
|
|
|
# Sleep for x milliseconds
|
2016-09-10 19:27:27 +10:00
|
|
|
xbmc.sleep(200)
|
2016-01-28 01:14:30 +11:00
|
|
|
dialog.close()
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Dialog Infobox thread terminated')
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
|
2016-09-11 03:49:03 +10:00
|
|
|
@ThreadMethodsAdditionalSuspend('suspend_LibraryThread')
|
|
|
|
@ThreadMethodsAdditionalStop('plex_shouldStop')
|
|
|
|
@ThreadMethods
|
|
|
|
class ProcessFanartThread(Thread):
|
|
|
|
"""
|
|
|
|
Threaded download of additional fanart in the background
|
|
|
|
|
|
|
|
Input:
|
|
|
|
queue Queue.Queue() object that you will need to fill with
|
|
|
|
dicts of the following form:
|
|
|
|
{
|
|
|
|
'itemId': the Plex id as a string
|
|
|
|
'class': the itemtypes class, e.g. 'Movies'
|
|
|
|
'mediaType': the kodi media type, e.g. 'movie'
|
|
|
|
'refresh': True/False if true, will overwrite any 3rd party
|
|
|
|
fanart. If False, will only get missing
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
def __init__(self, queue):
|
|
|
|
self.queue = queue
|
|
|
|
Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
threadStopped = self.threadStopped
|
|
|
|
threadSuspended = self.threadSuspended
|
2016-09-11 19:12:25 +10:00
|
|
|
queue = self.queue
|
2016-09-11 18:36:28 +10:00
|
|
|
log.info("---===### Starting FanartSync ###===---")
|
2016-09-11 03:49:03 +10:00
|
|
|
while not threadStopped():
|
|
|
|
# In the event the server goes offline
|
2016-09-17 17:48:38 +10:00
|
|
|
while threadSuspended() or window('plex_dbScan'):
|
2016-09-11 03:49:03 +10:00
|
|
|
# Set in service.py
|
|
|
|
if threadStopped():
|
|
|
|
# Abort was requested while waiting. We should exit
|
2016-09-11 18:36:28 +10:00
|
|
|
log.info("---===### Stopped FanartSync ###===---")
|
2016-09-11 18:14:29 +10:00
|
|
|
return
|
2016-09-11 03:49:03 +10:00
|
|
|
xbmc.sleep(1000)
|
|
|
|
# grabs Plex item from queue
|
|
|
|
try:
|
|
|
|
item = queue.get(block=False)
|
|
|
|
except Queue.Empty:
|
2016-09-12 01:51:00 +10:00
|
|
|
xbmc.sleep(200)
|
2016-09-11 03:49:03 +10:00
|
|
|
continue
|
|
|
|
if item['refresh'] is True:
|
|
|
|
# Leave the Plex art untouched
|
|
|
|
allartworks = None
|
|
|
|
else:
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
try:
|
|
|
|
kodiId = emby_db.getItem_byId(item['itemId'])[0]
|
|
|
|
except TypeError:
|
|
|
|
log.error('Could not get Kodi id for plex id %s'
|
|
|
|
% item['itemId'])
|
|
|
|
queue.task_done()
|
|
|
|
continue
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
|
|
|
allartworks = kodi_db.existingArt(kodiId,
|
|
|
|
item['mediaType'])
|
|
|
|
# Check if we even need to get additional art
|
|
|
|
needsupdate = False
|
|
|
|
for key, value in allartworks.iteritems():
|
|
|
|
if not value and not key == 'BoxRear':
|
|
|
|
needsupdate = True
|
2016-09-17 17:55:06 +10:00
|
|
|
break
|
2016-09-11 03:49:03 +10:00
|
|
|
if needsupdate is False:
|
|
|
|
log.debug('Already got all art for Plex id %s'
|
|
|
|
% item['itemId'])
|
|
|
|
queue.task_done()
|
|
|
|
continue
|
|
|
|
|
|
|
|
log.debug('Getting additional fanart for Plex id %s'
|
|
|
|
% item['itemId'])
|
|
|
|
# Download Metadata
|
|
|
|
xml = PF.GetPlexMetadata(item['itemId'])
|
|
|
|
if xml is None:
|
|
|
|
# Did not receive a valid XML - skip that item for now
|
|
|
|
log.warn("Could not get metadata for %s. Skipping that item "
|
|
|
|
"for now" % item['itemId'])
|
|
|
|
queue.task_done()
|
|
|
|
continue
|
|
|
|
elif xml == 401:
|
|
|
|
log.warn('HTTP 401 returned by PMS. Too much strain? '
|
|
|
|
'Cancelling sync for now')
|
|
|
|
# Kill remaining items in queue (for main thread to cont.)
|
|
|
|
queue.task_done()
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Do the work
|
|
|
|
with getattr(itemtypes, item['class'])() as cls:
|
|
|
|
cls.getfanart(xml[0], kodiId, item['mediaType'], allartworks)
|
|
|
|
# signals to queue job is done
|
|
|
|
log.debug('Done getting fanart for Plex id %s' % item['itemId'])
|
|
|
|
queue.task_done()
|
2016-09-11 18:36:28 +10:00
|
|
|
log.info("---===### Stopped FanartSync ###===---")
|
2016-09-11 03:49:03 +10:00
|
|
|
|
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@ThreadMethodsAdditionalSuspend('suspend_LibraryThread')
|
|
|
|
@ThreadMethodsAdditionalStop('plex_shouldStop')
|
|
|
|
@ThreadMethods
|
2016-02-12 00:03:04 +11:00
|
|
|
class LibrarySync(Thread):
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
|
|
|
librarysync.LibrarySync(queue)
|
|
|
|
|
|
|
|
where (communication with websockets)
|
|
|
|
queue: Queue object for background sync
|
|
|
|
"""
|
2016-02-11 22:56:57 +11:00
|
|
|
# Borg, even though it's planned to only have 1 instance up and running!
|
2015-12-25 07:07:00 +11:00
|
|
|
_shared_state = {}
|
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
def __init__(self, queue):
|
2015-12-25 07:07:00 +11:00
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
# Communication with websockets
|
|
|
|
self.queue = queue
|
|
|
|
self.itemsToProcess = []
|
2016-03-28 01:57:35 +11:00
|
|
|
self.sessionKeys = []
|
2016-09-11 18:20:29 +10:00
|
|
|
self.fanartqueue = Queue.Queue()
|
2016-09-11 03:49:03 +10:00
|
|
|
if settings('FanartTV') == 'true':
|
|
|
|
self.fanartthread = ProcessFanartThread(self.fanartqueue)
|
2016-03-28 01:57:35 +11:00
|
|
|
# How long should we wait at least to process new/changed PMS items?
|
2016-09-02 03:07:28 +10:00
|
|
|
self.saftyMargin = int(settings('saftyMargin'))
|
2016-03-25 04:52:02 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
self.fullSyncInterval = int(settings('fullSyncInterval')) * 60
|
2016-03-28 04:06:36 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
self.clientInfo = clientinfo.ClientInfo()
|
|
|
|
self.user = userclient.UserClient()
|
|
|
|
self.vnodes = videonodes.VideoNodes()
|
2016-04-07 23:33:01 +10:00
|
|
|
self.dialog = xbmcgui.Dialog()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-09-26 03:18:27 +10:00
|
|
|
self.syncThreadNumber = int(settings('syncThreadNumber'))
|
2016-09-02 03:07:28 +10:00
|
|
|
self.installSyncDone = settings('SyncInstallRunDone') == 'true'
|
|
|
|
self.showDbSync = settings('dbSyncIndicator') == 'true'
|
|
|
|
self.enableMusic = settings('enableMusic') == "true"
|
|
|
|
self.enableBackgroundSync = settings(
|
|
|
|
'enableBackgroundSync') == "true"
|
|
|
|
self.limitindex = int(settings('limitindex'))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-04-08 18:52:15 +10:00
|
|
|
# Just in case a time sync goes wrong
|
2016-09-02 03:07:28 +10:00
|
|
|
self.timeoffset = int(settings('kodiplextimeoffset'))
|
|
|
|
window('kodiplextimeoffset', value=str(self.timeoffset))
|
2016-02-12 00:03:04 +11:00
|
|
|
Thread.__init__(self)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-10 18:51:24 +11:00
|
|
|
def showKodiNote(self, message, forced=False, icon="plex"):
|
2016-02-12 00:44:11 +11:00
|
|
|
"""
|
2016-03-08 22:13:47 +11:00
|
|
|
Shows a Kodi popup, if user selected to do so. Pass message in unicode
|
|
|
|
or string
|
2016-03-10 18:51:24 +11:00
|
|
|
|
|
|
|
icon: "plex": shows Plex icon
|
|
|
|
"error": shows Kodi error icon
|
2016-04-07 23:33:01 +10:00
|
|
|
|
|
|
|
forced: always show popup, even if user setting to off
|
2016-02-12 00:44:11 +11:00
|
|
|
"""
|
2016-04-07 23:33:01 +10:00
|
|
|
if not self.showDbSync:
|
|
|
|
if not forced:
|
|
|
|
return
|
2016-03-10 18:51:24 +11:00
|
|
|
if icon == "plex":
|
2016-04-07 23:33:01 +10:00
|
|
|
self.dialog.notification(
|
2016-09-02 03:07:28 +10:00
|
|
|
addonName,
|
2016-04-07 23:33:01 +10:00
|
|
|
message,
|
|
|
|
"special://home/addons/plugin.video.plexkodiconnect/icon.png",
|
|
|
|
5000,
|
|
|
|
False)
|
2016-03-10 18:51:24 +11:00
|
|
|
elif icon == "error":
|
2016-04-07 23:33:01 +10:00
|
|
|
self.dialog.notification(
|
2016-09-02 03:07:28 +10:00
|
|
|
addonName,
|
2016-04-07 23:33:01 +10:00
|
|
|
message,
|
|
|
|
xbmcgui.NOTIFICATION_ERROR,
|
|
|
|
7000,
|
|
|
|
True)
|
2016-02-12 00:44:11 +11:00
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
def syncPMStime(self):
|
|
|
|
"""
|
|
|
|
PMS does not provide a means to get a server timestamp. This is a work-
|
|
|
|
around.
|
2016-03-28 01:57:35 +11:00
|
|
|
|
|
|
|
In general, everything saved to Kodi shall be in Kodi time.
|
|
|
|
|
|
|
|
Any info with a PMS timestamp is in Plex time, naturally
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Synching time with PMS server')
|
2016-03-12 00:42:14 +11:00
|
|
|
# Find a PMS item where we can toggle the view state to enforce a
|
|
|
|
# change in lastViewedAt
|
2016-04-08 18:52:15 +10:00
|
|
|
|
|
|
|
# Get all Plex libraries
|
|
|
|
sections = downloadutils.DownloadUtils().downloadUrl(
|
|
|
|
"{server}/library/sections")
|
|
|
|
try:
|
|
|
|
sections.attrib
|
|
|
|
except AttributeError:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Error download PMS views, abort syncPMStime")
|
2016-04-08 18:52:15 +10:00
|
|
|
return False
|
|
|
|
|
|
|
|
plexId = None
|
|
|
|
for mediatype in ('movie', 'show', 'artist'):
|
|
|
|
if plexId is not None:
|
|
|
|
break
|
|
|
|
for view in sections:
|
|
|
|
if plexId is not None:
|
2016-03-12 00:42:14 +11:00
|
|
|
break
|
2016-04-08 18:52:15 +10:00
|
|
|
if not view.attrib['type'] == mediatype:
|
|
|
|
continue
|
2016-04-17 21:44:45 +10:00
|
|
|
libraryId = view.attrib['key']
|
|
|
|
items = PF.GetAllPlexLeaves(libraryId,
|
2016-04-08 18:52:15 +10:00
|
|
|
containerSize=self.limitindex)
|
|
|
|
if items in (None, 401):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Could not download section %s"
|
|
|
|
% view.attrib['key'])
|
2016-04-08 18:52:15 +10:00
|
|
|
continue
|
|
|
|
for item in items:
|
|
|
|
if item.attrib.get('viewCount') is not None:
|
|
|
|
# Don't want to mess with items that have playcount>0
|
|
|
|
continue
|
|
|
|
if item.attrib.get('viewOffset') is not None:
|
|
|
|
# Don't mess with items with a resume point
|
|
|
|
continue
|
|
|
|
plexId = item.attrib.get('ratingKey')
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Found an item to sync with: %s' % plexId)
|
2016-04-08 18:52:15 +10:00
|
|
|
break
|
|
|
|
|
|
|
|
if plexId is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Could not find an item to sync time with")
|
|
|
|
log.error("Aborting PMS-Kodi time sync")
|
2016-04-08 18:52:15 +10:00
|
|
|
return False
|
2016-03-12 00:42:14 +11:00
|
|
|
|
|
|
|
# Get the Plex item's metadata
|
2016-03-25 04:52:02 +11:00
|
|
|
xml = PF.GetPlexMetadata(plexId)
|
2016-04-08 18:52:15 +10:00
|
|
|
if xml in (None, 401):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Could not download metadata, aborting time sync")
|
2016-04-08 18:52:15 +10:00
|
|
|
return False
|
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
timestamp = xml[0].attrib.get('lastViewedAt')
|
2016-04-08 18:52:15 +10:00
|
|
|
if timestamp is None:
|
2016-03-12 00:42:14 +11:00
|
|
|
timestamp = xml[0].attrib.get('updatedAt')
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Using items updatedAt=%s' % timestamp)
|
2016-04-08 18:52:15 +10:00
|
|
|
if timestamp is None:
|
2016-03-12 00:42:14 +11:00
|
|
|
timestamp = xml[0].attrib.get('addedAt')
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Using items addedAt=%s' % timestamp)
|
2016-04-08 18:52:15 +10:00
|
|
|
if timestamp is None:
|
|
|
|
timestamp = 0
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('No timestamp; using 0')
|
2016-04-08 18:52:15 +10:00
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
# Set the timer
|
2016-09-02 03:07:28 +10:00
|
|
|
koditime = getUnixTimestamp()
|
2016-03-12 00:42:14 +11:00
|
|
|
# Toggle watched state
|
2016-03-25 04:52:02 +11:00
|
|
|
PF.scrobble(plexId, 'watched')
|
2016-03-12 00:42:14 +11:00
|
|
|
# Let the PMS process this first!
|
2016-04-08 18:52:15 +10:00
|
|
|
xbmc.sleep(1000)
|
|
|
|
# Get PMS items to find the item we just changed
|
2016-03-25 04:52:02 +11:00
|
|
|
items = PF.GetAllPlexLeaves(libraryId,
|
|
|
|
lastViewedAt=timestamp,
|
|
|
|
containerSize=self.limitindex)
|
2016-03-12 00:42:14 +11:00
|
|
|
# Toggle watched state back
|
2016-03-25 04:52:02 +11:00
|
|
|
PF.scrobble(plexId, 'unwatched')
|
2016-04-08 17:11:03 +10:00
|
|
|
if items in (None, 401):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Could not download metadata, aborting time sync")
|
2016-04-08 18:52:15 +10:00
|
|
|
return False
|
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
plextime = None
|
|
|
|
for item in items:
|
|
|
|
if item.attrib['ratingKey'] == plexId:
|
|
|
|
plextime = item.attrib.get('lastViewedAt')
|
|
|
|
break
|
2016-04-08 18:52:15 +10:00
|
|
|
|
2016-04-08 17:11:03 +10:00
|
|
|
if plextime is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error('Could not get lastViewedAt - aborting')
|
2016-04-08 18:52:15 +10:00
|
|
|
return False
|
2016-03-12 00:42:14 +11:00
|
|
|
|
|
|
|
# Calculate time offset Kodi-PMS
|
2016-04-08 18:52:15 +10:00
|
|
|
self.timeoffset = int(koditime) - int(plextime)
|
2016-09-02 03:07:28 +10:00
|
|
|
window('kodiplextimeoffset', value=str(self.timeoffset))
|
|
|
|
settings('kodiplextimeoffset', value=str(self.timeoffset))
|
|
|
|
log.info("Time offset Koditime - Plextime in seconds: %s"
|
|
|
|
% str(self.timeoffset))
|
2016-04-08 18:52:15 +10:00
|
|
|
return True
|
2016-03-12 00:42:14 +11:00
|
|
|
|
2016-01-08 03:27:48 +11:00
|
|
|
def initializeDBs(self):
|
2016-01-08 06:31:25 +11:00
|
|
|
"""
|
|
|
|
Run once during startup to verify that emby db exists.
|
|
|
|
"""
|
2016-09-02 03:07:28 +10:00
|
|
|
embyconn = kodiSQL('emby')
|
2015-12-25 07:07:00 +11:00
|
|
|
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()
|
2016-03-01 22:10:09 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
# content sync: movies, tvshows, musicvideos, music
|
2016-01-08 03:27:48 +11:00
|
|
|
embyconn.close()
|
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@LogTime
|
2016-04-07 19:57:34 +10:00
|
|
|
def fullSync(self, repair=False):
|
|
|
|
"""
|
|
|
|
repair=True: force sync EVERY item
|
|
|
|
"""
|
2016-03-03 03:27:21 +11:00
|
|
|
# self.compare == False: we're syncing EVERY item
|
|
|
|
# True: we're syncing only the delta, e.g. different checksum
|
2016-04-07 19:57:34 +10:00
|
|
|
self.compare = not repair
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-03-02 02:52:09 +11:00
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(true)')
|
2016-09-02 03:07:28 +10:00
|
|
|
screensaver = getScreensaver()
|
|
|
|
setScreensaver(value="")
|
2016-03-02 02:52:09 +11:00
|
|
|
|
2016-01-08 03:27:48 +11:00
|
|
|
# Add sources
|
2016-09-02 03:07:28 +10:00
|
|
|
sourcesXML()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-03 03:27:21 +11:00
|
|
|
# Set views. Abort if unsuccessful
|
|
|
|
if not self.maintainViews():
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
2016-09-02 03:07:28 +10:00
|
|
|
setScreensaver(value=screensaver)
|
2016-03-03 03:27:21 +11:00
|
|
|
return False
|
2016-01-11 19:57:45 +11:00
|
|
|
|
2015-12-28 23:10:05 +11:00
|
|
|
process = {
|
2016-01-10 02:14:02 +11:00
|
|
|
'movies': self.PlexMovies,
|
2016-02-13 02:53:49 +11:00
|
|
|
'tvshows': self.PlexTVShows,
|
2015-12-28 23:10:05 +11:00
|
|
|
}
|
2016-02-13 02:53:49 +11:00
|
|
|
if self.enableMusic:
|
|
|
|
process['music'] = self.PlexMusic
|
2016-04-08 17:11:03 +10:00
|
|
|
|
|
|
|
# Do the processing
|
2015-12-25 07:07:00 +11:00
|
|
|
for itemtype in process:
|
2016-04-08 17:11:03 +10:00
|
|
|
if self.threadStopped():
|
|
|
|
return False
|
|
|
|
if not process[itemtype]():
|
2016-03-02 02:52:09 +11:00
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
2016-09-02 03:07:28 +10:00
|
|
|
setScreensaver(value=screensaver)
|
2015-12-25 07:07:00 +11:00
|
|
|
return False
|
|
|
|
|
2016-03-03 03:27:21 +11:00
|
|
|
# Let kodi update the views in any case, since we're doing a full sync
|
2015-12-25 07:07:00 +11:00
|
|
|
xbmc.executebuiltin('UpdateLibrary(video)')
|
2016-02-13 02:53:49 +11:00
|
|
|
if self.enableMusic:
|
|
|
|
xbmc.executebuiltin('UpdateLibrary(music)')
|
2016-02-12 00:44:11 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
window('plex_initialScan', clear=True)
|
2016-03-02 02:52:09 +11:00
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
2016-09-02 03:07:28 +10:00
|
|
|
setScreensaver(value=screensaver)
|
|
|
|
if window('plex_scancrashed') == 'true':
|
2016-04-08 02:29:23 +10:00
|
|
|
# Show warning if itemtypes.py crashed at some point
|
2016-09-02 03:07:28 +10:00
|
|
|
self.dialog.ok(addonName, lang(39408))
|
|
|
|
window('plex_scancrashed', clear=True)
|
|
|
|
elif window('plex_scancrashed') == '401':
|
|
|
|
window('plex_scancrashed', clear=True)
|
|
|
|
if window('plex_serverStatus') not in ('401', 'Auth'):
|
2016-04-08 02:29:23 +10:00
|
|
|
# Plex server had too much and returned ERROR
|
2016-09-02 03:07:28 +10:00
|
|
|
self.dialog.ok(addonName, lang(39409))
|
2016-04-07 21:49:05 +10:00
|
|
|
|
|
|
|
# Path hack, so Kodis Information screen works
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
2016-04-08 00:17:57 +10:00
|
|
|
try:
|
|
|
|
kodi_db.pathHack()
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Path hack successful')
|
2016-04-08 00:17:57 +10:00
|
|
|
except Exception as e:
|
|
|
|
# Empty movies, tv shows?
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error('Path hack failed with error message: %s' % str(e))
|
2015-12-25 07:07:00 +11:00
|
|
|
return True
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
def processView(self, folderItem, kodi_db, emby_db, totalnodes):
|
|
|
|
vnodes = self.vnodes
|
|
|
|
folder = folderItem.attrib
|
|
|
|
mediatype = folder['type']
|
|
|
|
# Only process supported formats
|
2016-06-05 02:48:22 +10:00
|
|
|
if mediatype not in ('movie', 'show', 'artist', 'photo'):
|
2016-03-03 03:27:21 +11:00
|
|
|
return totalnodes
|
|
|
|
|
|
|
|
# Prevent duplicate for nodes of the same type
|
|
|
|
nodes = self.nodes[mediatype]
|
|
|
|
# Prevent duplicate for playlists of the same type
|
|
|
|
playlists = self.playlists[mediatype]
|
2016-03-03 19:04:15 +11:00
|
|
|
sorted_views = self.sorted_views
|
2016-02-12 00:03:04 +11:00
|
|
|
|
|
|
|
folderid = folder['key']
|
|
|
|
foldername = folder['title']
|
|
|
|
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:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Creating viewid: %s in Plex database." % folderid)
|
2016-02-12 00:03:04 +11:00
|
|
|
tagid = kodi_db.createTag(foldername)
|
|
|
|
# Create playlist for the video library
|
2016-03-03 03:27:21 +11:00
|
|
|
if (foldername not in playlists and
|
|
|
|
mediatype in ('movie', 'show', 'musicvideos')):
|
2016-09-02 03:07:28 +10:00
|
|
|
playlistXSP(mediatype, foldername, folderid, viewtype)
|
2016-03-03 03:27:21 +11:00
|
|
|
playlists.append(foldername)
|
2016-02-12 00:03:04 +11:00
|
|
|
# Create the video node
|
2016-03-03 03:27:21 +11:00
|
|
|
if (foldername not in nodes and
|
|
|
|
mediatype not in ("musicvideos", "artist")):
|
|
|
|
vnodes.viewNode(sorted_views.index(foldername),
|
2016-02-12 00:03:04 +11:00
|
|
|
foldername,
|
|
|
|
mediatype,
|
2016-03-01 22:10:09 +11:00
|
|
|
viewtype,
|
|
|
|
folderid)
|
2016-03-03 03:27:21 +11:00
|
|
|
nodes.append(foldername)
|
2016-02-12 00:03:04 +11:00
|
|
|
totalnodes += 1
|
|
|
|
# Add view to emby database
|
|
|
|
emby_db.addView(folderid, foldername, viewtype, tagid)
|
|
|
|
else:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info(' '.join((
|
2016-02-12 00:03:04 +11:00
|
|
|
"Found viewid: %s" % folderid,
|
|
|
|
"viewname: %s" % current_viewname,
|
|
|
|
"viewtype: %s" % current_viewtype,
|
2016-09-02 03:07:28 +10:00
|
|
|
"tagid: %s" % current_tagid)))
|
2016-03-01 21:26:46 +11:00
|
|
|
|
|
|
|
# Remove views that are still valid to delete rest later
|
|
|
|
try:
|
|
|
|
self.old_views.remove(folderid)
|
|
|
|
except ValueError:
|
|
|
|
# View was just created, nothing to remove
|
|
|
|
pass
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
# View was modified, update with latest info
|
|
|
|
if current_viewname != foldername:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("viewid: %s new viewname: %s"
|
|
|
|
% (folderid, foldername))
|
2016-02-12 00:03:04 +11:00
|
|
|
tagid = kodi_db.createTag(foldername)
|
|
|
|
|
|
|
|
# Update view with new info
|
|
|
|
emby_db.updateView(foldername, tagid, folderid)
|
|
|
|
|
2016-03-03 03:27:21 +11:00
|
|
|
if mediatype != "artist":
|
2016-02-12 00:03:04 +11:00
|
|
|
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.
|
2016-09-02 03:07:28 +10:00
|
|
|
playlistXSP(mediatype,
|
|
|
|
current_viewname,
|
|
|
|
folderid,
|
|
|
|
current_viewtype,
|
|
|
|
True)
|
2016-02-12 00:03:04 +11:00
|
|
|
# Delete video node
|
|
|
|
if mediatype != "musicvideos":
|
2016-03-03 03:27:21 +11:00
|
|
|
vnodes.viewNode(
|
|
|
|
indexnumber=sorted_views.index(foldername),
|
|
|
|
tagname=current_viewname,
|
|
|
|
mediatype=mediatype,
|
|
|
|
viewtype=current_viewtype,
|
|
|
|
viewid=folderid,
|
|
|
|
delete=True)
|
2016-02-12 00:03:04 +11:00
|
|
|
# Added new playlist
|
2016-03-03 03:27:21 +11:00
|
|
|
if (foldername not in playlists and
|
|
|
|
mediatype in ('movie', 'show', 'musicvideos')):
|
2016-09-02 03:07:28 +10:00
|
|
|
playlistXSP(mediatype,
|
|
|
|
foldername,
|
|
|
|
folderid,
|
|
|
|
viewtype)
|
2016-03-03 03:27:21 +11:00
|
|
|
playlists.append(foldername)
|
2016-02-12 00:03:04 +11:00
|
|
|
# Add new video node
|
2016-03-03 03:27:21 +11:00
|
|
|
if foldername not in nodes and mediatype != "musicvideos":
|
|
|
|
vnodes.viewNode(sorted_views.index(foldername),
|
2016-02-12 00:03:04 +11:00
|
|
|
foldername,
|
|
|
|
mediatype,
|
2016-03-01 22:10:09 +11:00
|
|
|
viewtype,
|
|
|
|
folderid)
|
2016-03-03 03:27:21 +11:00
|
|
|
nodes.append(foldername)
|
2016-02-12 00:03:04 +11:00
|
|
|
totalnodes += 1
|
|
|
|
|
|
|
|
# Update items with new tag
|
|
|
|
items = emby_db.getItem_byView(folderid)
|
|
|
|
for item in items:
|
|
|
|
# Remove the "s" from viewtype for tags
|
2016-03-03 03:27:21 +11:00
|
|
|
kodi_db.updateTag(
|
|
|
|
current_tagid, tagid, item[0], current_viewtype[:-1])
|
2016-02-12 00:03:04 +11:00
|
|
|
else:
|
2016-03-03 03:27:21 +11:00
|
|
|
# Validate the playlist exists or recreate it
|
|
|
|
if mediatype != "artist":
|
|
|
|
if (foldername not in playlists and
|
|
|
|
mediatype in ('movie', 'show', 'musicvideos')):
|
2016-09-02 03:07:28 +10:00
|
|
|
playlistXSP(mediatype,
|
|
|
|
foldername,
|
|
|
|
folderid,
|
|
|
|
viewtype)
|
2016-03-03 03:27:21 +11:00
|
|
|
playlists.append(foldername)
|
2016-02-12 00:03:04 +11:00
|
|
|
# Create the video node if not already exists
|
2016-03-03 03:27:21 +11:00
|
|
|
if foldername not in nodes and mediatype != "musicvideos":
|
|
|
|
vnodes.viewNode(sorted_views.index(foldername),
|
2016-02-12 00:03:04 +11:00
|
|
|
foldername,
|
|
|
|
mediatype,
|
2016-03-01 22:10:09 +11:00
|
|
|
viewtype,
|
|
|
|
folderid)
|
2016-03-03 03:27:21 +11:00
|
|
|
nodes.append(foldername)
|
2016-02-12 00:03:04 +11:00
|
|
|
totalnodes += 1
|
2016-03-03 03:27:21 +11:00
|
|
|
return totalnodes
|
2016-02-12 00:03:04 +11:00
|
|
|
|
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-03-17 22:16:44 +11:00
|
|
|
self.views = []
|
2015-12-25 07:07:00 +11:00
|
|
|
vnodes = self.vnodes
|
2016-01-11 19:57:45 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
# Get views
|
2016-03-01 21:26:46 +11:00
|
|
|
sections = downloadutils.DownloadUtils().downloadUrl(
|
2016-02-20 06:03:06 +11:00
|
|
|
"{server}/library/sections")
|
2016-03-01 22:10:09 +11:00
|
|
|
try:
|
|
|
|
sections.attrib
|
|
|
|
except AttributeError:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Error download PMS views, abort maintainViews")
|
2016-02-01 20:33:33 +11:00
|
|
|
return False
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-03 04:56:42 +11:00
|
|
|
# For whatever freaking reason, .copy() or dict() does NOT work?!?!?!
|
2016-03-03 03:27:21 +11:00
|
|
|
self.nodes = {
|
|
|
|
'movie': [],
|
|
|
|
'show': [],
|
2016-06-05 02:48:22 +10:00
|
|
|
'artist': [],
|
|
|
|
'photo': []
|
2016-03-03 03:27:21 +11:00
|
|
|
}
|
2016-03-03 04:56:42 +11:00
|
|
|
self.playlists = {
|
|
|
|
'movie': [],
|
|
|
|
'show': [],
|
2016-06-05 02:48:22 +10:00
|
|
|
'artist': [],
|
|
|
|
'photo': []
|
2016-03-03 04:56:42 +11:00
|
|
|
}
|
2016-03-03 19:04:15 +11:00
|
|
|
self.sorted_views = []
|
2016-03-03 03:27:21 +11:00
|
|
|
|
|
|
|
for view in sections:
|
|
|
|
itemType = view.attrib['type']
|
2016-06-05 02:48:22 +10:00
|
|
|
if itemType in ('movie', 'show', 'photo'): # NOT artist for now
|
2016-03-03 19:04:15 +11:00
|
|
|
self.sorted_views.append(view.attrib['title'])
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Sorted views: %s' % self.sorted_views)
|
2016-03-03 19:04:15 +11:00
|
|
|
|
|
|
|
# total nodes for window properties
|
|
|
|
vnodes.clearProperties()
|
|
|
|
totalnodes = len(self.sorted_views)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-01 20:40:30 +11:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
2016-03-01 21:26:46 +11:00
|
|
|
# Backup old views to delete them later, if needed (at the end
|
|
|
|
# of this method, only unused views will be left in oldviews)
|
|
|
|
self.old_views = emby_db.getViews()
|
2016-03-01 20:40:30 +11:00
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
2016-03-01 21:26:46 +11:00
|
|
|
for folderItem in sections:
|
2016-03-03 03:27:21 +11:00
|
|
|
totalnodes = self.processView(folderItem,
|
|
|
|
kodi_db,
|
|
|
|
emby_db,
|
|
|
|
totalnodes)
|
2016-03-03 19:04:15 +11:00
|
|
|
# Add video nodes listings
|
|
|
|
# Plex: there seem to be no favorites/favorites tag
|
|
|
|
# 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
|
|
|
|
with kodidb.GetKodiDB('music') as kodi_db:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Save total
|
2016-09-02 03:07:28 +10:00
|
|
|
window('Plex.nodes.total', str(totalnodes))
|
2016-02-12 00:03:04 +11:00
|
|
|
|
2016-03-03 03:27:21 +11:00
|
|
|
# Reopen DB connection to ensure that changes were commited before
|
2016-03-01 21:26:46 +11:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Removing views: %s" % self.old_views)
|
2016-03-01 21:26:46 +11:00
|
|
|
for view in self.old_views:
|
|
|
|
emby_db.removeView(view)
|
2016-05-30 00:58:54 +10:00
|
|
|
# update views for all:
|
|
|
|
self.views = emby_db.getAllViewInfo()
|
2016-03-01 21:26:46 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Finished processing views. Views saved: %s" % self.views)
|
2016-03-03 03:27:21 +11:00
|
|
|
return True
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-04-07 19:57:34 +10:00
|
|
|
def GetUpdatelist(self, xml, itemType, method, viewName, viewId):
|
2016-01-08 03:27:48 +11:00
|
|
|
"""
|
2016-02-13 02:53:49 +11:00
|
|
|
THIS METHOD NEEDS TO BE FAST! => e.g. no API calls
|
|
|
|
|
2016-01-08 03:27:48 +11:00
|
|
|
Adds items to self.updatelist as well as self.allPlexElementsId dict
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-01-08 03:27:48 +11:00
|
|
|
Input:
|
2016-02-01 20:33:33 +11:00
|
|
|
xml: PMS answer for section items
|
2016-01-24 01:53:24 +11:00
|
|
|
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')
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
Output: self.updatelist, self.allPlexElementsId
|
2016-01-24 01:53:24 +11:00
|
|
|
self.updatelist APPENDED(!!) list itemids (Plex Keys as
|
2016-01-30 06:07:21 +11:00
|
|
|
as received from API.getRatingKey())
|
2016-01-24 01:53:24 +11:00
|
|
|
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
|
2016-09-12 01:59:00 +10:00
|
|
|
'mediaType': xxx, e.g. 'movie', 'episode'
|
2016-01-24 01:53:24 +11:00
|
|
|
|
|
|
|
self.allPlexElementsId APPENDED(!!) dict
|
2016-01-08 03:27:48 +11:00
|
|
|
= {itemid: checksum}
|
|
|
|
"""
|
2016-04-07 19:57:34 +10:00
|
|
|
if self.compare:
|
2016-03-01 22:10:09 +11:00
|
|
|
# Only process the delta - new or changed items
|
2016-02-01 20:33:33 +11:00
|
|
|
for item in xml:
|
2016-02-13 02:53:49 +11:00
|
|
|
itemId = item.attrib.get('ratingKey')
|
2016-02-01 20:33:33 +11:00
|
|
|
# Skipping items 'title=All episodes' without a 'ratingKey'
|
2016-02-13 02:53:49 +11:00
|
|
|
if not itemId:
|
2016-01-12 20:30:28 +11:00
|
|
|
continue
|
2016-02-13 02:53:49 +11:00
|
|
|
title = item.attrib.get('title', 'Missing Title Name')
|
|
|
|
plex_checksum = ("K%s%s"
|
|
|
|
% (itemId, item.attrib.get('updatedAt', '')))
|
2016-01-12 20:30:28 +11:00
|
|
|
self.allPlexElementsId[itemId] = plex_checksum
|
|
|
|
kodi_checksum = self.allKodiElementsId.get(itemId)
|
2016-03-01 22:10:09 +11:00
|
|
|
# Only update if movie is not in Kodi or checksum is
|
|
|
|
# different
|
2016-01-12 20:30:28 +11:00
|
|
|
if kodi_checksum != plex_checksum:
|
2016-09-12 01:59:00 +10:00
|
|
|
self.updatelist.append({
|
|
|
|
'itemId': itemId,
|
|
|
|
'itemType': itemType,
|
|
|
|
'method': method,
|
|
|
|
'viewName': viewName,
|
|
|
|
'viewId': viewId,
|
|
|
|
'title': title,
|
|
|
|
'mediaType': item.attrib.get('type')
|
|
|
|
})
|
2016-01-08 03:27:48 +11:00
|
|
|
else:
|
|
|
|
# Initial or repair sync: get all Plex movies
|
2016-02-01 20:33:33 +11:00
|
|
|
for item in xml:
|
2016-02-13 02:53:49 +11:00
|
|
|
itemId = item.attrib.get('ratingKey')
|
|
|
|
# Skipping items 'title=All episodes' without a 'ratingKey'
|
|
|
|
if not itemId:
|
2016-01-12 20:30:28 +11:00
|
|
|
continue
|
2016-02-13 02:53:49 +11:00
|
|
|
title = item.attrib.get('title', 'Missing Title Name')
|
|
|
|
plex_checksum = ("K%s%s"
|
|
|
|
% (itemId, item.attrib.get('updatedAt', '')))
|
2016-01-12 20:30:28 +11:00
|
|
|
self.allPlexElementsId[itemId] = plex_checksum
|
2016-09-12 01:59:00 +10:00
|
|
|
self.updatelist.append({
|
|
|
|
'itemId': itemId,
|
|
|
|
'itemType': itemType,
|
|
|
|
'method': method,
|
|
|
|
'viewName': viewName,
|
|
|
|
'viewId': viewId,
|
|
|
|
'title': title,
|
|
|
|
'mediaType': item.attrib.get('type')
|
|
|
|
})
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-03-10 18:51:24 +11:00
|
|
|
def GetAndProcessXMLs(self, itemType, showProgress=True):
|
2016-01-08 06:31:25 +11:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2016-01-08 06:31:25 +11:00
|
|
|
Input:
|
2016-01-24 01:53:24 +11:00
|
|
|
itemType: 'Movies', 'TVShows', ...
|
2016-01-11 17:55:22 +11:00
|
|
|
self.updatelist
|
2016-03-10 18:51:24 +11:00
|
|
|
showProgress If False, NEVER shows sync progress
|
2016-01-08 06:31:25 +11:00
|
|
|
"""
|
|
|
|
# Some logging, just in case.
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("self.updatelist: %s" % self.updatelist)
|
2016-01-28 06:41:28 +11:00
|
|
|
itemNumber = len(self.updatelist)
|
|
|
|
if itemNumber == 0:
|
2016-03-03 03:27:21 +11:00
|
|
|
return
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
# Run through self.updatelist, get XML metadata per item
|
|
|
|
# Initiate threads
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Starting sync threads")
|
2016-01-08 03:27:48 +11:00
|
|
|
getMetadataQueue = Queue.Queue()
|
2016-08-19 00:54:41 +10:00
|
|
|
processMetadataQueue = Queue.Queue(maxsize=100)
|
2016-02-12 00:03:04 +11:00
|
|
|
getMetadataLock = Lock()
|
|
|
|
processMetadataLock = Lock()
|
2016-01-08 03:27:48 +11:00
|
|
|
# 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)
|
2016-09-26 03:18:27 +10:00
|
|
|
# Spawn GetMetadata threads for downloading
|
2016-01-08 03:27:48 +11:00
|
|
|
threads = []
|
2016-09-26 03:18:27 +10:00
|
|
|
for i in range(min(self.syncThreadNumber, itemNumber)):
|
|
|
|
thread = ThreadedGetMetadata(getMetadataQueue,
|
|
|
|
processMetadataQueue,
|
|
|
|
getMetadataLock,
|
|
|
|
processMetadataLock)
|
|
|
|
thread.setDaemon(True)
|
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("%s download threads spawned" % len(threads))
|
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)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Processing thread spawned")
|
2016-01-08 03:27:48 +11:00
|
|
|
# Start one thread to show sync progress
|
2016-03-10 18:51:24 +11:00
|
|
|
if showProgress:
|
|
|
|
if self.showDbSync:
|
|
|
|
dialog = xbmcgui.DialogProgressBG()
|
|
|
|
thread = ThreadedShowSyncInfo(
|
|
|
|
dialog,
|
|
|
|
[getMetadataLock, processMetadataLock],
|
|
|
|
itemNumber,
|
|
|
|
itemType)
|
|
|
|
thread.setDaemon(True)
|
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Kodi Infobox thread spawned")
|
2016-01-30 06:07:21 +11:00
|
|
|
|
2016-01-08 03:27:48 +11:00
|
|
|
# Wait until finished
|
2016-01-30 18:43:28 +11:00
|
|
|
getMetadataQueue.join()
|
|
|
|
processMetadataQueue.join()
|
2016-01-08 03:27:48 +11:00
|
|
|
# Kill threads
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Waiting to kill threads")
|
2016-01-08 03:27:48 +11:00
|
|
|
for thread in threads:
|
2016-04-08 17:11:03 +10:00
|
|
|
# Threads might already have quit by themselves (e.g. Kodi exit)
|
|
|
|
try:
|
|
|
|
thread.stopThread()
|
|
|
|
except:
|
|
|
|
pass
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Stop sent to all threads")
|
2016-01-08 03:27:48 +11:00
|
|
|
# Wait till threads are indeed dead
|
|
|
|
for thread in threads:
|
2016-04-08 17:11:03 +10:00
|
|
|
try:
|
|
|
|
thread.join(1.0)
|
|
|
|
except:
|
|
|
|
pass
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Sync threads finished")
|
2016-09-11 19:46:42 +10:00
|
|
|
if (settings('FanartTV') == 'true' and
|
|
|
|
itemType in ('Movies', 'TVShows')):
|
|
|
|
# Save to queue for later processing
|
|
|
|
typus = {'Movies': 'movie', 'TVShows': 'tvshow'}[itemType]
|
|
|
|
for item in self.updatelist:
|
2016-09-12 01:59:00 +10:00
|
|
|
if item['mediaType'] in ('movie', 'tvshow'):
|
|
|
|
self.fanartqueue.put({
|
|
|
|
'itemId': item['itemId'],
|
|
|
|
'class': itemType,
|
|
|
|
'mediaType': typus,
|
|
|
|
'refresh': False
|
|
|
|
})
|
2016-01-28 06:41:28 +11:00
|
|
|
self.updatelist = []
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@LogTime
|
2016-01-08 06:31:25 +11:00
|
|
|
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-08 06:31:25 +11:00
|
|
|
|
2016-01-28 06:41:28 +11:00
|
|
|
views = [x for x in self.views if x['itemtype'] == 'movie']
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Processing Plex %s. Libraries: %s" % (itemType, views))
|
2016-01-08 06:31:25 +11:00
|
|
|
|
2016-02-10 20:04:49 +11:00
|
|
|
self.allKodiElementsId = {}
|
2016-01-08 06:31:25 +11:00
|
|
|
if self.compare:
|
2016-02-10 21:00:32 +11:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
2016-02-10 20:04:49 +11:00
|
|
|
# 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 = {}
|
2016-01-08 06:31:25 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
# PROCESS MOVIES #####
|
2016-01-11 17:55:22 +11:00
|
|
|
self.updatelist = []
|
2016-01-08 06:31:25 +11:00
|
|
|
for view in views:
|
2016-01-27 01:13:03 +11:00
|
|
|
if self.threadStopped():
|
2016-01-08 06:31:25 +11:00
|
|
|
return False
|
|
|
|
# Get items per view
|
|
|
|
viewId = view['id']
|
|
|
|
viewName = view['name']
|
2016-03-25 04:52:02 +11:00
|
|
|
all_plexmovies = PF.GetPlexSectionResults(
|
2016-03-15 00:51:49 +11:00
|
|
|
viewId, args=None, containerSize=self.limitindex)
|
2016-03-24 02:07:09 +11:00
|
|
|
if all_plexmovies is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Couldnt get section items, aborting for view.")
|
2016-02-01 20:33:33 +11:00
|
|
|
continue
|
2016-04-08 17:11:03 +10:00
|
|
|
elif all_plexmovies == 401:
|
|
|
|
return False
|
2016-01-08 06:31:25 +11:00
|
|
|
# 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)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Processed view")
|
2016-04-08 17:11:03 +10:00
|
|
|
# Update viewstate for EVERY item
|
2016-01-15 02:17:24 +11:00
|
|
|
for view in views:
|
2016-02-01 20:33:33 +11:00
|
|
|
if self.threadStopped():
|
|
|
|
return False
|
2016-01-15 02:17:24 +11:00
|
|
|
self.PlexUpdateWatched(view['id'], itemType)
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
# PROCESS DELETES #####
|
2016-01-11 01:16:59 +11:00
|
|
|
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-09-02 03:07:28 +10:00
|
|
|
log.info("%s sync is finished." % itemType)
|
2016-01-11 01:16:59 +11:00
|
|
|
return True
|
2016-01-08 06:31:25 +11:00
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
def PlexUpdateWatched(self, viewId, itemType,
|
|
|
|
lastViewedAt=None, updatedAt=None):
|
2016-01-15 02:17:24 +11:00
|
|
|
"""
|
2016-01-30 06:07:21 +11:00
|
|
|
Updates plex elements' view status ('watched' or 'unwatched') and
|
2016-01-15 02:17:24 +11:00
|
|
|
also updates resume times.
|
|
|
|
This is done by downloading one XML for ALL elements with viewId
|
|
|
|
"""
|
2016-03-25 04:52:02 +11:00
|
|
|
xml = PF.GetAllPlexLeaves(viewId,
|
|
|
|
lastViewedAt=lastViewedAt,
|
|
|
|
updatedAt=updatedAt,
|
|
|
|
containerSize=self.limitindex)
|
2016-02-11 22:44:12 +11:00
|
|
|
# Return if there are no items in PMS reply - it's faster
|
|
|
|
try:
|
|
|
|
xml[0].attrib
|
|
|
|
except (TypeError, AttributeError, IndexError):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error('Error updating watch status. Could not get viewId: '
|
|
|
|
'%s of itemType %s with lastViewedAt: %s, updatedAt: '
|
|
|
|
'%s' % (viewId, itemType, lastViewedAt, updatedAt))
|
2016-02-11 22:44:12 +11:00
|
|
|
return
|
|
|
|
|
2016-03-01 23:31:35 +11:00
|
|
|
if itemType in ('Movies', 'TVShows'):
|
2016-02-11 22:44:12 +11:00
|
|
|
self.updateKodiVideoLib = True
|
2016-03-01 23:31:35 +11:00
|
|
|
elif itemType in ('Music'):
|
|
|
|
self.updateKodiMusicLib = True
|
2016-02-11 22:44:12 +11:00
|
|
|
|
|
|
|
itemMth = getattr(itemtypes, itemType)
|
|
|
|
with itemMth() as method:
|
|
|
|
method.updateUserdata(xml)
|
2016-01-15 02:17:24 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@LogTime
|
2016-01-10 02:14:02 +11:00
|
|
|
def PlexTVShows(self):
|
2016-01-08 03:27:48 +11:00
|
|
|
# Initialize
|
|
|
|
self.allPlexElementsId = {}
|
2016-01-11 17:55:22 +11:00
|
|
|
itemType = 'TVShows'
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-01-28 06:41:28 +11:00
|
|
|
views = [x for x in self.views if x['itemtype'] == 'show']
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Media folders for %s: %s" % (itemType, views))
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
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:
|
2016-02-10 20:04:49 +11:00
|
|
|
# Pull the list of TV shows already in Kodi
|
2016-02-13 02:53:49 +11:00
|
|
|
for kind in ('Series', 'Season', 'Episode'):
|
|
|
|
try:
|
|
|
|
elements = dict(emby_db.getChecksum(kind))
|
|
|
|
self.allKodiElementsId.update(elements)
|
|
|
|
# Yet empty/not yet synched
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
# PROCESS TV Shows #####
|
2016-01-11 17:55:22 +11:00
|
|
|
self.updatelist = []
|
2016-01-08 03:27:48 +11:00
|
|
|
for view in views:
|
2016-01-27 01:13:03 +11:00
|
|
|
if self.threadStopped():
|
2016-01-08 03:27:48 +11:00
|
|
|
return False
|
|
|
|
# Get items per view
|
|
|
|
viewId = view['id']
|
|
|
|
viewName = view['name']
|
2016-03-25 04:52:02 +11:00
|
|
|
allPlexTvShows = PF.GetPlexSectionResults(
|
2016-03-15 00:51:49 +11:00
|
|
|
viewId, containerSize=self.limitindex)
|
2016-03-24 02:57:49 +11:00
|
|
|
if allPlexTvShows is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Error downloading show xml for view %s" % viewId)
|
2016-02-01 20:33:33 +11:00
|
|
|
continue
|
2016-04-08 17:11:03 +10:00
|
|
|
elif allPlexTvShows == 401:
|
|
|
|
return False
|
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-09-02 03:07:28 +10:00
|
|
|
log.debug("Analyzed view %s with ID %s" % (viewName, viewId))
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-01-11 01:16:59 +11:00
|
|
|
# COPY for later use
|
|
|
|
allPlexTvShowsId = self.allPlexElementsId.copy()
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2016-03-14 02:06:54 +11:00
|
|
|
# Process self.updatelist
|
|
|
|
self.GetAndProcessXMLs(itemType)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("GetAndProcessXMLs completed for tv shows")
|
2016-03-14 02:06:54 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
# PROCESS TV Seasons #####
|
2016-01-10 02:14:02 +11:00
|
|
|
# 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
|
2016-03-25 04:52:02 +11:00
|
|
|
seasons = PF.GetAllPlexChildren(
|
2016-03-15 00:51:49 +11:00
|
|
|
tvShowId, containerSize=self.limitindex)
|
2016-03-24 02:57:49 +11:00
|
|
|
if seasons is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Error download season xml for show %s" % tvShowId)
|
2016-02-01 20:33:33 +11:00
|
|
|
continue
|
2016-04-08 17:11:03 +10:00
|
|
|
elif seasons == 401:
|
|
|
|
return False
|
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
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Analyzed all seasons of TV show with Plex Id %s"
|
|
|
|
% tvShowId)
|
2016-01-10 02:14:02 +11:00
|
|
|
|
2016-03-14 02:06:54 +11:00
|
|
|
# Process self.updatelist
|
|
|
|
self.GetAndProcessXMLs(itemType)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("GetAndProcessXMLs completed for seasons")
|
2016-03-14 02:06:54 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
# PROCESS TV Episodes #####
|
2016-01-10 02:14:02 +11:00
|
|
|
# Cycle through tv shows
|
2016-01-15 02:17:24 +11:00
|
|
|
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
|
2016-03-25 04:52:02 +11:00
|
|
|
episodes = PF.GetAllPlexLeaves(
|
2016-03-15 00:51:49 +11:00
|
|
|
view['id'], containerSize=self.limitindex)
|
2016-03-24 02:57:49 +11:00
|
|
|
if episodes is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Error downloading episod xml for view %s"
|
|
|
|
% view.get('name'))
|
2016-02-01 20:33:33 +11:00
|
|
|
continue
|
2016-04-08 17:11:03 +10:00
|
|
|
elif episodes == 401:
|
|
|
|
return False
|
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)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Analyzed all episodes of TV show with Plex Id %s"
|
|
|
|
% view['id'])
|
2016-01-11 17:55:22 +11:00
|
|
|
|
|
|
|
# Process self.updatelist
|
|
|
|
self.GetAndProcessXMLs(itemType)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("GetAndProcessXMLs completed for episodes")
|
2016-01-11 17:55:22 +11:00
|
|
|
# Refresh season info
|
|
|
|
# Cycle through tv shows
|
|
|
|
with itemtypes.TVShows() as TVshow:
|
|
|
|
for tvShowId in allPlexTvShowsId:
|
2016-03-25 04:52:02 +11:00
|
|
|
XMLtvshow = PF.GetPlexMetadata(tvShowId)
|
2016-04-08 02:29:23 +10:00
|
|
|
if XMLtvshow is None or XMLtvshow == 401:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error('Could not download XMLtvshow')
|
2016-04-08 02:29:23 +10:00
|
|
|
continue
|
2016-01-11 17:55:22 +11:00
|
|
|
TVshow.refreshSeasonEntry(XMLtvshow, tvShowId)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Season info refreshed")
|
2016-01-11 17:55:22 +11:00
|
|
|
|
2016-01-15 02:17:24 +11:00
|
|
|
# Update viewstate:
|
|
|
|
for view in views:
|
2016-04-08 17:11:03 +10:00
|
|
|
if self.threadStopped():
|
|
|
|
return False
|
2016-01-15 02:17:24 +11:00
|
|
|
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-09-02 03:07:28 +10:00
|
|
|
log.info("%s sync is finished." % itemType)
|
2016-01-08 03:27:48 +11:00
|
|
|
return True
|
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
@LogTime
|
2016-02-13 02:53:49 +11:00
|
|
|
def PlexMusic(self):
|
|
|
|
itemType = 'Music'
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
views = [x for x in self.views if x['itemtype'] == 'artist']
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Media folders for %s: %s" % (itemType, views))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
methods = {
|
|
|
|
'MusicArtist': 'add_updateArtist',
|
|
|
|
'MusicAlbum': 'add_updateAlbum',
|
|
|
|
'Audio': 'add_updateSong'
|
|
|
|
}
|
|
|
|
urlArgs = {
|
|
|
|
'MusicArtist': {'type': 8},
|
|
|
|
'MusicAlbum': {'type': 9},
|
|
|
|
'Audio': {'type': 10}
|
2015-12-25 07:07:00 +11:00
|
|
|
}
|
2016-01-23 22:05:56 +11:00
|
|
|
|
2016-03-01 22:10:09 +11:00
|
|
|
# Process artist, then album and tracks last to minimize overhead
|
2016-02-13 02:53:49 +11:00
|
|
|
for kind in ('MusicArtist', 'MusicAlbum', 'Audio'):
|
|
|
|
if self.threadStopped():
|
2016-04-08 17:11:03 +10:00
|
|
|
return False
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Start processing music %s" % kind)
|
2016-04-08 17:11:03 +10:00
|
|
|
if self.ProcessMusic(views,
|
|
|
|
kind,
|
|
|
|
urlArgs[kind],
|
|
|
|
methods[kind]) is False:
|
|
|
|
return False
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Processing of music %s done" % kind)
|
2016-02-13 02:53:49 +11:00
|
|
|
self.GetAndProcessXMLs(itemType)
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("GetAndProcessXMLs for music %s completed" % kind)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2016-04-14 00:51:53 +10:00
|
|
|
# Update viewstate for EVERY item
|
|
|
|
for view in views:
|
|
|
|
if self.threadStopped():
|
|
|
|
return False
|
|
|
|
self.PlexUpdateWatched(view['id'], itemType)
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# reset stuff
|
|
|
|
self.allKodiElementsId = {}
|
|
|
|
self.allPlexElementsId = {}
|
|
|
|
self.updatelist = []
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("%s sync is finished." % itemType)
|
2016-02-13 02:53:49 +11:00
|
|
|
return True
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
def ProcessMusic(self, views, kind, urlArgs, method):
|
|
|
|
self.allKodiElementsId = {}
|
|
|
|
self.allPlexElementsId = {}
|
|
|
|
self.updatelist = []
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# Get a list of items already existing in Kodi db
|
|
|
|
if self.compare:
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
# Pull the list of items already in Kodi
|
|
|
|
try:
|
|
|
|
elements = dict(emby_db.getChecksum(kind))
|
|
|
|
self.allKodiElementsId.update(elements)
|
|
|
|
# Yet empty/nothing yet synched
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
for view in views:
|
|
|
|
if self.threadStopped():
|
2016-04-08 17:11:03 +10:00
|
|
|
return False
|
2016-02-13 02:53:49 +11:00
|
|
|
# Get items per view
|
|
|
|
viewId = view['id']
|
|
|
|
viewName = view['name']
|
2016-03-25 04:52:02 +11:00
|
|
|
itemsXML = PF.GetPlexSectionResults(
|
2016-03-15 00:51:49 +11:00
|
|
|
viewId, args=urlArgs, containerSize=self.limitindex)
|
2016-03-24 02:57:49 +11:00
|
|
|
if itemsXML is None:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Error downloading xml for view %s" % viewId)
|
2016-02-13 02:53:49 +11:00
|
|
|
continue
|
2016-04-08 17:11:03 +10:00
|
|
|
elif itemsXML == 401:
|
|
|
|
return False
|
2016-02-13 02:53:49 +11:00
|
|
|
# Populate self.updatelist and self.allPlexElementsId
|
|
|
|
self.GetUpdatelist(itemsXML,
|
|
|
|
'Music',
|
|
|
|
method,
|
|
|
|
viewName,
|
|
|
|
viewId)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-04-08 22:11:50 +10:00
|
|
|
if self.compare:
|
|
|
|
# Manual sync, process deletes
|
|
|
|
with itemtypes.Music() as Music:
|
|
|
|
for itemid in self.allKodiElementsId:
|
|
|
|
if itemid not in self.allPlexElementsId:
|
|
|
|
Music.remove(itemid)
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def compareDBVersion(self, current, minimum):
|
|
|
|
# It returns True is database is up to date. False otherwise.
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("current DB: %s minimum DB: %s" % (current, minimum))
|
2016-03-04 01:28:44 +11:00
|
|
|
try:
|
|
|
|
currMajor, currMinor, currPatch = current.split(".")
|
|
|
|
except ValueError:
|
|
|
|
# there WAS no current DB, e.g. deleted.
|
|
|
|
return True
|
2015-12-25 07:07:00 +11:00
|
|
|
minMajor, minMinor, minPatch = minimum.split(".")
|
2016-03-15 04:10:36 +11:00
|
|
|
currMajor = int(currMajor)
|
|
|
|
currMinor = int(currMinor)
|
|
|
|
currPatch = int(currPatch)
|
|
|
|
minMajor = int(minMajor)
|
|
|
|
minMinor = int(minMinor)
|
|
|
|
minPatch = int(minPatch)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if currMajor > minMajor:
|
|
|
|
return True
|
2016-03-28 04:06:36 +11:00
|
|
|
elif currMajor < minMajor:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if currMinor > minMinor:
|
|
|
|
return True
|
|
|
|
elif currMinor < minMinor:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if currPatch >= minPatch:
|
2015-12-25 07:07:00 +11:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
def processMessage(self, message):
|
|
|
|
"""
|
|
|
|
processes json.loads() messages from websocket. Triage what we need to
|
|
|
|
do with "process_" methods
|
|
|
|
"""
|
|
|
|
typus = message.get('type')
|
|
|
|
if typus == 'playing':
|
|
|
|
self.process_playing(message['_children'])
|
|
|
|
elif typus == 'timeline':
|
|
|
|
self.process_timeline(message['_children'])
|
|
|
|
|
|
|
|
def multi_delete(self, liste, deleteListe):
|
|
|
|
"""
|
|
|
|
Deletes the list items of liste at the positions in deleteListe
|
2016-04-08 17:11:03 +10:00
|
|
|
(which can be in any arbitrary order)
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
|
|
|
indexes = sorted(deleteListe, reverse=True)
|
|
|
|
for index in indexes:
|
|
|
|
del liste[index]
|
|
|
|
return liste
|
|
|
|
|
2016-03-28 04:06:36 +11:00
|
|
|
def processItems(self):
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
|
|
|
Periodically called to process new/updated PMS items
|
|
|
|
|
|
|
|
PMS needs a while to download info from internet AFTER it
|
|
|
|
showed up under 'timeline' websocket messages
|
2016-03-28 04:06:36 +11:00
|
|
|
|
|
|
|
data['type']:
|
|
|
|
1: movie
|
|
|
|
2: tv show??
|
|
|
|
3: season??
|
|
|
|
4: episode
|
|
|
|
8: artist (band)
|
|
|
|
9: album
|
|
|
|
10: track (song)
|
|
|
|
12: trailer, extras?
|
|
|
|
|
|
|
|
data['state']:
|
|
|
|
0: 'created',
|
|
|
|
2: 'matching',
|
|
|
|
3: 'downloading',
|
|
|
|
4: 'loading',
|
|
|
|
5: 'finished',
|
|
|
|
6: 'analyzing',
|
|
|
|
9: 'deleted'
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2016-03-28 04:06:36 +11:00
|
|
|
self.videoLibUpdate = False
|
|
|
|
self.musicLibUpdate = False
|
2016-09-02 03:07:28 +10:00
|
|
|
now = getUnixTimestamp()
|
2016-03-25 04:52:02 +11:00
|
|
|
deleteListe = []
|
|
|
|
for i, item in enumerate(self.itemsToProcess):
|
2016-10-24 01:55:28 +11:00
|
|
|
if (now - item['timestamp'] < self.saftyMargin and
|
|
|
|
item['state'] != 9):
|
2016-03-25 04:52:02 +11:00
|
|
|
# We haven't waited long enough for the PMS to finish
|
2016-10-24 01:55:28 +11:00
|
|
|
# processing the item. Do it later (excepting deletions)
|
2016-03-25 04:52:02 +11:00
|
|
|
continue
|
2016-05-13 05:46:50 +10:00
|
|
|
if item['state'] == 9:
|
2016-06-01 03:30:12 +10:00
|
|
|
successful = self.process_deleteditems(item)
|
|
|
|
else:
|
2016-09-18 03:12:32 +10:00
|
|
|
successful, item = self.process_newitems(item)
|
|
|
|
if successful and settings('FanartTV') == 'true':
|
|
|
|
if item['mediatype'] in ('movie', 'show'):
|
|
|
|
mediaType = {'movie': 'Movie'}[item['mediatype']]
|
|
|
|
cls = {'movie': 'Movies'}[item['mediatype']]
|
|
|
|
self.fanartqueue.put({
|
|
|
|
'itemId': item['ratingKey'],
|
|
|
|
'class': cls,
|
|
|
|
'mediaType': mediaType,
|
|
|
|
'refresh': False
|
|
|
|
})
|
2016-06-01 03:30:12 +10:00
|
|
|
if successful is True:
|
|
|
|
deleteListe.append(i)
|
2016-05-13 05:46:50 +10:00
|
|
|
else:
|
2016-06-01 03:30:12 +10:00
|
|
|
# Safety net if we can't process an item
|
|
|
|
item['attempt'] += 1
|
|
|
|
if item['attempt'] > 3:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.warn('Repeatedly could not process item %s, abort'
|
|
|
|
% item)
|
2016-05-13 05:46:50 +10:00
|
|
|
deleteListe.append(i)
|
2016-03-25 04:52:02 +11:00
|
|
|
|
|
|
|
# Get rid of the items we just processed
|
|
|
|
if len(deleteListe) > 0:
|
|
|
|
self.itemsToProcess = self.multi_delete(
|
|
|
|
self.itemsToProcess, deleteListe)
|
|
|
|
# Let Kodi know of the change
|
2016-03-28 04:06:36 +11:00
|
|
|
if self.videoLibUpdate is True:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Doing Kodi Video Lib update")
|
2016-03-25 04:52:02 +11:00
|
|
|
xbmc.executebuiltin('UpdateLibrary(video)')
|
2016-03-28 04:06:36 +11:00
|
|
|
if self.musicLibUpdate is True:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Doing Kodi Music Lib update")
|
2016-03-28 04:06:36 +11:00
|
|
|
xbmc.executebuiltin('UpdateLibrary(music)')
|
|
|
|
|
|
|
|
def process_newitems(self, item):
|
|
|
|
ratingKey = item['ratingKey']
|
|
|
|
xml = PF.GetPlexMetadata(ratingKey)
|
2016-10-12 03:35:11 +11:00
|
|
|
try:
|
|
|
|
mediatype = xml[0].attrib['type']
|
2016-10-12 03:37:47 +11:00
|
|
|
except (IndexError, KeyError, TypeError):
|
2016-10-12 03:35:11 +11:00
|
|
|
log.error('Could not download metadata for %s' % ratingKey)
|
|
|
|
return False, item
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Processing new/updated PMS item: %s" % ratingKey)
|
2016-03-28 04:06:36 +11:00
|
|
|
viewtag = xml.attrib.get('librarySectionTitle')
|
|
|
|
viewid = xml.attrib.get('librarySectionID')
|
2016-09-11 19:12:25 +10:00
|
|
|
# Attach mediatype for later
|
|
|
|
item['mediatype'] = mediatype
|
2016-03-28 04:06:36 +11:00
|
|
|
if mediatype == 'movie':
|
|
|
|
self.videoLibUpdate = True
|
|
|
|
with itemtypes.Movies() as movie:
|
|
|
|
movie.add_update(xml[0],
|
|
|
|
viewtag=viewtag,
|
|
|
|
viewid=viewid)
|
|
|
|
elif mediatype == 'episode':
|
|
|
|
self.videoLibUpdate = True
|
|
|
|
with itemtypes.TVShows() as show:
|
|
|
|
show.add_updateEpisode(xml[0],
|
|
|
|
viewtag=viewtag,
|
|
|
|
viewid=viewid)
|
|
|
|
elif mediatype == 'track':
|
|
|
|
self.musicLibUpdate = True
|
|
|
|
with itemtypes.Music() as music:
|
|
|
|
music.add_updateSong(xml[0],
|
|
|
|
viewtag=viewtag,
|
|
|
|
viewid=viewid)
|
2016-09-18 03:12:32 +10:00
|
|
|
return True, item
|
2016-03-28 04:06:36 +11:00
|
|
|
|
|
|
|
def process_deleteditems(self, item):
|
|
|
|
if item.get('type') == 1:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Removing movie %s" % item.get('ratingKey'))
|
2016-03-28 04:06:36 +11:00
|
|
|
self.videoLibUpdate = True
|
|
|
|
with itemtypes.Movies() as movie:
|
|
|
|
movie.remove(item.get('ratingKey'))
|
|
|
|
elif item.get('type') in (2, 3, 4):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Removing episode/season/tv show %s"
|
|
|
|
% item.get('ratingKey'))
|
2016-03-28 04:06:36 +11:00
|
|
|
self.videoLibUpdate = True
|
|
|
|
with itemtypes.TVShows() as show:
|
|
|
|
show.remove(item.get('ratingKey'))
|
|
|
|
elif item.get('type') in (8, 9, 10):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug("Removing song/album/artist %s" % item.get('ratingKey'))
|
2016-03-28 04:06:36 +11:00
|
|
|
self.musicLibUpdate = True
|
|
|
|
with itemtypes.Music() as music:
|
|
|
|
music.remove(item.get('ratingKey'))
|
|
|
|
return True
|
2016-03-25 04:52:02 +11:00
|
|
|
|
|
|
|
def process_timeline(self, data):
|
|
|
|
"""
|
2016-03-28 04:06:36 +11:00
|
|
|
PMS is messing with the library items, e.g. new or changed. Put in our
|
2016-04-08 17:11:03 +10:00
|
|
|
"processing queue" for later
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
|
|
|
for item in data:
|
2016-10-12 03:52:59 +11:00
|
|
|
if 'tv.plex' in item.get('identifier', ''):
|
|
|
|
# Ommit Plex DVR messages - the Plex IDs are not corresponding
|
|
|
|
# (DVR ratingKeys are not unique and might correspond to a
|
|
|
|
# movie or episode)
|
|
|
|
continue
|
2016-10-12 03:28:07 +11:00
|
|
|
typus = int(item.get('type', 0))
|
|
|
|
state = int(item.get('state', 0))
|
2016-05-13 05:46:50 +10:00
|
|
|
if state == 9 or typus in (1, 4, 10):
|
2016-10-12 03:52:59 +11:00
|
|
|
# Only process deleted items OR movies, episodes, tracks/songs
|
2016-10-12 03:28:07 +11:00
|
|
|
itemId = str(item.get('itemID', '0'))
|
|
|
|
if itemId == '0':
|
|
|
|
log.warn('Received malformed PMS message: %s' % item)
|
|
|
|
continue
|
2016-05-13 05:46:50 +10:00
|
|
|
# Have we already added this element?
|
|
|
|
for existingItem in self.itemsToProcess:
|
|
|
|
if existingItem['ratingKey'] == itemId:
|
2016-09-11 18:43:16 +10:00
|
|
|
break
|
2016-05-13 05:46:50 +10:00
|
|
|
else:
|
|
|
|
# Haven't added this element to the queue yet
|
|
|
|
self.itemsToProcess.append({
|
|
|
|
'state': state,
|
|
|
|
'type': typus,
|
|
|
|
'ratingKey': itemId,
|
2016-09-02 03:07:28 +10:00
|
|
|
'timestamp': getUnixTimestamp(),
|
2016-06-01 03:30:12 +10:00
|
|
|
'attempt': 0
|
2016-05-13 05:46:50 +10:00
|
|
|
})
|
2016-03-25 04:52:02 +11:00
|
|
|
|
|
|
|
def process_playing(self, data):
|
2016-03-28 01:57:35 +11:00
|
|
|
"""
|
|
|
|
Someone (not necessarily the user signed in) is playing something some-
|
|
|
|
where
|
|
|
|
"""
|
2016-03-25 04:52:02 +11:00
|
|
|
items = []
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
for item in data:
|
2016-03-28 01:57:35 +11:00
|
|
|
# Drop buffering messages immediately
|
2016-03-25 04:52:02 +11:00
|
|
|
state = item.get('state')
|
|
|
|
if state == 'buffering':
|
|
|
|
continue
|
|
|
|
ratingKey = item.get('ratingKey')
|
2016-04-13 23:27:02 +10:00
|
|
|
kodiInfo = emby_db.getItem_byId(ratingKey)
|
|
|
|
if kodiInfo is None:
|
|
|
|
# Item not (yet) in Kodi library
|
|
|
|
continue
|
2016-03-28 01:57:35 +11:00
|
|
|
sessionKey = item.get('sessionKey')
|
|
|
|
# Do we already have a sessionKey stored?
|
|
|
|
if sessionKey not in self.sessionKeys:
|
2016-09-17 01:47:09 +10:00
|
|
|
if settings('plex_serverowned') == 'false':
|
2016-04-13 23:27:02 +10:00
|
|
|
# Not our PMS, we are not authorized to get the
|
|
|
|
# sessions
|
|
|
|
# On the bright side, it must be us playing :-)
|
|
|
|
self.sessionKeys = {
|
|
|
|
sessionKey: {}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
# PMS is ours - get all current sessions
|
|
|
|
self.sessionKeys = PF.GetPMSStatus(
|
2016-09-02 03:07:28 +10:00
|
|
|
window('plex_token'))
|
|
|
|
log.debug('Updated current sessions. They are: %s'
|
|
|
|
% self.sessionKeys)
|
2016-04-13 23:27:02 +10:00
|
|
|
if sessionKey not in self.sessionKeys:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.warn('Session key %s still unknown! Skip '
|
|
|
|
'item' % sessionKey)
|
2016-04-13 23:27:02 +10:00
|
|
|
continue
|
2016-03-28 01:57:35 +11:00
|
|
|
|
|
|
|
currSess = self.sessionKeys[sessionKey]
|
2016-09-18 04:36:53 +10:00
|
|
|
if window('plex_currently_playing_itemid') == ratingKey:
|
|
|
|
# Don't update what we already know
|
|
|
|
continue
|
2016-09-17 01:47:09 +10:00
|
|
|
if settings('plex_serverowned') != 'false':
|
2016-04-13 23:27:02 +10:00
|
|
|
# Identify the user - same one as signed on with PKC? Skip
|
|
|
|
# update if neither session's username nor userid match
|
|
|
|
# (Owner sometime's returns id '1', not always)
|
2016-09-02 03:07:28 +10:00
|
|
|
if (window('plex_token') == '' and
|
2016-04-13 23:27:02 +10:00
|
|
|
currSess['userId'] == '1'):
|
|
|
|
# PKC not signed in to plex.tv. Plus owner of PMS is
|
|
|
|
# playing (the '1').
|
|
|
|
# Hence must be us (since several users require plex.tv
|
|
|
|
# token for PKC)
|
|
|
|
pass
|
2016-09-02 03:07:28 +10:00
|
|
|
elif not (currSess['userId'] == window('currUserId')
|
2016-04-13 23:27:02 +10:00
|
|
|
or
|
2016-09-02 03:07:28 +10:00
|
|
|
currSess['username'] == window('plex_username')):
|
2016-10-24 01:15:57 +11:00
|
|
|
log.debug('Our username %s, userid %s did not match '
|
|
|
|
'the session username %s with userid %s'
|
|
|
|
% (window('plex_username'),
|
|
|
|
window('currUserId'),
|
|
|
|
currSess['username'],
|
|
|
|
currSess['userId']))
|
2016-04-13 23:27:02 +10:00
|
|
|
continue
|
2016-03-28 01:57:35 +11:00
|
|
|
|
|
|
|
# Get an up-to-date XML from the PMS
|
|
|
|
# because PMS will NOT directly tell us:
|
|
|
|
# duration of item
|
|
|
|
# viewCount
|
|
|
|
if currSess.get('duration') is None:
|
|
|
|
xml = PF.GetPlexMetadata(ratingKey)
|
2016-04-08 17:11:03 +10:00
|
|
|
if xml in (None, 401):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error('Could not get up-to-date xml for item %s'
|
|
|
|
% ratingKey)
|
2016-03-28 01:57:35 +11:00
|
|
|
continue
|
|
|
|
API = PlexAPI.API(xml[0])
|
|
|
|
userdata = API.getUserData()
|
|
|
|
currSess['duration'] = userdata['Runtime']
|
|
|
|
currSess['viewCount'] = userdata['PlayCount']
|
2016-06-06 02:39:26 +10:00
|
|
|
# Sometimes, Plex tells us resume points in milliseconds and
|
|
|
|
# not in seconds - thank you very much!
|
|
|
|
if item.get('viewOffset') > currSess['duration']:
|
|
|
|
resume = item.get('viewOffset') / 1000
|
|
|
|
else:
|
|
|
|
resume = item.get('viewOffset')
|
2016-03-28 01:57:35 +11:00
|
|
|
# Append to list that we need to process
|
2016-03-25 04:52:02 +11:00
|
|
|
items.append({
|
|
|
|
'ratingKey': ratingKey,
|
|
|
|
'kodi_id': kodiInfo[0],
|
|
|
|
'file_id': kodiInfo[1],
|
|
|
|
'kodi_type': kodiInfo[4],
|
2016-06-06 02:39:26 +10:00
|
|
|
'viewOffset': resume,
|
2016-03-25 04:52:02 +11:00
|
|
|
'state': state,
|
2016-03-28 01:57:35 +11:00
|
|
|
'duration': currSess['duration'],
|
|
|
|
'viewCount': currSess['viewCount'],
|
2016-09-02 03:07:28 +10:00
|
|
|
'lastViewedAt': DateToKodi(getUnixTimestamp())
|
2016-03-25 04:52:02 +11:00
|
|
|
})
|
2016-09-02 03:07:28 +10:00
|
|
|
log.debug('Update playstate for user %s with id %s: %s'
|
|
|
|
% (window('plex_username'),
|
|
|
|
window('currUserId'),
|
|
|
|
items[-1]))
|
2016-04-08 17:11:03 +10:00
|
|
|
# Now tell Kodi where we are
|
2016-03-25 04:52:02 +11:00
|
|
|
for item in items:
|
|
|
|
itemFkt = getattr(itemtypes,
|
|
|
|
PF.GetItemClassFromType(item['kodi_type']))
|
|
|
|
with itemFkt() as Fkt:
|
|
|
|
Fkt.updatePlaystate(item)
|
|
|
|
|
2016-09-11 03:49:03 +10:00
|
|
|
def fanartSync(self, refresh=False):
|
|
|
|
"""
|
|
|
|
Checks all Plex movies and TV shows whether they still need fanart
|
|
|
|
|
|
|
|
refresh=True Force refresh all external fanart
|
|
|
|
"""
|
|
|
|
items = []
|
|
|
|
typus = {
|
|
|
|
'Movie': 'Movies',
|
|
|
|
'Series': 'TVShows'
|
|
|
|
}
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
for plextype in typus:
|
|
|
|
items.extend(emby_db.itemsByType(plextype))
|
|
|
|
# Shuffle the list to not always start out identically
|
2016-09-11 19:29:51 +10:00
|
|
|
shuffle(items)
|
2016-09-11 03:49:03 +10:00
|
|
|
for item in items:
|
|
|
|
self.fanartqueue.put({
|
|
|
|
'itemId': item['plexId'],
|
|
|
|
'mediaType': item['kodi_type'],
|
|
|
|
'class': typus[item['plex_type']],
|
|
|
|
'refresh': refresh
|
|
|
|
})
|
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def run(self):
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
2016-08-07 23:33:36 +10:00
|
|
|
self.run_internal()
|
2015-12-25 07:07:00 +11:00
|
|
|
except Exception as e:
|
2016-09-02 03:07:28 +10:00
|
|
|
window('plex_dbScan', clear=True)
|
2016-09-05 00:57:06 +10:00
|
|
|
log.error('LibrarySync thread crashed. Error message: %s' % e)
|
2016-04-08 17:11:03 +10:00
|
|
|
import traceback
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Traceback:\n%s" % traceback.format_exc())
|
2016-03-08 21:47:46 +11:00
|
|
|
# Library sync thread has crashed
|
2016-09-02 03:07:28 +10:00
|
|
|
self.dialog.ok(addonName, lang(39400))
|
2015-12-25 07:07:00 +11:00
|
|
|
raise
|
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def run_internal(self):
|
2016-03-08 21:20:11 +11:00
|
|
|
# Re-assign handles to have faster calls
|
|
|
|
threadStopped = self.threadStopped
|
|
|
|
threadSuspended = self.threadSuspended
|
|
|
|
installSyncDone = self.installSyncDone
|
|
|
|
enableBackgroundSync = self.enableBackgroundSync
|
|
|
|
fullSync = self.fullSync
|
2016-03-25 04:52:02 +11:00
|
|
|
processMessage = self.processMessage
|
2016-04-08 17:11:03 +10:00
|
|
|
processItems = self.processItems
|
2016-03-28 04:06:36 +11:00
|
|
|
fullSyncInterval = self.fullSyncInterval
|
2016-04-08 18:52:15 +10:00
|
|
|
lastSync = 0
|
|
|
|
lastTimeSync = 0
|
2016-03-28 04:06:36 +11:00
|
|
|
lastProcessing = 0
|
2016-04-08 17:11:03 +10:00
|
|
|
oneDay = 60*60*24
|
2016-03-08 21:20:11 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
xbmcplayer = xbmc.Player()
|
2016-03-30 04:45:32 +11:00
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
queue = self.queue
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
startupComplete = False
|
2016-01-28 06:41:28 +11:00
|
|
|
self.views = []
|
2016-03-03 03:27:21 +11:00
|
|
|
errorcount = 0
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("---===### Starting LibrarySync ###===---")
|
2016-04-08 21:57:55 +10:00
|
|
|
|
2016-05-30 00:52:38 +10:00
|
|
|
# Ensure that DBs exist if called for very first time
|
|
|
|
self.initializeDBs()
|
|
|
|
|
2016-04-08 21:57:55 +10:00
|
|
|
if self.enableMusic:
|
2016-09-02 03:07:28 +10:00
|
|
|
advancedSettingsXML()
|
2016-04-08 21:57:55 +10:00
|
|
|
|
2016-09-11 03:49:03 +10:00
|
|
|
if settings('FanartTV') == 'true':
|
|
|
|
self.fanartthread.start()
|
|
|
|
|
2016-03-08 21:20:11 +11:00
|
|
|
while not threadStopped():
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-04-08 17:11:03 +10:00
|
|
|
# In the event the server goes offline
|
2016-03-08 21:20:11 +11:00
|
|
|
while threadSuspended():
|
2015-12-25 07:07:00 +11:00
|
|
|
# Set in service.py
|
2016-03-08 21:20:11 +11:00
|
|
|
if threadStopped():
|
2015-12-25 07:07:00 +11:00
|
|
|
# Abort was requested while waiting. We should exit
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("###===--- LibrarySync Stopped ---===###")
|
2016-01-28 06:41:28 +11:00
|
|
|
return
|
2016-02-11 20:56:01 +11:00
|
|
|
xbmc.sleep(1000)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
if (window('plex_dbCheck') != "true" and installSyncDone):
|
2015-12-25 07:07:00 +11:00
|
|
|
# Verify the validity of the database
|
2016-02-20 06:03:06 +11:00
|
|
|
currentVersion = settings('dbCreatedWithVersion')
|
2016-05-31 16:06:42 +10:00
|
|
|
minVersion = window('plex_minDBVersion')
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-28 01:57:35 +11:00
|
|
|
if not self.compareDBVersion(currentVersion, minVersion):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.warn("Db version out of date: %s minimum version "
|
|
|
|
"required: %s" % (currentVersion, minVersion))
|
2016-03-08 21:47:46 +11:00
|
|
|
# DB out of date. Proceed to recreate?
|
2016-09-02 03:07:28 +10:00
|
|
|
resp = self.dialog.yesno(heading=addonName,
|
|
|
|
line1=lang(39401))
|
2015-12-25 07:07:00 +11:00
|
|
|
if not resp:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.warn("Db version out of date! USER IGNORED!")
|
2016-03-08 21:47:46 +11:00
|
|
|
# PKC may not work correctly until reset
|
2016-09-02 03:07:28 +10:00
|
|
|
self.dialog.ok(heading=addonName,
|
|
|
|
line1=(addonName + lang(39402)))
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2016-09-02 03:07:28 +10:00
|
|
|
reset()
|
2016-03-02 03:49:16 +11:00
|
|
|
break
|
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbCheck', value="true")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if not startupComplete:
|
2016-03-02 02:52:09 +11:00
|
|
|
# Also runs when first installed
|
2015-12-25 07:07:00 +11:00
|
|
|
# Verify the video database can be found
|
2016-09-02 03:07:28 +10:00
|
|
|
videoDb = getKodiVideoDBPath()
|
2015-12-25 07:07:00 +11:00
|
|
|
if not xbmcvfs.exists(videoDb):
|
|
|
|
# Database does not exists
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("The current Kodi version is incompatible "
|
|
|
|
"to know which Kodi versions are supported.")
|
|
|
|
log.error('Current Kodi version: %s' % tryDecode(
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmc.getInfoLabel('System.BuildVersion')))
|
2016-03-08 21:47:46 +11:00
|
|
|
# "Current Kodi version is unsupported, cancel lib sync"
|
2016-09-02 03:07:28 +10:00
|
|
|
self.dialog.ok(heading=addonName, line1=lang(39403))
|
2015-12-25 07:07:00 +11:00
|
|
|
break
|
|
|
|
|
|
|
|
# Run start up sync
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', value="true")
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Db version: %s" % settings('dbCreatedWithVersion'))
|
|
|
|
lastTimeSync = getUnixTimestamp()
|
2016-03-28 01:57:35 +11:00
|
|
|
self.syncPMStime()
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Initial start-up full sync starting")
|
|
|
|
lastSync = getUnixTimestamp()
|
2016-04-07 19:57:34 +10:00
|
|
|
librarySync = fullSync()
|
2016-03-12 00:42:14 +11:00
|
|
|
# Initialize time offset Kodi - PMS
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', clear=True)
|
2016-03-03 03:27:21 +11:00
|
|
|
if librarySync:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Initial start-up full sync successful")
|
2016-03-03 03:27:21 +11:00
|
|
|
startupComplete = True
|
|
|
|
settings('SyncInstallRunDone', value="true")
|
|
|
|
settings("dbCreatedWithVersion",
|
|
|
|
self.clientInfo.getVersion())
|
2016-03-08 21:20:11 +11:00
|
|
|
installSyncDone = True
|
2016-03-03 03:27:21 +11:00
|
|
|
else:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Initial start-up full sync unsuccessful")
|
2016-03-03 03:27:21 +11:00
|
|
|
errorcount += 1
|
|
|
|
if errorcount > 2:
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Startup full sync failed. Stopping sync")
|
2016-03-08 21:47:46 +11:00
|
|
|
# "Startup syncing process failed repeatedly"
|
|
|
|
# "Please restart"
|
2016-09-02 03:07:28 +10:00
|
|
|
self.dialog.ok(heading=addonName,
|
|
|
|
line1=lang(39404))
|
2016-03-03 03:27:21 +11:00
|
|
|
break
|
2016-01-28 06:41:28 +11:00
|
|
|
|
|
|
|
# Currently no db scan, so we can start a new scan
|
2016-05-31 16:06:42 +10:00
|
|
|
elif window('plex_dbScan') != "true":
|
2016-01-28 06:41:28 +11:00
|
|
|
# Full scan was requested from somewhere else, e.g. userclient
|
2016-04-07 19:57:34 +10:00
|
|
|
if window('plex_runLibScan') in ("full", "repair"):
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Full library scan requested, starting')
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', value="true")
|
2016-04-07 19:57:34 +10:00
|
|
|
if window('plex_runLibScan') == "full":
|
|
|
|
fullSync()
|
|
|
|
elif window('plex_runLibScan') == "repair":
|
|
|
|
fullSync(repair=True)
|
2016-02-20 06:03:06 +11:00
|
|
|
window('plex_runLibScan', clear=True)
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', clear=True)
|
2016-03-10 18:51:24 +11:00
|
|
|
# Full library sync finished
|
2016-09-02 03:07:28 +10:00
|
|
|
self.showKodiNote(lang(39407), forced=False)
|
2016-03-03 20:11:32 +11:00
|
|
|
# Reset views was requested from somewhere else
|
|
|
|
elif window('plex_runLibScan') == "views":
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Refresh playlist and nodes requested, starting')
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', value="true")
|
2016-03-03 20:11:32 +11:00
|
|
|
window('plex_runLibScan', clear=True)
|
|
|
|
|
|
|
|
# First remove playlists
|
2016-09-02 03:07:28 +10:00
|
|
|
deletePlaylists()
|
2016-03-03 20:11:32 +11:00
|
|
|
# Remove video nodes
|
2016-09-02 03:07:28 +10:00
|
|
|
deleteNodes()
|
2016-03-03 20:11:32 +11:00
|
|
|
# Kick off refresh
|
2016-04-08 17:11:03 +10:00
|
|
|
if self.maintainViews() is True:
|
2016-03-08 21:20:11 +11:00
|
|
|
# Ran successfully
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("Refresh playlists/nodes completed")
|
2016-03-08 21:47:46 +11:00
|
|
|
# "Plex playlists/nodes refreshed"
|
2016-09-02 03:07:28 +10:00
|
|
|
self.showKodiNote(lang(39405), forced=True)
|
2016-03-03 20:11:32 +11:00
|
|
|
else:
|
2016-03-08 21:20:11 +11:00
|
|
|
# Failed
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error("Refresh playlists/nodes failed")
|
2016-03-08 21:47:46 +11:00
|
|
|
# "Plex playlists/nodes refresh failed"
|
2016-09-02 03:07:28 +10:00
|
|
|
self.showKodiNote(lang(39406),
|
2016-03-10 18:51:24 +11:00
|
|
|
forced=True,
|
|
|
|
icon="error")
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', clear=True)
|
2016-09-11 20:15:26 +10:00
|
|
|
elif window('plex_runLibScan') == 'fanart':
|
|
|
|
window('plex_runLibScan', clear=True)
|
|
|
|
# Only look for missing fanart (No)
|
|
|
|
# or refresh all fanart (Yes)
|
|
|
|
self.fanartSync(refresh=self.dialog.yesno(
|
|
|
|
heading=addonName,
|
|
|
|
line1=lang(39223),
|
|
|
|
nolabel=lang(39224),
|
|
|
|
yeslabel=lang(39225)))
|
2016-09-17 22:48:40 +10:00
|
|
|
elif window('plex_runLibScan') == 'del_textures':
|
|
|
|
window('plex_runLibScan', clear=True)
|
|
|
|
window('plex_dbScan', value="true")
|
|
|
|
import artwork
|
|
|
|
artwork.Artwork().fullTextureCacheSync()
|
|
|
|
window('plex_dbScan', clear=True)
|
2016-03-28 04:06:36 +11:00
|
|
|
else:
|
2016-09-02 03:07:28 +10:00
|
|
|
now = getUnixTimestamp()
|
2016-04-08 17:11:03 +10:00
|
|
|
if (now - lastSync > fullSyncInterval and
|
2016-03-30 04:45:32 +11:00
|
|
|
not xbmcplayer.isPlaying()):
|
2016-04-08 17:11:03 +10:00
|
|
|
lastSync = now
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Doing scheduled full library scan')
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', value="true")
|
2016-04-08 17:11:03 +10:00
|
|
|
if fullSync() is False and not threadStopped():
|
2016-09-02 03:07:28 +10:00
|
|
|
log.error('Could not finish scheduled full sync')
|
|
|
|
self.showKodiNote(lang(39410),
|
2016-04-08 17:11:03 +10:00
|
|
|
forced=True,
|
|
|
|
icon='error')
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', clear=True)
|
2016-03-10 18:51:24 +11:00
|
|
|
# Full library sync finished
|
2016-09-02 03:07:28 +10:00
|
|
|
self.showKodiNote(lang(39407), forced=False)
|
2016-04-08 17:11:03 +10:00
|
|
|
elif now - lastTimeSync > oneDay:
|
|
|
|
lastTimeSync = now
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info('Starting daily time sync')
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', value="true")
|
2016-04-08 17:11:03 +10:00
|
|
|
self.syncPMStime()
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_dbScan', clear=True)
|
2016-03-28 04:06:36 +11:00
|
|
|
elif enableBackgroundSync:
|
|
|
|
# Check back whether we should process something
|
2016-10-24 01:48:59 +11:00
|
|
|
# Only do this once every while (otherwise, potentially
|
|
|
|
# many screen refreshes lead to flickering)
|
|
|
|
if now - lastProcessing > 5:
|
2016-04-08 17:11:03 +10:00
|
|
|
lastProcessing = now
|
|
|
|
processItems()
|
2016-03-25 04:52:02 +11:00
|
|
|
# See if there is a PMS message we need to handle
|
|
|
|
try:
|
|
|
|
message = queue.get(block=False)
|
|
|
|
except Queue.Empty:
|
|
|
|
xbmc.sleep(100)
|
|
|
|
continue
|
|
|
|
# Got a message from PMS; process it
|
|
|
|
else:
|
|
|
|
processMessage(message)
|
2016-03-28 01:57:35 +11:00
|
|
|
queue.task_done()
|
2016-03-25 04:52:02 +11:00
|
|
|
# NO sleep!
|
|
|
|
continue
|
2016-04-08 17:11:03 +10:00
|
|
|
else:
|
|
|
|
# Still sleep if backgroundsync disabled
|
|
|
|
xbmc.sleep(100)
|
2016-03-25 04:52:02 +11:00
|
|
|
|
|
|
|
xbmc.sleep(100)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-04-10 00:57:45 +10:00
|
|
|
# doUtils could still have a session open due to interrupted sync
|
|
|
|
try:
|
|
|
|
downloadutils.DownloadUtils().stopSession()
|
|
|
|
except:
|
|
|
|
pass
|
2016-09-02 03:07:28 +10:00
|
|
|
log.info("###===--- LibrarySync Stopped ---===###")
|