2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2017-08-18 18:38:03 +10:00
|
|
|
from logging import getLogger
|
2016-09-11 03:49:03 +10:00
|
|
|
from random import shuffle
|
2018-10-24 19:57:52 +11:00
|
|
|
import Queue
|
2015-12-25 07:07:00 +11:00
|
|
|
import xbmc
|
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
from . import library_sync
|
2018-08-06 02:09:48 +10:00
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
from .plex_api import API
|
|
|
|
from .downloadutils import DownloadUtils as DU
|
|
|
|
from . import backgroundthread, utils, path_ops
|
|
|
|
from . import itemtypes, plex_db, kodidb_functions as kodidb
|
|
|
|
from . import artwork, plex_functions as PF
|
|
|
|
from . import variables as v, state
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-10-25 02:19:36 +11:00
|
|
|
LOG = getLogger('PLEX.sync')
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
|
|
|
|
def set_library_scan_toggle(boolean=True):
|
|
|
|
"""
|
|
|
|
Make sure to hit this function before starting large scans
|
|
|
|
"""
|
|
|
|
if not boolean:
|
|
|
|
# Deactivate
|
|
|
|
state.DB_SCAN = False
|
|
|
|
utils.window('plex_dbScan', clear=True)
|
|
|
|
else:
|
|
|
|
state.DB_SCAN = True
|
|
|
|
utils.window('plex_dbScan', value="true")
|
2016-09-02 03:07:28 +10:00
|
|
|
|
|
|
|
|
2018-10-25 02:19:36 +11:00
|
|
|
class Sync(backgroundthread.KillableThread):
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
The one and only library sync thread. Spawn only 1!
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2018-01-07 01:19:12 +11:00
|
|
|
def __init__(self):
|
2018-04-18 04:18:25 +10:00
|
|
|
self.items_to_process = []
|
|
|
|
self.session_keys = {}
|
2018-10-24 16:08:32 +11:00
|
|
|
self.sync_successful = False
|
|
|
|
self.last_full_sync = 0
|
2018-10-24 19:57:52 +11:00
|
|
|
if utils.settings('FanartTV') == 'true':
|
|
|
|
self.fanartqueue = Queue.Queue()
|
|
|
|
self.fanartthread = library_sync.fanart.ThreadedProcessFanart(self.fanartqueue)
|
|
|
|
else:
|
|
|
|
self.fanartqueue = None
|
|
|
|
self.fanartthread = None
|
2016-03-28 01:57:35 +11:00
|
|
|
# How long should we wait at least to process new/changed PMS items?
|
2017-08-21 16:01:48 +10:00
|
|
|
# Show sync dialog even if user deactivated?
|
2018-10-24 16:08:32 +11:00
|
|
|
self.force_dialog = False
|
2018-04-18 04:18:25 +10:00
|
|
|
# Need to be set accordingly later
|
2018-10-06 22:40:14 +10:00
|
|
|
self.update_kodi_video_library = False
|
|
|
|
self.update_kodi_music_library = False
|
2018-10-24 16:08:32 +11:00
|
|
|
# Lock used to wait on a full sync, e.g. on initial sync
|
|
|
|
self.lock = backgroundthread.threading.Lock()
|
2018-10-25 02:19:36 +11:00
|
|
|
super(Sync, self).__init__()
|
2018-10-24 16:08:32 +11:00
|
|
|
|
|
|
|
def isCanceled(self):
|
|
|
|
return xbmc.abortRequested or state.STOP_PKC
|
|
|
|
|
|
|
|
def isSuspended(self):
|
|
|
|
return state.SUSPEND_LIBRARY_THREAD or state.STOP_SYNC
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def suspend_item_sync(self):
|
|
|
|
"""
|
|
|
|
Returns True if we should not sync new items or artwork to Kodi or even
|
|
|
|
abort a sync currently running.
|
|
|
|
|
|
|
|
Returns False otherwise.
|
|
|
|
"""
|
2018-10-24 16:08:32 +11:00
|
|
|
if self.isSuspended() or self.isCanceled():
|
2018-04-18 04:18:25 +10:00
|
|
|
return True
|
|
|
|
elif state.SUSPEND_SYNC:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def show_kodi_note(self, message, 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-02-12 00:44:11 +11:00
|
|
|
"""
|
2017-08-21 16:01:48 +10:00
|
|
|
if state.SYNC_DIALOG is not True and self.force_dialog is not True:
|
|
|
|
return
|
2016-03-10 18:51:24 +11:00
|
|
|
if icon == "plex":
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.dialog('notification',
|
|
|
|
heading='{plex}',
|
|
|
|
message=message,
|
|
|
|
icon='{plex}',
|
|
|
|
sound=False)
|
2016-03-10 18:51:24 +11:00
|
|
|
elif icon == "error":
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.dialog('notification',
|
|
|
|
heading='{plex}',
|
|
|
|
message=message,
|
|
|
|
icon='{error}')
|
2016-02-12 00:44:11 +11:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def process_message(self, message):
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
|
|
|
processes json.loads() messages from websocket. Triage what we need to
|
|
|
|
do with "process_" methods
|
|
|
|
"""
|
2018-10-24 16:08:32 +11:00
|
|
|
if message['type'] == 'playing':
|
|
|
|
self.process_playing(message['PlaySessionStateNotification'])
|
|
|
|
elif message['type'] == 'timeline':
|
|
|
|
self.process_timeline(message['TimelineEntry'])
|
|
|
|
elif message['type'] == 'activity':
|
|
|
|
self.process_activity(message['ActivityNotification'])
|
2016-03-25 04:52:02 +11:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def multi_delete(self, liste, delete_list):
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
Deletes the list items of liste at the positions in delete_list
|
2016-04-08 17:11:03 +10:00
|
|
|
(which can be in any arbitrary order)
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
indexes = sorted(delete_list, reverse=True)
|
2016-03-25 04:52:02 +11:00
|
|
|
for index in indexes:
|
|
|
|
del liste[index]
|
|
|
|
return liste
|
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def process_items(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
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
now = utils.unix_timestamp()
|
|
|
|
delete_list = []
|
|
|
|
for i, item in enumerate(self.items_to_process):
|
2018-10-24 16:08:32 +11:00
|
|
|
if self.isCanceled() or self.suspended():
|
2017-02-03 00:49:14 +11:00
|
|
|
# Chances are that Kodi gets shut down
|
|
|
|
break
|
2017-02-02 19:32:00 +11:00
|
|
|
if item['state'] == 9:
|
|
|
|
successful = self.process_deleteditems(item)
|
2017-08-22 16:16:21 +10:00
|
|
|
elif now - item['timestamp'] < state.BACKGROUNDSYNC_SAFTYMARGIN:
|
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-06-01 03:30:12 +10:00
|
|
|
else:
|
2017-02-02 22:27:21 +11:00
|
|
|
successful = self.process_newitems(item)
|
2018-06-22 03:24:37 +10:00
|
|
|
if successful and utils.settings('FanartTV') == 'true':
|
2017-09-08 20:06:31 +10:00
|
|
|
if item['type'] in (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_SHOW):
|
2016-09-18 03:12:32 +10:00
|
|
|
self.fanartqueue.put({
|
2017-02-02 22:27:21 +11:00
|
|
|
'plex_id': item['ratingKey'],
|
2017-09-08 20:06:31 +10:00
|
|
|
'plex_type': item['type'],
|
2016-09-18 03:12:32 +10:00
|
|
|
'refresh': False
|
|
|
|
})
|
2016-06-01 03:30:12 +10:00
|
|
|
if successful is True:
|
2018-04-18 04:18:25 +10:00
|
|
|
delete_list.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:
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.error('Repeatedly could not process item %s, abort',
|
|
|
|
item)
|
2018-04-18 04:18:25 +10:00
|
|
|
delete_list.append(i)
|
2016-03-25 04:52:02 +11:00
|
|
|
|
|
|
|
# Get rid of the items we just processed
|
2018-04-18 04:18:25 +10:00
|
|
|
if delete_list:
|
|
|
|
self.items_to_process = self.multi_delete(self.items_to_process,
|
|
|
|
delete_list)
|
2016-03-25 04:52:02 +11:00
|
|
|
# Let Kodi know of the change
|
2018-10-06 22:40:14 +10:00
|
|
|
if self.update_kodi_video_library or self.update_kodi_music_library:
|
2018-10-24 19:57:52 +11:00
|
|
|
library_sync.update_library(video=self.update_kodi_video_library,
|
|
|
|
music=self.update_kodi_music_library)
|
2018-10-06 22:40:14 +10:00
|
|
|
self.update_kodi_video_library = False
|
|
|
|
self.update_kodi_music_library = False
|
2016-03-28 04:06:36 +11:00
|
|
|
|
|
|
|
def process_newitems(self, item):
|
2018-04-18 04:18:25 +10:00
|
|
|
xml = PF.GetPlexMetadata(item['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):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.error('Could not download metadata for %s', item['ratingKey'])
|
2017-02-02 22:27:21 +11:00
|
|
|
return False
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug("Processing new/updated PMS item: %s", item['ratingKey'])
|
2016-03-28 04:06:36 +11:00
|
|
|
viewtag = xml.attrib.get('librarySectionTitle')
|
|
|
|
viewid = xml.attrib.get('librarySectionID')
|
2017-02-02 22:27:21 +11:00
|
|
|
if mediatype == v.PLEX_TYPE_MOVIE:
|
2018-04-18 04:18:25 +10:00
|
|
|
self.update_kodi_video_library = True
|
2016-03-28 04:06:36 +11:00
|
|
|
with itemtypes.Movies() as movie:
|
|
|
|
movie.add_update(xml[0],
|
|
|
|
viewtag=viewtag,
|
|
|
|
viewid=viewid)
|
2017-02-02 22:27:21 +11:00
|
|
|
elif mediatype == v.PLEX_TYPE_EPISODE:
|
2018-04-18 04:18:25 +10:00
|
|
|
self.update_kodi_video_library = True
|
2016-03-28 04:06:36 +11:00
|
|
|
with itemtypes.TVShows() as show:
|
|
|
|
show.add_updateEpisode(xml[0],
|
|
|
|
viewtag=viewtag,
|
|
|
|
viewid=viewid)
|
2017-02-02 22:27:21 +11:00
|
|
|
elif mediatype == v.PLEX_TYPE_SONG:
|
2018-04-18 04:18:25 +10:00
|
|
|
self.update_kodi_music_library = True
|
|
|
|
with itemtypes.Music() as music_db:
|
|
|
|
music_db.add_updateSong(xml[0], viewtag=viewtag, viewid=viewid)
|
2017-02-02 22:27:21 +11:00
|
|
|
return True
|
2016-03-28 04:06:36 +11:00
|
|
|
|
|
|
|
def process_deleteditems(self, item):
|
2017-09-08 20:06:31 +10:00
|
|
|
if item['type'] == v.PLEX_TYPE_MOVIE:
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug("Removing movie %s", item['ratingKey'])
|
2018-04-18 04:18:25 +10:00
|
|
|
self.update_kodi_video_library = True
|
2016-03-28 04:06:36 +11:00
|
|
|
with itemtypes.Movies() as movie:
|
2017-09-08 20:06:31 +10:00
|
|
|
movie.remove(item['ratingKey'])
|
|
|
|
elif item['type'] in (v.PLEX_TYPE_SHOW,
|
|
|
|
v.PLEX_TYPE_SEASON,
|
|
|
|
v.PLEX_TYPE_EPISODE):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug("Removing episode/season/show with plex id %s",
|
2018-03-12 01:23:32 +11:00
|
|
|
item['ratingKey'])
|
2018-04-18 04:18:25 +10:00
|
|
|
self.update_kodi_video_library = True
|
2016-03-28 04:06:36 +11:00
|
|
|
with itemtypes.TVShows() as show:
|
2017-09-08 20:06:31 +10:00
|
|
|
show.remove(item['ratingKey'])
|
|
|
|
elif item['type'] in (v.PLEX_TYPE_ARTIST,
|
|
|
|
v.PLEX_TYPE_ALBUM,
|
|
|
|
v.PLEX_TYPE_SONG):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug("Removing song/album/artist %s", item['ratingKey'])
|
2018-04-18 04:18:25 +10:00
|
|
|
self.update_kodi_music_library = True
|
|
|
|
with itemtypes.Music() as music_db:
|
|
|
|
music_db.remove(item['ratingKey'])
|
2016-03-28 04:06:36 +11:00
|
|
|
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
|
2017-09-08 20:06:31 +10:00
|
|
|
typus = v.PLEX_TYPE_FROM_WEBSOCKET[int(item['type'])]
|
2017-09-08 20:34:13 +10:00
|
|
|
if typus == v.PLEX_TYPE_CLIP:
|
|
|
|
# No need to process extras or trailers
|
|
|
|
continue
|
2017-09-08 20:06:31 +10:00
|
|
|
status = int(item['state'])
|
2018-05-03 01:27:35 +10:00
|
|
|
if typus == 'playlist':
|
2018-10-24 16:08:32 +11:00
|
|
|
if not library_sync.PLAYLIST_SYNC_ENABLED:
|
2018-05-03 16:20:55 +10:00
|
|
|
continue
|
2018-07-12 05:24:27 +10:00
|
|
|
playlists.websocket(plex_id=unicode(item['itemID']),
|
|
|
|
status=status)
|
2018-05-03 01:27:35 +10:00
|
|
|
elif status == 9:
|
2017-09-08 20:34:13 +10:00
|
|
|
# Immediately and always process deletions (as the PMS will
|
|
|
|
# send additional message with other codes)
|
2018-04-18 04:18:25 +10:00
|
|
|
self.items_to_process.append({
|
2017-09-08 20:34:13 +10:00
|
|
|
'state': status,
|
|
|
|
'type': typus,
|
|
|
|
'ratingKey': str(item['itemID']),
|
2018-04-18 04:18:25 +10:00
|
|
|
'timestamp': utils.unix_timestamp(),
|
2017-09-08 20:34:13 +10:00
|
|
|
'attempt': 0
|
|
|
|
})
|
|
|
|
elif typus in (v.PLEX_TYPE_MOVIE,
|
|
|
|
v.PLEX_TYPE_EPISODE,
|
|
|
|
v.PLEX_TYPE_SONG) and status == 5:
|
2017-09-08 20:06:31 +10:00
|
|
|
plex_id = str(item['itemID'])
|
2017-09-08 20:34:13 +10:00
|
|
|
# Have we already added this element for processing?
|
2018-04-18 04:18:25 +10:00
|
|
|
for existing_item in self.items_to_process:
|
|
|
|
if existing_item['ratingKey'] == plex_id:
|
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
|
2018-04-18 04:18:25 +10:00
|
|
|
self.items_to_process.append({
|
2017-05-17 18:09:50 +10:00
|
|
|
'state': status,
|
2016-05-13 05:46:50 +10:00
|
|
|
'type': typus,
|
2017-02-02 19:32:00 +11:00
|
|
|
'ratingKey': plex_id,
|
2018-04-18 04:18:25 +10:00
|
|
|
'timestamp': utils.unix_timestamp(),
|
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
|
|
|
|
2017-09-08 20:06:31 +10:00
|
|
|
def process_activity(self, data):
|
|
|
|
"""
|
|
|
|
PMS is re-scanning an item, e.g. after having changed a movie poster.
|
|
|
|
WATCH OUT for this if it's triggered by our PKC library scan!
|
|
|
|
"""
|
|
|
|
for item in data:
|
|
|
|
if item['event'] != 'ended':
|
|
|
|
# Scan still going on, so skip for now
|
|
|
|
continue
|
2017-09-08 20:12:29 +10:00
|
|
|
elif item['Activity'].get('Context') is None:
|
|
|
|
# Not related to any Plex element, but entire library
|
|
|
|
continue
|
2017-09-08 20:06:31 +10:00
|
|
|
elif item['Activity']['type'] != 'library.refresh.items':
|
|
|
|
# Not the type of message relevant for us
|
|
|
|
continue
|
2018-04-18 04:18:25 +10:00
|
|
|
plex_id = PF.GetPlexKeyNumber(item['Activity']['Context']['key'])[1]
|
2017-09-08 20:06:31 +10:00
|
|
|
if plex_id == '':
|
2017-09-08 20:36:26 +10:00
|
|
|
# Likely a Plex id like /library/metadata/3/children
|
|
|
|
continue
|
2017-09-08 20:06:31 +10:00
|
|
|
# We're only looking at existing elements - have we synced yet?
|
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
kodi_info = plex_db.getItem_byId(plex_id)
|
|
|
|
if kodi_info is None:
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug('Plex id %s not synced yet - skipping', plex_id)
|
2017-09-08 20:06:31 +10:00
|
|
|
continue
|
|
|
|
# Have we already added this element?
|
2018-04-18 04:18:25 +10:00
|
|
|
for existing_item in self.items_to_process:
|
|
|
|
if existing_item['ratingKey'] == plex_id:
|
2017-09-08 20:06:31 +10:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Haven't added this element to the queue yet
|
2018-04-18 04:18:25 +10:00
|
|
|
self.items_to_process.append({
|
2017-09-08 20:06:31 +10:00
|
|
|
'state': None, # Don't need a state here
|
|
|
|
'type': kodi_info[5],
|
|
|
|
'ratingKey': plex_id,
|
2018-04-18 04:18:25 +10:00
|
|
|
'timestamp': utils.unix_timestamp(),
|
2017-09-08 20:06:31 +10:00
|
|
|
'attempt': 0
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
"""
|
2017-09-08 20:06:31 +10:00
|
|
|
for item in data:
|
|
|
|
status = item['state']
|
2018-07-05 21:55:38 +10:00
|
|
|
if status == 'buffering' or status == 'stopped':
|
|
|
|
# Drop buffering and stop messages immediately - no value
|
2017-09-08 20:06:31 +10:00
|
|
|
continue
|
2018-02-16 03:19:12 +11:00
|
|
|
plex_id = item['ratingKey']
|
|
|
|
skip = False
|
2018-02-04 22:22:10 +11:00
|
|
|
for pid in (0, 1, 2):
|
2018-02-06 03:48:50 +11:00
|
|
|
if plex_id == state.PLAYER_STATES[pid]['plex_id']:
|
2018-01-28 23:55:00 +11:00
|
|
|
# Kodi is playing this item - no need to set the playstate
|
2018-02-16 03:19:12 +11:00
|
|
|
skip = True
|
|
|
|
if skip:
|
|
|
|
continue
|
2018-04-18 04:18:25 +10:00
|
|
|
session_key = item['sessionKey']
|
2017-09-08 20:06:31 +10:00
|
|
|
# Do we already have a sessionKey stored?
|
2018-04-18 04:18:25 +10:00
|
|
|
if session_key not in self.session_keys:
|
2018-02-06 03:48:50 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
kodi_info = plex_db.getItem_byId(plex_id)
|
|
|
|
if kodi_info is None:
|
|
|
|
# Item not (yet) in Kodi library
|
|
|
|
continue
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.settings('plex_serverowned') == 'false':
|
2018-02-06 03:48:50 +11:00
|
|
|
# Not our PMS, we are not authorized to get the sessions
|
2017-09-08 20:06:31 +10:00
|
|
|
# On the bright side, it must be us playing :-)
|
2018-04-18 04:18:25 +10:00
|
|
|
self.session_keys[session_key] = {}
|
2017-09-08 20:06:31 +10:00
|
|
|
else:
|
|
|
|
# PMS is ours - get all current sessions
|
2018-04-18 04:18:25 +10:00
|
|
|
self.session_keys.update(PF.GetPMSStatus(state.PLEX_TOKEN))
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug('Updated current sessions. They are: %s',
|
2018-04-18 04:18:25 +10:00
|
|
|
self.session_keys)
|
|
|
|
if session_key not in self.session_keys:
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.info('Session key %s still unknown! Skip '
|
2018-04-18 04:18:25 +10:00
|
|
|
'playstate update', session_key)
|
2016-04-13 23:27:02 +10:00
|
|
|
continue
|
2018-02-06 03:48:50 +11:00
|
|
|
# Attach Kodi info to the session
|
2018-04-18 04:18:25 +10:00
|
|
|
self.session_keys[session_key]['kodi_id'] = kodi_info[0]
|
|
|
|
self.session_keys[session_key]['file_id'] = kodi_info[1]
|
|
|
|
self.session_keys[session_key]['kodi_type'] = kodi_info[4]
|
|
|
|
session = self.session_keys[session_key]
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.settings('plex_serverowned') != 'false':
|
2017-09-08 20:06:31 +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)
|
2018-02-06 03:48:50 +11:00
|
|
|
if not state.PLEX_TOKEN and session['userId'] == '1':
|
2017-09-08 20:06:31 +10:00
|
|
|
# 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
|
2018-02-06 03:48:50 +11:00
|
|
|
elif not (session['userId'] == state.PLEX_USER_ID or
|
|
|
|
session['username'] == state.PLEX_USERNAME):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug('Our username %s, userid %s did not match '
|
2018-02-06 03:48:50 +11:00
|
|
|
'the session username %s with userid %s',
|
|
|
|
state.PLEX_USERNAME,
|
|
|
|
state.PLEX_USER_ID,
|
|
|
|
session['username'],
|
|
|
|
session['userId'])
|
2017-09-08 20:06:31 +10:00
|
|
|
continue
|
2018-02-06 03:48:50 +11:00
|
|
|
# Get an up-to-date XML from the PMS because PMS will NOT directly
|
|
|
|
# tell us: duration of item viewCount
|
|
|
|
if session.get('duration') is None:
|
2018-04-18 04:18:25 +10:00
|
|
|
xml = PF.GetPlexMetadata(plex_id)
|
2017-09-08 20:06:31 +10:00
|
|
|
if xml in (None, 401):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.error('Could not get up-to-date xml for item %s',
|
2018-02-06 03:48:50 +11:00
|
|
|
plex_id)
|
2017-09-08 20:06:31 +10:00
|
|
|
continue
|
2018-06-22 03:24:37 +10:00
|
|
|
api = API(xml[0])
|
2018-02-12 00:42:49 +11:00
|
|
|
userdata = api.userdata()
|
2018-02-06 03:48:50 +11:00
|
|
|
session['duration'] = userdata['Runtime']
|
|
|
|
session['viewCount'] = userdata['PlayCount']
|
2017-09-08 20:06:31 +10:00
|
|
|
# Sometimes, Plex tells us resume points in milliseconds and
|
|
|
|
# not in seconds - thank you very much!
|
2018-02-06 03:48:50 +11:00
|
|
|
if item['viewOffset'] > session['duration']:
|
|
|
|
resume = item['viewOffset'] / 1000
|
2017-09-08 20:06:31 +10:00
|
|
|
else:
|
2018-02-06 03:48:50 +11:00
|
|
|
resume = item['viewOffset']
|
|
|
|
if resume < v.IGNORE_SECONDS_AT_START:
|
2017-09-24 02:49:59 +10:00
|
|
|
continue
|
2018-02-06 03:48:50 +11:00
|
|
|
try:
|
|
|
|
completed = float(resume) / float(session['duration'])
|
|
|
|
except (ZeroDivisionError, TypeError):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.error('Could not mark playstate for %s and session %s',
|
2018-02-06 03:48:50 +11:00
|
|
|
data, session)
|
|
|
|
continue
|
|
|
|
if completed >= v.MARK_PLAYED_AT:
|
|
|
|
# Only mark completely watched ONCE
|
|
|
|
if session.get('marked_played') is None:
|
|
|
|
session['marked_played'] = True
|
|
|
|
mark_played = True
|
|
|
|
else:
|
|
|
|
# Don't mark it as completely watched again
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
mark_played = False
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.debug('Update playstate for user %s with id %s for plex id %s',
|
2018-02-06 03:48:50 +11:00
|
|
|
state.PLEX_USERNAME, state.PLEX_USER_ID, plex_id)
|
|
|
|
item_fkt = getattr(itemtypes,
|
|
|
|
v.ITEMTYPE_FROM_KODITYPE[session['kodi_type']])
|
|
|
|
with item_fkt() as fkt:
|
2018-05-27 02:54:20 +10:00
|
|
|
plex_type = v.PLEX_TYPE_FROM_KODI_TYPE[session['kodi_type']]
|
2018-02-06 03:48:50 +11:00
|
|
|
fkt.updatePlaystate(mark_played,
|
|
|
|
session['viewCount'],
|
|
|
|
resume,
|
|
|
|
session['duration'],
|
|
|
|
session['file_id'],
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.unix_date_to_kodi(
|
|
|
|
utils.unix_timestamp()),
|
2018-05-27 02:54:20 +10:00
|
|
|
plex_type)
|
2016-03-25 04:52:02 +11:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def sync_fanart(self, missing_only=True, refresh=False):
|
2016-09-11 03:49:03 +10:00
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
Throw items to the fanart queue in order to download missing (or all)
|
|
|
|
additional fanart.
|
2016-09-11 03:49:03 +10:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
missing_only=True False will start look-up for EVERY item
|
|
|
|
refresh=False True will force refresh all external fanart
|
2016-09-11 03:49:03 +10:00
|
|
|
"""
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.settings('FanartTV') == 'false':
|
2018-05-13 22:42:58 +10:00
|
|
|
return
|
2017-01-05 06:57:16 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
2018-04-18 04:18:25 +10:00
|
|
|
if missing_only:
|
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
items = plex_db.get_missing_fanart()
|
|
|
|
LOG.info('Trying to get %s additional fanart', len(items))
|
|
|
|
else:
|
|
|
|
items = []
|
|
|
|
for plex_type in (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_SHOW):
|
|
|
|
items.extend(plex_db.itemsByType(plex_type))
|
|
|
|
LOG.info('Trying to get ALL additional fanart for %s items',
|
|
|
|
len(items))
|
2018-05-13 23:22:03 +10:00
|
|
|
if not items:
|
|
|
|
return
|
2016-09-11 03:49:03 +10:00
|
|
|
# Shuffle the list to not always start out identically
|
2016-09-11 19:29:51 +10:00
|
|
|
shuffle(items)
|
2018-05-13 23:22:03 +10:00
|
|
|
# Checking FanartTV for %s items
|
2018-06-22 03:24:37 +10:00
|
|
|
self.fanartqueue.put(artwork.ArtworkSyncMessage(
|
|
|
|
utils.lang(30018) % len(items)))
|
2018-09-05 00:54:06 +10:00
|
|
|
for item in items:
|
2016-09-11 03:49:03 +10:00
|
|
|
self.fanartqueue.put({
|
2017-02-02 22:27:21 +11:00
|
|
|
'plex_id': item['plex_id'],
|
|
|
|
'plex_type': item['plex_type'],
|
2016-09-11 03:49:03 +10:00
|
|
|
'refresh': refresh
|
|
|
|
})
|
2018-05-13 23:22:03 +10:00
|
|
|
# FanartTV lookup completed
|
2018-06-22 03:24:37 +10:00
|
|
|
self.fanartqueue.put(artwork.ArtworkSyncMessage(utils.lang(30019)))
|
2016-09-11 03:49:03 +10:00
|
|
|
|
2017-08-22 02:53:38 +10:00
|
|
|
def triage_lib_scans(self):
|
|
|
|
"""
|
2017-08-22 03:38:41 +10:00
|
|
|
Decides what to do if state.RUN_LIB_SCAN has been set. E.g. manually
|
|
|
|
triggered full or repair syncs
|
2017-08-22 02:53:38 +10:00
|
|
|
"""
|
|
|
|
if state.RUN_LIB_SCAN in ("full", "repair"):
|
2018-10-24 16:08:32 +11:00
|
|
|
set_library_scan_toggle()
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.info('Full library scan requested, starting')
|
2018-10-24 16:08:32 +11:00
|
|
|
self.start_library_sync(show_dialog=True,
|
|
|
|
repair=state.RUN_LIB_SCAN == 'repair',
|
|
|
|
block=True)
|
|
|
|
if self.sync_successful:
|
2018-04-18 04:18:25 +10:00
|
|
|
# Full library sync finished
|
2018-06-22 03:24:37 +10:00
|
|
|
self.show_kodi_note(utils.lang(39407))
|
2018-04-18 04:18:25 +10:00
|
|
|
elif not self.suspend_item_sync():
|
|
|
|
self.force_dialog = True
|
|
|
|
# ERROR in library sync
|
2018-06-22 03:24:37 +10:00
|
|
|
self.show_kodi_note(utils.lang(39410), icon='error')
|
2018-04-18 04:18:25 +10:00
|
|
|
self.force_dialog = False
|
2017-08-22 02:53:38 +10:00
|
|
|
elif state.RUN_LIB_SCAN == 'fanart':
|
|
|
|
# Only look for missing fanart (No)
|
|
|
|
# or refresh all fanart (Yes)
|
2018-09-19 00:26:40 +10:00
|
|
|
from .windows import optionsdialog
|
|
|
|
refresh = optionsdialog.show(utils.lang(29999),
|
|
|
|
utils.lang(39223),
|
|
|
|
utils.lang(39224), # refresh all
|
|
|
|
utils.lang(39225)) == 0
|
2018-04-18 04:18:25 +10:00
|
|
|
self.sync_fanart(missing_only=not refresh, refresh=refresh)
|
2017-08-22 02:53:38 +10:00
|
|
|
elif state.RUN_LIB_SCAN == 'textures':
|
|
|
|
artwork.Artwork().fullTextureCacheSync()
|
|
|
|
else:
|
|
|
|
raise NotImplementedError('Library scan not defined: %s'
|
|
|
|
% state.RUN_LIB_SCAN)
|
2018-10-24 16:08:32 +11:00
|
|
|
|
|
|
|
def onLibrary_scan_finished(self, successful):
|
|
|
|
"""
|
|
|
|
Hit this after the full sync has finished
|
|
|
|
"""
|
|
|
|
self.sync_successful = successful
|
|
|
|
self.last_full_sync = utils.unix_timestamp()
|
|
|
|
set_library_scan_toggle(boolean=False)
|
|
|
|
try:
|
|
|
|
self.lock.release()
|
|
|
|
except backgroundthread.threading.ThreadError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def start_library_sync(self, show_dialog=None, repair=False, block=False):
|
|
|
|
show_dialog = show_dialog if show_dialog is not None else state.SYNC_DIALOG
|
|
|
|
if block:
|
|
|
|
self.lock.acquire()
|
|
|
|
library_sync.start(show_dialog, repair, self.onLibrary_scan_finished)
|
|
|
|
# Will block until scan is finished
|
|
|
|
self.lock.acquire()
|
|
|
|
self.lock.release()
|
|
|
|
else:
|
|
|
|
library_sync.start(show_dialog, repair, self.onLibrary_scan_finished)
|
2017-08-22 02:53:38 +10:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def run(self):
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
2018-04-18 04:18:25 +10:00
|
|
|
self._run_internal()
|
2018-10-24 16:08:32 +11:00
|
|
|
except:
|
2017-05-17 18:09:50 +10:00
|
|
|
state.DB_SCAN = False
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.window('plex_dbScan', clear=True)
|
2018-10-25 02:19:36 +11:00
|
|
|
utils.ERROR(txt='Sync.py crashed', notify=True)
|
2015-12-25 07:07:00 +11:00
|
|
|
raise
|
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def _run_internal(self):
|
2018-10-25 02:19:36 +11:00
|
|
|
LOG.info("---===### Starting Sync ###===---")
|
2018-10-24 16:08:32 +11:00
|
|
|
install_sync_done = utils.settings('SyncInstallRunDone') == 'true'
|
|
|
|
|
|
|
|
playlist_monitor = None
|
2018-04-18 04:18:25 +10:00
|
|
|
initial_sync_done = False
|
|
|
|
kodi_db_version_checked = False
|
|
|
|
last_processing = 0
|
2018-05-20 22:28:56 +10:00
|
|
|
last_time_sync = 0
|
2018-06-22 03:24:37 +10:00
|
|
|
one_day_in_seconds = 60 * 60 * 24
|
2016-12-28 03:33:52 +11:00
|
|
|
# Link to Websocket queue
|
2018-01-07 01:19:12 +11:00
|
|
|
queue = state.WEBSOCKET_QUEUE
|
2016-03-25 04:52:02 +11:00
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
if (not path_ops.exists(v.DB_VIDEO_PATH) or
|
|
|
|
not path_ops.exists(v.DB_TEXTURE_PATH) or
|
|
|
|
(state.ENABLE_MUSIC and not path_ops.exists(v.DB_MUSIC_PATH))):
|
2018-04-18 04:18:25 +10:00
|
|
|
# Database does not exists
|
2018-10-24 16:08:32 +11:00
|
|
|
LOG.error('The current Kodi version is incompatible')
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG.error('Current Kodi version: %s', utils.try_decode(
|
2018-04-18 04:18:25 +10:00
|
|
|
xbmc.getInfoLabel('System.BuildVersion')))
|
|
|
|
# "Current Kodi version is unsupported, cancel lib sync"
|
2018-09-19 00:26:40 +10:00
|
|
|
utils.messageDialog(utils.lang(29999), utils.lang(39403))
|
2018-04-18 04:18:25 +10:00
|
|
|
return
|
2016-04-08 21:57:55 +10:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
# Do some initializing
|
2018-10-24 16:08:32 +11:00
|
|
|
# Ensure that Plex DB is set-up
|
|
|
|
plex_db.initialize()
|
|
|
|
# Hack to speed up look-ups for actors (giant table!)
|
|
|
|
utils.create_actor_db_index()
|
2018-04-18 04:18:25 +10:00
|
|
|
# Run start up sync
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG.info("Db version: %s", utils.settings('dbCreatedWithVersion'))
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.info('Refreshing video nodes and playlists now')
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
2018-10-08 02:47:32 +11:00
|
|
|
# Setup the paths for addon-paths (even when using direct paths)
|
2018-04-18 04:18:25 +10:00
|
|
|
kodi_db.setup_path_table()
|
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
while not self.isCanceled():
|
2016-04-08 17:11:03 +10:00
|
|
|
# In the event the server goes offline
|
2018-10-24 16:08:32 +11:00
|
|
|
while self.isSuspended():
|
|
|
|
if self.isCanceled():
|
2015-12-25 07:07:00 +11:00
|
|
|
# Abort was requested while waiting. We should exit
|
2018-10-25 02:19:36 +11:00
|
|
|
LOG.info("###===--- Sync 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
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
if not install_sync_done:
|
|
|
|
# Very FIRST sync ever upon installation or reset of Kodi DB
|
|
|
|
set_library_scan_toggle()
|
2018-05-20 22:28:56 +10:00
|
|
|
# Initialize time offset Kodi - PMS
|
2018-10-24 16:08:32 +11:00
|
|
|
library_sync.sync_pms_time()
|
2018-05-20 22:28:56 +10:00
|
|
|
last_time_sync = utils.unix_timestamp()
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.info('Initial start-up full sync starting')
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(true)')
|
2018-10-24 16:08:32 +11:00
|
|
|
# This call will block until scan is completed
|
|
|
|
self.start_library_sync(show_dialog=True, block=True)
|
|
|
|
if self.sync_successful:
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.info('Initial start-up full sync successful')
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.settings('SyncInstallRunDone', value='true')
|
2018-10-24 16:08:32 +11:00
|
|
|
install_sync_done = True
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.settings('dbCreatedWithVersion', v.ADDON_VERSION)
|
2018-04-18 04:18:25 +10:00
|
|
|
self.force_dialog = False
|
|
|
|
kodi_db_version_checked = True
|
2018-10-24 16:08:32 +11:00
|
|
|
if library_sync.PLAYLIST_SYNC_ENABLED:
|
|
|
|
from . import playlists
|
2018-08-06 02:09:48 +10:00
|
|
|
playlist_monitor = playlists.kodi_playlist_monitor()
|
2018-05-13 22:42:58 +10:00
|
|
|
self.sync_fanart()
|
2018-04-29 22:12:39 +10:00
|
|
|
self.fanartthread.start()
|
2018-04-18 04:18:25 +10:00
|
|
|
else:
|
|
|
|
LOG.error('Initial start-up full sync unsuccessful')
|
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
|
|
|
|
|
|
|
elif not kodi_db_version_checked:
|
2017-08-21 16:01:48 +10:00
|
|
|
# Install sync was already done, don't force-show dialogs
|
|
|
|
self.force_dialog = False
|
2015-12-25 07:07:00 +11:00
|
|
|
# Verify the validity of the database
|
2018-06-22 03:24:37 +10:00
|
|
|
current_version = utils.settings('dbCreatedWithVersion')
|
|
|
|
if not utils.compare_version(current_version,
|
|
|
|
v.MIN_DB_VERSION):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.warn("Db version out of date: %s minimum version "
|
2018-04-18 04:18:25 +10:00
|
|
|
"required: %s", current_version, v.MIN_DB_VERSION)
|
2016-03-08 21:47:46 +11:00
|
|
|
# DB out of date. Proceed to recreate?
|
2018-09-19 00:26:40 +10:00
|
|
|
if not utils.yesno_dialog(utils.lang(29999),
|
|
|
|
utils.lang(39401)):
|
2018-04-16 02:33:20 +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
|
2018-09-19 00:26:40 +10:00
|
|
|
utils.messageDialog(utils.lang(29999),
|
|
|
|
'%s%s' % (utils.lang(29999),
|
|
|
|
utils.lang(39402)))
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2018-05-15 04:51:14 +10:00
|
|
|
utils.reset(ask_user=False)
|
2016-03-02 03:49:16 +11:00
|
|
|
break
|
2018-04-18 04:18:25 +10:00
|
|
|
kodi_db_version_checked = True
|
|
|
|
|
|
|
|
elif not initial_sync_done:
|
|
|
|
# First sync upon PKC restart. Skipped if very first sync upon
|
|
|
|
# PKC installation has been completed
|
2018-10-24 16:08:32 +11:00
|
|
|
set_library_scan_toggle()
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.info('Doing initial sync on Kodi startup')
|
2018-06-03 21:48:00 +10:00
|
|
|
if state.SUSPEND_SYNC:
|
|
|
|
LOG.warning('Forcing startup sync even if Kodi is playing')
|
|
|
|
state.SUSPEND_SYNC = False
|
2018-10-24 16:08:32 +11:00
|
|
|
self.start_library_sync(block=True)
|
|
|
|
if self.sync_successful:
|
2018-04-18 04:18:25 +10:00
|
|
|
initial_sync_done = True
|
|
|
|
LOG.info('Done initial sync on Kodi startup')
|
2018-10-24 16:08:32 +11:00
|
|
|
if library_sync.PLAYLIST_SYNC_ENABLED:
|
|
|
|
from . import playlists
|
2018-08-06 02:09:48 +10:00
|
|
|
playlist_monitor = playlists.kodi_playlist_monitor()
|
2018-04-29 22:26:53 +10:00
|
|
|
artwork.Artwork().cache_major_artwork()
|
2018-05-13 22:42:58 +10:00
|
|
|
self.sync_fanart()
|
2018-04-29 22:26:53 +10:00
|
|
|
self.fanartthread.start()
|
2018-04-18 04:18:25 +10:00
|
|
|
else:
|
|
|
|
LOG.info('Startup sync has not yet been successful')
|
2016-01-28 06:41:28 +11:00
|
|
|
|
2018-10-24 16:08:32 +11:00
|
|
|
# Currently no db scan, so we could start a new scan
|
2017-05-17 18:09:50 +10:00
|
|
|
elif state.DB_SCAN is False:
|
2016-01-28 06:41:28 +11:00
|
|
|
# Full scan was requested from somewhere else, e.g. userclient
|
2017-08-22 02:53:38 +10:00
|
|
|
if state.RUN_LIB_SCAN is not None:
|
|
|
|
# Force-show dialogs since they are user-initiated
|
|
|
|
self.force_dialog = True
|
|
|
|
self.triage_lib_scans()
|
|
|
|
self.force_dialog = False
|
2018-10-24 16:08:32 +11:00
|
|
|
# Reset the flag
|
|
|
|
state.RUN_LIB_SCAN = None
|
2017-08-22 02:53:38 +10:00
|
|
|
continue
|
2018-10-24 16:08:32 +11:00
|
|
|
|
2017-08-22 02:53:38 +10:00
|
|
|
# Standard syncs - don't force-show dialogs
|
2018-10-24 16:08:32 +11:00
|
|
|
now = utils.unix_timestamp()
|
2017-08-22 02:53:38 +10:00
|
|
|
self.force_dialog = False
|
2018-10-24 16:08:32 +11:00
|
|
|
if (now - self.last_full_sync > state.FULL_SYNC_INTERVALL):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.info('Doing scheduled full library scan')
|
2018-10-24 16:08:32 +11:00
|
|
|
set_library_scan_toggle()
|
2018-04-18 04:18:25 +10:00
|
|
|
success = self.maintain_views()
|
|
|
|
if success:
|
2018-10-24 16:08:32 +11:00
|
|
|
success = library_sync.start()
|
2018-04-18 04:18:25 +10:00
|
|
|
if not success and not self.suspend_item_sync():
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.error('Could not finish scheduled full sync')
|
2017-08-22 02:53:38 +10:00
|
|
|
self.force_dialog = True
|
2018-06-22 03:24:37 +10:00
|
|
|
self.show_kodi_note(utils.lang(39410),
|
2018-04-18 04:18:25 +10:00
|
|
|
icon='error')
|
2017-08-22 02:53:38 +10:00
|
|
|
self.force_dialog = False
|
2018-04-18 04:18:25 +10:00
|
|
|
elif success:
|
2018-10-24 16:08:32 +11:00
|
|
|
self.last_full_sync = now
|
2018-04-18 04:18:25 +10:00
|
|
|
# Full library sync finished successfully
|
2018-06-22 03:24:37 +10:00
|
|
|
self.show_kodi_note(utils.lang(39407))
|
2018-04-18 04:18:25 +10:00
|
|
|
else:
|
|
|
|
LOG.info('Full sync interrupted')
|
|
|
|
elif now - last_time_sync > one_day_in_seconds:
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.info('Starting daily time sync')
|
2018-10-24 16:08:32 +11:00
|
|
|
library_sync.sync_pms_time()
|
2018-04-18 04:18:25 +10:00
|
|
|
last_time_sync = now
|
2018-03-10 22:58:11 +11:00
|
|
|
elif not state.BACKGROUND_SYNC_DISABLED:
|
2017-08-22 02:53:38 +10:00
|
|
|
# Check back whether we should process something
|
|
|
|
# Only do this once every while (otherwise, potentially
|
|
|
|
# many screen refreshes lead to flickering)
|
2018-04-18 04:18:25 +10:00
|
|
|
if now - last_processing > 5:
|
|
|
|
last_processing = now
|
|
|
|
self.process_items()
|
2017-08-22 02:53:38 +10:00
|
|
|
# See if there is a PMS message we need to handle
|
|
|
|
try:
|
|
|
|
message = queue.get(block=False)
|
2018-10-24 16:08:32 +11:00
|
|
|
except backgroundthread.Queue.Empty:
|
2018-04-18 04:18:25 +10:00
|
|
|
pass
|
2017-08-22 02:53:38 +10:00
|
|
|
# Got a message from PMS; process it
|
|
|
|
else:
|
2018-04-18 04:18:25 +10:00
|
|
|
self.process_message(message)
|
2017-08-22 02:53:38 +10:00
|
|
|
queue.task_done()
|
2018-04-18 04:18:25 +10:00
|
|
|
# Sleep just a bit
|
|
|
|
xbmc.sleep(10)
|
2017-08-22 02:53:38 +10:00
|
|
|
continue
|
2017-08-21 16:03:08 +10:00
|
|
|
xbmc.sleep(100)
|
2018-04-28 17:12:29 +10:00
|
|
|
# Shut down playlist monitoring
|
2018-05-01 22:48:49 +10:00
|
|
|
if playlist_monitor:
|
|
|
|
playlist_monitor.stop()
|
2016-04-10 00:57:45 +10:00
|
|
|
# doUtils could still have a session open due to interrupted sync
|
|
|
|
try:
|
2018-04-18 04:18:25 +10:00
|
|
|
DU().stopSession()
|
2018-10-24 16:08:32 +11:00
|
|
|
except AttributeError:
|
2016-04-10 00:57:45 +10:00
|
|
|
pass
|
2018-10-25 02:19:36 +11:00
|
|
|
LOG.info("###===--- Sync Stopped ---===###")
|