-
Notifications
You must be signed in to change notification settings - Fork 2
/
_common.py
161 lines (107 loc) · 4.51 KB
/
_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
##################################################################################################
import logging
import sqlite3
##################################################################################################
log = logging.getLogger("LD." + __name__)
##################################################################################################
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 sqlite3.Error as error:
raise
except errors as error:
if not (hasattr(error, 'quiet') and error.quiet):
errStrings = ga.formatException()
ga.sendEventData("Exception", errStrings[0], errStrings[1], True)
log.exception(error)
log.error("function: %s \n args: %s \n kwargs: %s",
func.__name__, args, kwargs)
return default_value
return wrapper
return decorator
class Items(object):
pdialog = None
title = None
count = 0
total = 0
def __init__(self):
self.artwork = artwork.Artwork()
self.emby = embyserver.Read_EmbyServer()
self.do_url = downloadutils.DownloadUtils().downloadUrl
self.kodi_version = int(xbmc.getInfoLabel('System.BuildVersion')[:2])
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))
process = self._get_func(item_type, "added")
if view:
process(items, total, view)
else:
process(items, total)
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 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)
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:
self.title = item.get('Name', "unknown")
yield item
self.update_pdialog()
if update:
self.count += 1
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
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:
self.process_all(item_type, "update", emby_items, total, view)
# Process deletes
if compare_to:
self.remove_all(item_type, compare_to.keys())
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():
# 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