jellyfin-kodi/jellyfin_kodi/library.py

837 lines
28 KiB
Python
Raw Normal View History

2018-09-06 08:36:32 +00:00
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
2018-09-06 08:36:32 +00:00
##################################################################################################
import logging
import threading
from datetime import datetime, timedelta
from six.moves import queue as Queue
from kodi_six import xbmc, xbmcgui
2018-09-06 08:36:32 +00:00
from objects import Movies, TVShows, MusicVideos, Music
from database import Database, jellyfin_db, get_sync, save_sync
2018-09-06 08:36:32 +00:00
from full_sync import FullSync
from views import Views
from downloader import GetItemWorker
from helper import translate, api, stop, settings, window, dialog, event, LibraryException
from helper.utils import split_list, set_screensaver, get_screensaver
from jellyfin import Jellyfin
2018-09-06 08:36:32 +00:00
##################################################################################################
2019-10-03 02:14:54 +00:00
LOG = logging.getLogger("JELLYFIN." + __name__)
LIMIT = int(settings('limitIndex') or 15)
2018-10-01 00:35:31 +00:00
DTHREADS = int(settings('limitThreads') or 3)
2018-09-06 08:36:32 +00:00
MEDIA = {
'Movie': Movies,
'BoxSet': Movies,
'MusicVideo': MusicVideos,
'Series': TVShows,
'Season': TVShows,
'Episode': TVShows,
'MusicAlbum': Music,
'MusicArtist': Music,
'AlbumArtist': Music,
'Audio': Music
}
##################################################################################################
class Library(threading.Thread):
started = False
stop_thread = False
suspend = False
pending_refresh = False
2018-10-04 03:36:57 +00:00
screensaver = None
progress_updates = None
total_updates = 0
2018-09-06 08:36:32 +00:00
def __init__(self, monitor):
2019-01-25 15:30:30 +00:00
self.media = {'Movies': Movies, 'TVShows': TVShows, 'MusicVideos': MusicVideos, 'Music': Music}
self.MEDIA = MEDIA
2018-09-06 08:36:32 +00:00
self.direct_path = settings('useDirectPaths') == "1"
2018-10-04 03:36:57 +00:00
self.progress_display = int(settings('syncProgress') or 50)
2018-09-06 08:36:32 +00:00
self.monitor = monitor
2018-10-04 05:41:31 +00:00
self.player = monitor.monitor.player
self.server = Jellyfin().get_client()
2018-09-06 08:36:32 +00:00
self.updated_queue = Queue.Queue()
self.userdata_queue = Queue.Queue()
self.removed_queue = Queue.Queue()
self.updated_output = self.__new_queues__()
self.userdata_output = self.__new_queues__()
self.removed_output = self.__new_queues__()
2018-10-04 05:41:31 +00:00
self.notify_output = Queue.Queue()
2018-09-06 08:36:32 +00:00
self.jellyfin_threads = []
2018-09-06 08:36:32 +00:00
self.download_threads = []
2018-10-04 05:41:31 +00:00
self.notify_threads = []
2018-09-06 08:36:32 +00:00
self.writer_threads = {'updated': [], 'userdata': [], 'removed': []}
self.database_lock = threading.Lock()
self.music_database_lock = threading.Lock()
threading.Thread.__init__(self)
def __new_queues__(self):
return {
'Movie': Queue.Queue(),
'BoxSet': Queue.Queue(),
'MusicVideo': Queue.Queue(),
'Series': Queue.Queue(),
'Season': Queue.Queue(),
'Episode': Queue.Queue(),
'MusicAlbum': Queue.Queue(),
'MusicArtist': Queue.Queue(),
'AlbumArtist': Queue.Queue(),
'Audio': Queue.Queue()
}
def run(self):
LOG.info("--->[ library ]")
2018-09-06 08:36:32 +00:00
if not self.startup():
self.stop_client()
window('jellyfin_startup.bool', True)
2018-09-06 08:36:32 +00:00
while not self.stop_thread:
try:
self.service()
except LibraryException as error:
break
except Exception as error:
LOG.exception(error)
break
if self.monitor.waitForAbort(2):
break
LOG.info("---<[ library ]")
2018-09-06 08:36:32 +00:00
2019-01-10 23:38:57 +00:00
def test_databases(self):
''' Open the databases to test if the file exists.
'''
with Database('video'), Database('music'):
pass
2019-01-10 23:38:57 +00:00
2018-09-06 08:36:32 +00:00
@stop()
def service(self):
2019-07-09 20:05:28 +00:00
2018-09-06 08:36:32 +00:00
''' If error is encountered, it will rerun this function.
Start new "daemon threads" to process library updates.
(actual daemon thread is not supported in Kodi)
'''
self.download_threads = [thread for thread in self.download_threads if not thread.is_done]
self.writer_threads['updated'] = [thread for thread in self.writer_threads['updated'] if not thread.is_done]
self.writer_threads['userdata'] = [thread for thread in self.writer_threads['userdata'] if not thread.is_done]
self.writer_threads['removed'] = [thread for thread in self.writer_threads['removed'] if not thread.is_done]
2018-09-06 08:36:32 +00:00
2018-11-19 06:12:46 +00:00
if not self.player.isPlayingVideo() or settings('syncDuringPlay.bool') or xbmc.getCondVisibility('VideoPlayer.Content(livetv)'):
2018-10-04 03:36:57 +00:00
self.worker_downloads()
2018-11-17 09:46:22 +00:00
self.worker_sort()
self.worker_updates()
self.worker_userdata()
self.worker_remove()
self.worker_notify()
2018-10-04 03:36:57 +00:00
if self.pending_refresh:
window('jellyfin_sync.bool', True)
2018-10-04 03:36:57 +00:00
2018-11-17 09:46:22 +00:00
if self.total_updates > self.progress_display:
queue_size = self.worker_queue_size()
2018-10-04 03:36:57 +00:00
if self.progress_updates is None:
2018-10-04 03:36:57 +00:00
self.progress_updates = xbmcgui.DialogProgressBG()
self.progress_updates.create(translate('addon_name'), translate(33178))
self.progress_updates.update(int((float(self.total_updates - queue_size) / float(self.total_updates)) * 100), message="%s: %s" % (translate(33178), queue_size))
elif queue_size:
self.progress_updates.update(int((float(self.total_updates - queue_size) / float(self.total_updates)) * 100), message="%s: %s" % (translate(33178), queue_size))
2018-10-04 03:36:57 +00:00
else:
self.progress_updates.update(int((float(self.total_updates - queue_size) / float(self.total_updates)) * 100), message=translate(33178))
2018-10-04 03:36:57 +00:00
if not settings('dbSyncScreensaver.bool') and self.screensaver is None:
xbmc.executebuiltin('InhibitIdleShutdown(true)')
self.screensaver = get_screensaver()
set_screensaver(value="")
2019-07-09 20:05:28 +00:00
2019-10-03 02:14:54 +00:00
if (self.pending_refresh and not self.download_threads and not self.writer_threads['updated'] and not self.writer_threads['userdata'] and not self.writer_threads['removed']):
2018-10-04 03:36:57 +00:00
self.pending_refresh = False
self.save_last_sync()
self.total_updates = 0
window('jellyfin_sync', clear=True)
2018-10-04 03:36:57 +00:00
if self.progress_updates:
self.progress_updates.close()
self.progress_updates = None
if not settings('dbSyncScreensaver.bool') and self.screensaver is not None:
xbmc.executebuiltin('InhibitIdleShutdown(false)')
set_screensaver(value=self.screensaver)
self.screensaver = None
2019-10-03 02:14:54 +00:00
if xbmc.getCondVisibility('Container.Content(musicvideos)'): # Prevent cursor from moving
2018-10-04 03:36:57 +00:00
xbmc.executebuiltin('Container.Refresh')
2019-10-03 02:14:54 +00:00
else: # Update widgets
2018-10-04 03:36:57 +00:00
xbmc.executebuiltin('UpdateLibrary(video)')
if xbmc.getCondVisibility('Window.IsMedia'):
xbmc.executebuiltin('Container.Refresh')
2018-10-04 03:36:57 +00:00
def stop_client(self):
self.stop_thread = True
2019-01-25 11:32:29 +00:00
def enable_pending_refresh(self):
''' When there's an active thread. Let the main thread know.
'''
self.pending_refresh = True
window('jellyfin_sync.bool', True)
2019-01-25 11:32:29 +00:00
2018-10-04 03:36:57 +00:00
def worker_queue_size(self):
''' Get how many items are queued up for worker threads.
2018-10-04 03:36:57 +00:00
'''
total = 0
for queues in self.updated_output:
total += self.updated_output[queues].qsize()
for queues in self.userdata_output:
total += self.userdata_output[queues].qsize()
for queues in self.removed_output:
total += self.removed_output[queues].qsize()
return total
def worker_downloads(self):
''' Get items from jellyfin and place them in the appropriate queues.
2018-10-04 03:36:57 +00:00
'''
2018-09-06 08:36:32 +00:00
for queue in ((self.updated_queue, self.updated_output), (self.userdata_queue, self.userdata_output)):
2018-10-01 00:35:31 +00:00
if queue[0].qsize() and len(self.download_threads) < DTHREADS:
2019-07-09 20:05:28 +00:00
2018-09-06 08:36:32 +00:00
new_thread = GetItemWorker(self.server, queue[0], queue[1])
new_thread.start()
LOG.info("-->[ q:download/%s ]", id(new_thread))
self.download_threads.append(new_thread)
2018-09-06 08:36:32 +00:00
def worker_sort(self):
2018-10-04 03:36:57 +00:00
''' Get items based on the local jellyfin database and place item in appropriate queues.
2018-10-04 03:36:57 +00:00
'''
if self.removed_queue.qsize() and len(self.jellyfin_threads) < 2:
2018-09-06 08:36:32 +00:00
new_thread = SortWorker(self.removed_queue, self.removed_output)
new_thread.start()
LOG.info("-->[ q:sort/%s ]", id(new_thread))
2018-10-04 03:36:57 +00:00
def worker_updates(self):
''' Update items in the Kodi database.
'''
2018-09-06 08:36:32 +00:00
for queues in self.updated_output:
queue = self.updated_output[queues]
2018-10-07 23:19:06 +00:00
if queue.qsize() and not len(self.writer_threads['updated']):
2018-09-06 08:36:32 +00:00
if queues in ('Audio', 'MusicArtist', 'AlbumArtist', 'MusicAlbum'):
new_thread = UpdateWorker(queue, self.notify_output, self.music_database_lock, "music", self.server, self.direct_path)
2018-09-06 08:36:32 +00:00
else:
new_thread = UpdateWorker(queue, self.notify_output, self.database_lock, "video", self.server, self.direct_path)
2018-09-06 08:36:32 +00:00
new_thread.start()
LOG.info("-->[ q:updated/%s/%s ]", queues, id(new_thread))
self.writer_threads['updated'].append(new_thread)
2019-01-25 11:32:29 +00:00
self.enable_pending_refresh()
2018-09-06 08:36:32 +00:00
2018-10-04 03:36:57 +00:00
def worker_userdata(self):
''' Update userdata in the Kodi database.
'''
2018-09-06 08:36:32 +00:00
for queues in self.userdata_output:
queue = self.userdata_output[queues]
2018-10-07 23:19:06 +00:00
if queue.qsize() and not len(self.writer_threads['userdata']):
2018-09-06 08:36:32 +00:00
if queues in ('Audio', 'MusicArtist', 'AlbumArtist', 'MusicAlbum'):
new_thread = UserDataWorker(queue, self.music_database_lock, "music", self.server, self.direct_path)
else:
new_thread = UserDataWorker(queue, self.database_lock, "video", self.server, self.direct_path)
new_thread.start()
LOG.info("-->[ q:userdata/%s/%s ]", queues, id(new_thread))
self.writer_threads['userdata'].append(new_thread)
2019-01-25 11:32:29 +00:00
self.enable_pending_refresh()
2018-09-06 08:36:32 +00:00
2018-10-04 03:36:57 +00:00
def worker_remove(self):
''' Remove items from the Kodi database.
'''
2018-09-06 08:36:32 +00:00
for queues in self.removed_output:
queue = self.removed_output[queues]
2018-10-07 23:19:06 +00:00
if queue.qsize() and not len(self.writer_threads['removed']):
2018-09-06 08:36:32 +00:00
if queues in ('Audio', 'MusicArtist', 'AlbumArtist', 'MusicAlbum'):
new_thread = RemovedWorker(queue, self.music_database_lock, "music", self.server, self.direct_path)
else:
new_thread = RemovedWorker(queue, self.database_lock, "video", self.server, self.direct_path)
2019-07-09 20:05:28 +00:00
2018-09-06 08:36:32 +00:00
new_thread.start()
LOG.info("-->[ q:removed/%s/%s ]", queues, id(new_thread))
self.writer_threads['removed'].append(new_thread)
2019-01-25 11:32:29 +00:00
self.enable_pending_refresh()
2018-10-04 05:41:31 +00:00
def worker_notify(self):
''' Notify the user of new additions.
'''
2018-10-07 23:19:06 +00:00
if self.notify_output.qsize() and not len(self.notify_threads):
2018-10-04 05:41:31 +00:00
new_thread = NotifyWorker(self.notify_output, self.player)
new_thread.start()
LOG.info("-->[ q:notify/%s ]", id(new_thread))
self.notify_threads.append(new_thread)
2018-09-06 08:36:32 +00:00
def startup(self):
2019-07-09 20:05:28 +00:00
''' Run at startup.
Check databases.
2019-01-10 23:38:57 +00:00
Check for the server plugin.
2018-09-06 08:36:32 +00:00
'''
2019-01-21 13:01:34 +00:00
self.test_databases()
2019-01-10 23:38:57 +00:00
2018-09-06 08:36:32 +00:00
Views().get_views()
Views().get_nodes()
try:
if get_sync()['Libraries']:
try:
2019-01-25 15:30:30 +00:00
with FullSync(self, self.server) as sync:
sync.libraries()
Views().get_nodes()
except Exception as error:
2019-07-09 20:05:28 +00:00
LOG.exception(error)
elif not settings('SyncInstallRunDone.bool'):
2019-07-09 20:05:28 +00:00
2019-01-25 15:30:30 +00:00
with FullSync(self, self.server) as sync:
sync.libraries()
Views().get_nodes()
2018-10-12 05:26:44 +00:00
return True
2018-09-06 08:36:32 +00:00
if settings('SyncInstallRunDone.bool'):
if settings('kodiCompanion.bool'):
if self.server.jellyfin.check_companion_installed():
2019-07-09 20:05:28 +00:00
2019-09-12 20:29:01 +00:00
if not self.fast_sync():
dialog("ok", heading="{jellyfin}", line1=translate(33128))
2018-09-06 08:36:32 +00:00
2019-09-12 20:29:01 +00:00
raise Exception("Failed to retrieve latest updates")
2019-09-12 20:29:01 +00:00
LOG.info("--<[ retrieve changes ]")
2018-09-06 08:36:32 +00:00
else:
raise LibraryException('CompanionMissing')
2018-09-06 08:36:32 +00:00
return True
2018-09-06 08:36:32 +00:00
except LibraryException as error:
LOG.error(error.status)
if error.status in 'SyncLibraryLater':
dialog("ok", heading="{jellyfin}", line1=translate(33129))
2018-09-06 08:36:32 +00:00
settings('SyncInstallRunDone.bool', True)
sync = get_sync()
sync['Libraries'] = []
save_sync(sync)
2018-09-06 08:36:32 +00:00
return True
elif error.status == 'CompanionMissing':
dialog("ok", heading="{jellyfin}", line1=translate(33099))
2018-09-10 04:16:44 +00:00
settings('kodiCompanion.bool', False)
2018-09-06 08:36:32 +00:00
return True
except Exception as error:
LOG.exception(error)
return False
def fast_sync(self):
''' Movie and userdata not provided by server yet.
'''
last_sync = settings('LastIncrementalSync')
filters = ["tvshows", "boxsets", "musicvideos", "music", "movies"]
sync = get_sync()
LOG.info("--[ retrieve changes ] %s", last_sync)
try:
updated = []
userdata = []
removed = []
2018-09-06 08:36:32 +00:00
for media in filters:
2019-09-04 02:58:31 +00:00
result = self.server.jellyfin.get_sync_queue(last_sync, ",".join([x for x in filters if x != media]))
updated.extend(result['ItemsAdded'])
updated.extend(result['ItemsUpdated'])
userdata.extend(result['UserDataChanged'])
removed.extend(result['ItemsRemoved'])
total = len(updated) + len(userdata)
if total > int(settings('syncIndicator') or 99):
''' Inverse yes no, in case the dialog is forced closed by Kodi.
'''
if dialog("yesno", heading="{jellyfin}", line1=translate(33172).replace('{number}', str(total)), nolabel=translate(107), yeslabel=translate(106)):
LOG.warning("Large updates skipped.")
return True
self.updated(updated)
self.userdata(userdata)
self.removed(removed)
2018-09-06 08:36:32 +00:00
"""
2019-09-04 02:58:31 +00:00
result = self.server.jellyfin.get_sync_queue(last_sync)
2018-09-06 08:36:32 +00:00
self.userdata(result['UserDataChanged'])
self.removed(result['ItemsRemoved'])
2019-07-09 20:05:28 +00:00
2018-09-06 08:36:32 +00:00
filters.extend(["tvshows", "boxsets", "musicvideos", "music"])
# Get only movies.
2019-09-04 02:58:31 +00:00
result = self.server.jellyfin.get_sync_queue(last_sync, ",".join(filters))
2018-09-06 08:36:32 +00:00
self.updated(result['ItemsAdded'])
self.updated(result['ItemsUpdated'])
self.userdata(result['UserDataChanged'])
self.removed(result['ItemsRemoved'])
"""
except Exception as error:
LOG.exception(error)
return False
return True
def save_last_sync(self):
2019-07-09 20:05:28 +00:00
try:
time_now = datetime.strptime(self.server.config.data['server-time'].split(', ', 1)[1], '%d %b %Y %H:%M:%S GMT') - timedelta(minutes=2)
except Exception as error:
2019-07-09 20:05:28 +00:00
LOG.exception(error)
time_now = datetime.utcnow() - timedelta(minutes=2)
2018-09-06 08:36:32 +00:00
last_sync = time_now.strftime('%Y-%m-%dT%H:%M:%Sz')
settings('LastIncrementalSync', value=last_sync)
LOG.info("--[ sync/%s ]", last_sync)
def select_libraries(self, mode=None):
''' Select from libraries synced. Either update or repair libraries.
Send event back to service.py
'''
modes = {
'SyncLibrarySelection': 'SyncLibrary',
'RepairLibrarySelection': 'RepairLibrary',
'AddLibrarySelection': 'SyncLibrary',
'RemoveLibrarySelection': 'RemoveLibrary'
}
2018-09-06 08:36:32 +00:00
sync = get_sync()
whitelist = [x.replace('Mixed:', "") for x in sync['Whitelist']]
2018-09-06 08:36:32 +00:00
libraries = []
with Database('jellyfin') as jellyfindb:
db = jellyfin_db.JellyfinDatabase(jellyfindb.cursor)
2018-09-06 08:36:32 +00:00
if mode in ('SyncLibrarySelection', 'RepairLibrarySelection', 'RemoveLibrarySelection'):
for library in sync['Whitelist']:
name = db.get_view_name(library.replace('Mixed:', ""))
libraries.append({'Id': library, 'Name': name})
else:
available = [x for x in sync['SortedViews'] if x not in whitelist]
for library in available:
2019-10-03 02:14:54 +00:00
name, media = db.get_view(library)
2018-09-06 08:36:32 +00:00
if media in ('movies', 'tvshows', 'musicvideos', 'mixed', 'music'):
libraries.append({'Id': library, 'Name': name})
2018-09-06 08:36:32 +00:00
choices = [x['Name'] for x in libraries]
choices.insert(0, translate(33121))
titles = {
"RepairLibrarySelection": 33199,
"SyncLibrarySelection": 33198,
"RemoveLibrarySelection": 33200,
"AddLibrarySelection": 33120
}
title = titles.get(mode, "Failed to get title {}".format(mode))
selection = dialog("multi", translate(title), choices)
2018-09-06 08:36:32 +00:00
if selection is None:
return
if 0 in selection:
selection = list(range(1, len(libraries) + 1))
selected_libraries = []
for x in selection:
library = libraries[x - 1]
selected_libraries.append(library['Id'])
event(modes[mode], {'Id': ','.join([libraries[x - 1]['Id'] for x in selection]), 'Update': mode == 'SyncLibrarySelection'})
2018-09-06 08:36:32 +00:00
def add_library(self, library_id, update=False):
2018-09-06 08:36:32 +00:00
try:
2019-01-25 15:30:30 +00:00
with FullSync(self, server=self.server) as sync:
sync.libraries(library_id, update)
2018-09-06 08:36:32 +00:00
except Exception as error:
LOG.exception(error)
return False
Views().get_nodes()
return True
2019-01-25 15:30:30 +00:00
def remove_library(self, library_id):
2019-01-25 11:32:29 +00:00
2018-09-06 08:36:32 +00:00
try:
2019-01-25 15:30:30 +00:00
with FullSync(self, self.server) as sync:
sync.remove_library(library_id)
2018-09-06 08:36:32 +00:00
Views().remove_library(library_id)
except Exception as error:
LOG.exception(error)
return False
Views().get_views()
2018-09-06 08:36:32 +00:00
Views().get_nodes()
return True
def userdata(self, data):
''' Add item_id to userdata queue.
'''
if not data:
return
2018-10-01 00:35:31 +00:00
items = [x['ItemId'] for x in data]
2018-09-06 08:36:32 +00:00
2018-10-01 00:35:31 +00:00
for item in split_list(items, LIMIT):
self.userdata_queue.put(item)
2018-09-06 08:36:32 +00:00
2018-10-04 03:36:57 +00:00
self.total_updates += len(items)
2018-10-01 00:35:31 +00:00
LOG.info("---[ userdata:%s ]", len(items))
2018-09-06 08:36:32 +00:00
def updated(self, data):
''' Add item_id to updated queue.
'''
if not data:
return
2018-10-01 00:35:31 +00:00
for item in split_list(data, LIMIT):
2018-09-06 08:36:32 +00:00
self.updated_queue.put(item)
2018-10-04 03:36:57 +00:00
self.total_updates += len(data)
2018-10-01 00:35:31 +00:00
LOG.info("---[ updated:%s ]", len(data))
2018-09-06 08:36:32 +00:00
def removed(self, data):
''' Add item_id to removed queue.
'''
if not data:
return
for item in data:
if item in list(self.removed_queue.queue):
continue
self.removed_queue.put(item)
2018-10-04 03:36:57 +00:00
self.total_updates += len(data)
2018-10-01 00:35:31 +00:00
LOG.info("---[ removed:%s ]", len(data))
2018-09-06 08:36:32 +00:00
class UpdateWorker(threading.Thread):
2018-09-06 08:36:32 +00:00
is_done = False
def __init__(self, queue, notify, lock, database, server=None, direct_path=None, *args):
2018-09-06 08:36:32 +00:00
self.queue = queue
2018-10-04 05:41:31 +00:00
self.notify_output = notify
self.notify = settings('newContent.bool')
2018-09-06 08:36:32 +00:00
self.lock = lock
self.database = Database(database)
self.args = args
self.server = server
self.direct_path = direct_path
2018-09-06 08:36:32 +00:00
threading.Thread.__init__(self)
def run(self):
with self.lock, self.database as kodidb, Database('jellyfin') as jellyfindb:
while True:
2018-09-06 08:36:32 +00:00
try:
item = self.queue.get(timeout=1)
except Queue.Empty:
break
2018-09-06 08:36:32 +00:00
default_args = (self.server, jellyfindb, kodidb, self.direct_path)
if item['Type'] == 'Movie':
obj = Movies(*default_args).movie
2020-03-19 22:04:23 +00:00
elif item['Type'] == 'BoxSet':
obj = Movies(*default_args).boxset
elif item['Type'] == 'Series':
obj = TVShows(*default_args).tvshow
elif item['Type'] == 'Season':
obj = TVShows(*default_args).season
elif item['Type'] == 'Episode':
obj = TVShows(*default_args).episode
elif item['Type'] == 'MusicVideo':
obj = MusicVideos(*default_args).musicvideo
elif item['Type'] == 'MusicAlbum':
obj = Music(*default_args).album
elif item['Type'] == 'MusicArtist':
obj = Music(*default_args).artist
elif item['Type'] == 'AlbumArtist':
obj = Music(s*default_args).albumartist
elif item['Type'] == 'Audio':
obj = Music(*default_args).song
try:
if obj(item) and self.notify:
self.notify_output.put((item['Type'], api.API(item).get_naming()))
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
2018-09-06 08:36:32 +00:00
self.queue.task_done()
if window('jellyfin_should_stop.bool'):
break
2018-09-06 08:36:32 +00:00
LOG.info("--<[ q:updated/%s ]", id(self))
self.is_done = True
2019-10-03 02:14:54 +00:00
2018-09-06 08:36:32 +00:00
class UserDataWorker(threading.Thread):
is_done = False
def __init__(self, queue, lock, database, *args):
self.queue = queue
self.lock = lock
self.database = Database(database)
self.args = args
threading.Thread.__init__(self)
def run(self):
with self.lock, self.database as kodidb, Database('jellyfin') as jellyfindb:
while True:
2018-09-06 08:36:32 +00:00
try:
item = self.queue.get(timeout=1)
except Queue.Empty:
break
2018-09-06 08:36:32 +00:00
try:
2020-02-25 23:39:50 +00:00
if item['Type'] == 'Movie':
obj = Movies(self.args[0], jellyfindb, kodidb, self.args[1]).userdata(item)
elif item['Type'] in ['Series', 'Season', 'Episode']:
obj = TVShows(self.args[0], jellyfindb, kodidb, self.args[1]).userdata(item)
elif item['Type'] == 'MusicAlbum':
obj = Music(self.args[0], jellyfindb, kodidb, self.args[1]).album(item)
elif item['Type'] == 'MusicArtist':
obj = Music(self.args[0], jellyfindb, kodidb, self.args[1]).artist(item)
elif item['Type'] == 'AlbumArtist':
obj = Music(self.args[0], jellyfindb, kodidb, self.args[1]).albumartist(item)
elif item['Type'] == 'Audio':
obj = Music(self.args[0], jellyfindb, kodidb, self.args[1]).song(item)
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
2018-09-06 08:36:32 +00:00
self.queue.task_done()
if window('jellyfin_should_stop.bool'):
break
2018-09-06 08:36:32 +00:00
LOG.info("--<[ q:userdata/%s ]", id(self))
self.is_done = True
2019-10-03 02:14:54 +00:00
2018-09-06 08:36:32 +00:00
class SortWorker(threading.Thread):
is_done = False
def __init__(self, queue, output, *args):
self.queue = queue
self.output = output
self.args = args
threading.Thread.__init__(self)
def run(self):
with Database('jellyfin') as jellyfindb:
database = jellyfin_db.JellyfinDatabase(jellyfindb.cursor)
2018-09-06 08:36:32 +00:00
while True:
try:
item_id = self.queue.get(timeout=1)
except Queue.Empty:
break
2018-09-06 08:36:32 +00:00
2018-09-13 00:43:51 +00:00
try:
media = database.get_media_by_id(item_id)
2018-09-06 08:36:32 +00:00
self.output[media].put({'Id': item_id, 'Type': media})
2019-07-09 20:05:28 +00:00
except Exception as error:
LOG.exception(error)
2018-09-13 00:43:51 +00:00
items = database.get_media_by_parent_id(item_id)
if not items:
LOG.info("Could not find media %s in the jellyfin database.", item_id)
2018-09-13 00:43:51 +00:00
else:
for item in items:
self.output[item[1]].put({'Id': item[0], 'Type': item[1]})
2018-09-06 08:36:32 +00:00
self.queue.task_done()
if window('jellyfin_should_stop.bool'):
2018-09-06 08:36:32 +00:00
break
LOG.info("--<[ q:sort/%s ]", id(self))
self.is_done = True
2019-10-03 02:14:54 +00:00
2018-09-06 08:36:32 +00:00
class RemovedWorker(threading.Thread):
is_done = False
def __init__(self, queue, lock, database, *args):
self.queue = queue
self.lock = lock
self.database = Database(database)
self.args = args
threading.Thread.__init__(self)
def run(self):
with self.lock, self.database as kodidb, Database('jellyfin') as jellyfindb:
while True:
2018-09-06 08:36:32 +00:00
try:
item = self.queue.get(timeout=1)
except Queue.Empty:
break
2018-09-06 08:36:32 +00:00
if item['Type'] == 'Movie':
obj = Movies(self.args[0], jellyfindb, kodidb, self.args[1]).remove
elif item['Type'] in ['Series', 'Season', 'Episode']:
obj = TVShows(self.args[0], jellyfindb, kodidb, self.args[1]).remove
elif item['Type'] in ['MusicAlbum', 'MusicArtist', 'AlbumArtist', 'Audio']:
obj = Music(self.args[0], jellyfindb, kodidb, self.args[1]).remove
elif item['Type'] == 'MusicVideo':
obj = MusicVideos(self.args[0], jellyfindb, kodidb, self.args[1]).remove
2018-09-06 08:36:32 +00:00
try:
obj(item['Id'])
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
2018-09-06 08:36:32 +00:00
self.queue.task_done()
if window('jellyfin_should_stop.bool'):
break
2018-09-06 08:36:32 +00:00
LOG.info("--<[ q:removed/%s ]", id(self))
self.is_done = True
2019-10-03 02:14:54 +00:00
2018-09-06 08:36:32 +00:00
class NotifyWorker(threading.Thread):
is_done = False
2018-10-04 05:41:31 +00:00
def __init__(self, queue, player):
2018-09-06 08:36:32 +00:00
self.queue = queue
2018-10-04 05:41:31 +00:00
self.video_time = int(settings('newvideotime')) * 1000
self.music_time = int(settings('newmusictime')) * 1000
self.player = player
threading.Thread.__init__(self)
2018-09-06 08:36:32 +00:00
def run(self):
while True:
try:
item = self.queue.get(timeout=3)
except Queue.Empty:
break
2018-10-04 05:41:31 +00:00
time = self.music_time if item[0] == 'Audio' else self.video_time
if time and (not self.player.isPlayingVideo() or xbmc.getCondVisibility('VideoPlayer.Content(livetv)')):
dialog("notification", heading="%s %s" % (translate(33049), item[0]), message=item[1],
icon="{jellyfin}", time=time, sound=False)
2018-10-04 05:41:31 +00:00
2018-09-06 08:36:32 +00:00
self.queue.task_done()
if window('jellyfin_should_stop.bool'):
2018-09-06 08:36:32 +00:00
break
LOG.info("--<[ q:notify/%s ]", id(self))
self.is_done = True