jellyfin-kodi/resources/lib/objects/_common.py

203 lines
5.8 KiB
Python
Raw Normal View History

2016-10-10 08:19:00 +00:00
# -*- coding: utf-8 -*-
##################################################################################################
import logging
import os
2016-10-10 08:19:00 +00:00
import xbmc
import xbmcvfs
import api
2016-10-10 08:19:00 +00:00
import artwork
import downloadutils
import read_embyserver as embyserver
from ga_client import GoogleAnalytics
2016-10-10 08:19:00 +00:00
from utils import window, settings, dialog, language as lang, should_stop
##################################################################################################
log = logging.getLogger("EMBY."+__name__)
ga = GoogleAnalytics()
2016-10-10 08:19:00 +00:00
##################################################################################################
2016-10-28 03:31:58 +00:00
def catch_except(errors=(Exception, ), default_value=False):
# Will wrap method with try/except and print parameters for easier debugging
def decorator(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except errors as error:
errStrings = ga.formatException()
ga.sendEventData("Exception", errStrings[0], errStrings[1])
2016-10-28 03:31:58 +00:00
log.exception(error)
log.error("function: %s \n args: %s \n kwargs: %s",
func.__name__, args, kwargs)
return default_value
return wrapper
return decorator
2016-10-10 08:19:00 +00:00
class Items(object):
pdialog = None
title = None
count = 0
total = 0
def __init__(self):
2016-10-10 08:19:00 +00:00
self.artwork = artwork.Artwork()
self.emby = embyserver.Read_EmbyServer()
self.do_url = downloadutils.DownloadUtils().downloadUrl
self.should_stop = should_stop
self.kodi_version = int(xbmc.getInfoLabel('System.BuildVersion')[:2])
self.direct_path = settings('useDirectPaths') == "1"
self.content_msg = settings('newContent') == "true"
2016-10-10 09:01:13 +00:00
@classmethod
def path_validation(cls, path):
2016-10-10 08:19:00 +00:00
# Verify if direct path is accessible or not
if not os.path.supports_unicode_filenames:
path = path.encode('utf-8')
2016-10-10 08:19:00 +00:00
if window('emby_pathverified') != "true" and not xbmcvfs.exists(path):
if dialog(type_="yesno",
heading="{emby}",
line1="%s %s. %s" % (lang(33047), path, lang(33048))):
window('emby_shouldStop', value="true")
return False
return True
2016-10-10 09:01:13 +00:00
def content_pop(self, name):
2016-10-10 08:19:00 +00:00
# It's possible for the time to be 0. It should be considered disabled in this case.
2016-10-10 09:01:13 +00:00
if not self.pdialog and self.content_msg and self.new_time:
2016-10-10 08:19:00 +00:00
dialog(type_="notification",
heading="{emby}",
message="%s %s" % (lang(33049), name),
icon="{emby}",
2016-10-10 09:01:13 +00:00
time=self.new_time,
2016-10-10 08:19:00 +00:00
sound=False)
def update_pdialog(self):
if self.pdialog:
percentage = int((float(self.count) / float(self.total))*100)
self.pdialog.update(percentage, message=self.title)
def add_all(self, item_type, items, view=None):
if self.should_stop():
return False
total = items['TotalRecordCount'] if 'TotalRecordCount' in items else len(items)
items = items['Items'] if 'Items' in items else items
if self.pdialog and view:
self.pdialog.update(heading="Processing %s / %s items" % (view['name'], total))
2016-10-10 11:14:10 +00:00
process = self._get_func(item_type, "added")
2016-10-10 08:19:00 +00:00
if view:
2016-10-10 11:14:10 +00:00
process(items, total, view)
2016-10-10 08:19:00 +00:00
else:
2016-10-10 11:14:10 +00:00
process(items, total)
2016-10-10 08:19:00 +00:00
def process_all(self, item_type, action, items, total=None, view=None):
log.debug("Processing %s: %s", action, items)
process = self._get_func(item_type, action)
self.total = total or len(items)
self.count = 0
for item in items:
if self.should_stop():
return False
if not process:
continue
self.title = item.get('Name', "unknown")
self.update_pdialog()
process(item)
self.count += 1
def remove_all(self, item_type, items):
log.debug("Processing removal: %s", items)
process = self._get_func(item_type, "remove")
for item in items:
process(item)
2016-10-10 08:19:00 +00:00
def added(self, items, total=None, update=True):
# Generator for newly added content
if update:
self.total = total or len(items)
self.count = 0
for item in items:
if self.should_stop():
break
self.title = item.get('Name', "unknown")
yield item
self.update_pdialog()
if update:
self.count += 1
2016-10-18 08:17:08 +00:00
def compare(self, item_type, items, compare_to, view=None):
view_name = view['name'] if view else item_type
update_list = self._compare_checksum(items, compare_to)
log.info("Update for %s: %s", view_name, update_list)
if self.should_stop():
return False
2016-10-18 08:17:08 +00:00
emby_items = self.emby.getFullItems(update_list)
total = len(update_list)
if self.pdialog:
self.pdialog.update(heading="Processing %s / %s items" % (view_name, total))
# Process additions and updates
if emby_items:
2016-10-18 10:20:10 +00:00
self.process_all(item_type, "update", emby_items, total, view)
2016-10-18 08:17:08 +00:00
# Process deletes
if compare_to:
self.remove_all(item_type, compare_to.items())
return True
def _compare_checksum(self, items, compare_to):
update_list = list()
for item in items:
if self.should_stop():
break
item_id = item['Id']
if compare_to.get(item_id) != api.API(item).get_checksum():
2016-10-18 10:20:10 +00:00
# Only update if item is not in Kodi or checksum is different
update_list.append(item_id)
compare_to.pop(item_id, None)
return update_list