PlexKodiConnect/resources/lib/sync.py

726 lines
31 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2015-12-25 07:07:00 +11:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
2017-08-18 18:38:03 +10:00
from logging import getLogger
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-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
"""
The one and only library sync thread. Spawn only 1!
2016-03-25 04:52:02 +11:00
"""
def __init__(self):
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?
# Show sync dialog even if user deactivated?
2018-10-24 16:08:32 +11:00
self.force_dialog = False
# 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
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():
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
icon: "plex": shows Plex icon
"error": shows Kodi error icon
2016-02-12 00:44:11 +11:00
"""
if state.SYNC_DIALOG is not True and self.force_dialog is not True:
return
if icon == "plex":
2018-06-22 03:24:37 +10:00
utils.dialog('notification',
heading='{plex}',
message=message,
icon='{plex}',
sound=False)
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
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
def multi_delete(self, liste, delete_list):
2016-03-25 04:52:02 +11:00
"""
Deletes the list items of liste at the positions in delete_list
(which can be in any arbitrary order)
2016-03-25 04:52:02 +11:00
"""
indexes = sorted(delete_list, reverse=True)
2016-03-25 04:52:02 +11:00
for index in indexes:
del liste[index]
return liste
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
"""
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
if item['state'] == 9:
successful = self.process_deleteditems(item)
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
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':
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'],
'plex_type': item['type'],
2016-09-18 03:12:32 +10:00
'refresh': False
})
if successful is True:
delete_list.append(i)
else:
# 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)
delete_list.append(i)
2016-03-25 04:52:02 +11:00
# Get rid of the items we just processed
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):
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:
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:
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:
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):
if item['type'] == v.PLEX_TYPE_MOVIE:
2018-04-16 02:33:20 +10:00
LOG.debug("Removing movie %s", item['ratingKey'])
self.update_kodi_video_library = True
2016-03-28 04:06:36 +11:00
with itemtypes.Movies() as movie:
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",
item['ratingKey'])
self.update_kodi_video_library = True
2016-03-28 04:06:36 +11:00
with itemtypes.TVShows() as show:
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'])
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
"processing queue" for later
2016-03-25 04:52:02 +11:00
"""
for item in data:
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
typus = v.PLEX_TYPE_FROM_WEBSOCKET[int(item['type'])]
if typus == v.PLEX_TYPE_CLIP:
# No need to process extras or trailers
continue
status = int(item['state'])
if typus == 'playlist':
2018-10-24 16:08:32 +11:00
if not library_sync.PLAYLIST_SYNC_ENABLED:
continue
2018-07-12 05:24:27 +10:00
playlists.websocket(plex_id=unicode(item['itemID']),
status=status)
elif status == 9:
# Immediately and always process deletions (as the PMS will
# send additional message with other codes)
self.items_to_process.append({
'state': status,
'type': typus,
'ratingKey': str(item['itemID']),
'timestamp': utils.unix_timestamp(),
'attempt': 0
})
elif typus in (v.PLEX_TYPE_MOVIE,
v.PLEX_TYPE_EPISODE,
v.PLEX_TYPE_SONG) and status == 5:
plex_id = str(item['itemID'])
# Have we already added this element for processing?
for existing_item in self.items_to_process:
if existing_item['ratingKey'] == plex_id:
break
else:
# Haven't added this element to the queue yet
self.items_to_process.append({
'state': status,
'type': typus,
'ratingKey': plex_id,
'timestamp': utils.unix_timestamp(),
'attempt': 0
})
2016-03-25 04:52:02 +11: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
elif item['Activity'].get('Context') is None:
# Not related to any Plex element, but entire library
continue
elif item['Activity']['type'] != 'library.refresh.items':
# Not the type of message relevant for us
continue
plex_id = PF.GetPlexKeyNumber(item['Activity']['Context']['key'])[1]
if plex_id == '':
# Likely a Plex id like /library/metadata/3/children
continue
# 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)
continue
# Have we already added this element?
for existing_item in self.items_to_process:
if existing_item['ratingKey'] == plex_id:
break
else:
# Haven't added this element to the queue yet
self.items_to_process.append({
'state': None, # Don't need a state here
'type': kodi_info[5],
'ratingKey': plex_id,
'timestamp': utils.unix_timestamp(),
'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
"""
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
continue
plex_id = item['ratingKey']
skip = False
2018-02-04 22:22:10 +11:00
for pid in (0, 1, 2):
if plex_id == state.PLAYER_STATES[pid]['plex_id']:
# Kodi is playing this item - no need to set the playstate
skip = True
if skip:
continue
session_key = item['sessionKey']
# Do we already have a sessionKey stored?
if session_key not in self.session_keys:
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':
# Not our PMS, we are not authorized to get the sessions
# On the bright side, it must be us playing :-)
self.session_keys[session_key] = {}
else:
# PMS is ours - get all current sessions
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',
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 '
'playstate update', session_key)
continue
# Attach Kodi info to the session
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':
# 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)
if not state.PLEX_TOKEN and session['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
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 '
'the session username %s with userid %s',
state.PLEX_USERNAME,
state.PLEX_USER_ID,
session['username'],
session['userId'])
continue
# 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:
xml = PF.GetPlexMetadata(plex_id)
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',
plex_id)
continue
2018-06-22 03:24:37 +10:00
api = API(xml[0])
2018-02-12 00:42:49 +11:00
userdata = api.userdata()
session['duration'] = userdata['Runtime']
session['viewCount'] = userdata['PlayCount']
# Sometimes, Plex tells us resume points in milliseconds and
# not in seconds - thank you very much!
if item['viewOffset'] > session['duration']:
resume = item['viewOffset'] / 1000
else:
resume = item['viewOffset']
if resume < v.IGNORE_SECONDS_AT_START:
continue
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',
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',
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:
plex_type = v.PLEX_TYPE_FROM_KODI_TYPE[session['kodi_type']]
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()),
plex_type)
2016-03-25 04:52:02 +11:00
def sync_fanart(self, missing_only=True, refresh=False):
"""
Throw items to the fanart queue in order to download missing (or all)
additional fanart.
missing_only=True False will start look-up for EVERY item
refresh=False True will force refresh all external fanart
"""
2018-06-22 03:24:37 +10:00
if utils.settings('FanartTV') == 'false':
return
with plexdb.Get_Plex_DB() as plex_db:
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))
if not items:
return
# Shuffle the list to not always start out identically
2016-09-11 19:29:51 +10:00
shuffle(items)
# 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:
self.fanartqueue.put({
2017-02-02 22:27:21 +11:00
'plex_id': item['plex_id'],
'plex_type': item['plex_type'],
'refresh': refresh
})
# FanartTV lookup completed
2018-06-22 03:24:37 +10:00
self.fanartqueue.put(artwork.ArtworkSyncMessage(utils.lang(30019)))
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:
# Full library sync finished
2018-06-22 03:24:37 +10:00
self.show_kodi_note(utils.lang(39407))
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')
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
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:
self._run_internal()
2018-10-24 16:08:32 +11:00
except:
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
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
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
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))):
# 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(
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))
return
# 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()
# Run start up sync
2018-06-22 03:24:37 +10:00
LOG.info("Db version: %s", utils.settings('dbCreatedWithVersion'))
LOG.info('Refreshing video nodes and playlists now')
with kodidb.GetKodiDB('video') as kodi_db:
# Setup the paths for addon-paths (even when using direct paths)
kodi_db.setup_path_table()
2018-10-24 16:08:32 +11:00
while not self.isCanceled():
# 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()
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:
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)
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
playlist_monitor = playlists.kodi_playlist_monitor()
self.sync_fanart()
self.fanartthread.start()
else:
LOG.error('Initial start-up full sync unsuccessful')
xbmc.executebuiltin('InhibitIdleShutdown(false)')
elif not kodi_db_version_checked:
# 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 "
"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:
utils.reset(ask_user=False)
break
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()
LOG.info('Doing initial sync on Kodi startup')
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:
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
playlist_monitor = playlists.kodi_playlist_monitor()
2018-04-29 22:26:53 +10:00
artwork.Artwork().cache_major_artwork()
self.sync_fanart()
2018-04-29 22:26:53 +10:00
self.fanartthread.start()
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
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()
success = self.maintain_views()
if success:
2018-10-24 16:08:32 +11:00
success = library_sync.start()
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),
icon='error')
2017-08-22 02:53:38 +10:00
self.force_dialog = False
elif success:
2018-10-24 16:08:32 +11:00
self.last_full_sync = now
# Full library sync finished successfully
2018-06-22 03:24:37 +10:00
self.show_kodi_note(utils.lang(39407))
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()
last_time_sync = now
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)
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:
pass
2017-08-22 02:53:38 +10:00
# Got a message from PMS; process it
else:
self.process_message(message)
2017-08-22 02:53:38 +10:00
queue.task_done()
# Sleep just a bit
xbmc.sleep(10)
2017-08-22 02:53:38 +10:00
continue
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:
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 ---===###")