Skip to content

Commit f06160e

Browse files
committed
don't queue same path if already queued
1 parent e8863e4 commit f06160e

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

plugin.program.autowidget/resources/lib/common/cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def read_history(path, create_if_missing=True):
6565

6666
def push_cache_queue(path, widget_id=None):
6767
hash = path2hash(path)
68-
queue_path = os.path.join(_addon_data, "{}.queue".format(hash))
6968
history = read_history(path, create_if_missing=True) # Ensure its created
7069
changed = False
7170
if widget_id is not None and widget_id not in history["widgets"]:
@@ -81,7 +80,7 @@ def push_cache_queue(path, widget_id=None):
8180
command = {'jsonrpc': '2.0', 'method': 'JSONRPC.NotifyAll',
8281
'params': {'sender': "AutoWidget",
8382
'message': "queue",
84-
'data': {"hash": hash, "path": path, "widget_id": widget_id},
83+
'data': (hash, path, widget_id),
8584
},
8685
'id': 1,}
8786
def send():
@@ -458,7 +457,7 @@ def chance_playback_updates_widget(cache_data, plays, cutoff_time=60 * 60):
458457
return prob
459458

460459

461-
def save_playback_history(media_type, playback_percentage):
460+
def save_playback_history(media_type, playback_percentage, path):
462461
# Record in json when things got played to help predict which widgets will change after playback
463462
# if playback_percentage < 0.7:
464463
# return

plugin.program.autowidget/resources/lib/refresh.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self):
3434
utils.ensure_addon_data()
3535
self._update_properties()
3636
self._clean_widgets()
37-
self.queue = queue.Queue()
37+
self.queue = OrderedSetQueue()
3838
for _ in range(1 if self.low_end else 4):
3939
thread = threading.Thread(target=self._processQueue)
4040
thread.start()
@@ -84,7 +84,7 @@ def tick(self, step, max, abort_check=lambda: False):
8484

8585
def _update_widgets(self):
8686
if self.low_end:
87-
self.waitForAbort(120)
87+
self.waitForAbort(30)
8888

8989
startup = True
9090
while not self.abortRequested():
@@ -101,26 +101,26 @@ def _update_widgets(self):
101101
def onNotification(self, sender, method, data):
102102
if sender == "AutoWidget":
103103
data = json.loads(data)
104-
# TODO: ensure we don't queue same one twice
105-
utils.log("Added to queue: {} {}".format(data['hash'][:5], data['path']), "notice")
106-
self.queue.put(data)
104+
utils.log("Added to queue: {} {} {}".format(*data), "notice")
105+
# special queue ensures we don't queue same one twice at the same time
106+
self.queue.put(tuple(data))
107107
return True
108108

109109

110110
def _processQueue(self):
111111
if self.low_end:
112-
self.waitForAbort(170) # TODO: wait until no more added to queue?
112+
self.waitForAbort(70) # TODO: wait until no more added to queue?
113113
utils.log("Starting processing queue", "notice")
114114

115115
while not self.abortRequested():
116116
if self.player.isPlayingVideo():
117117
xbmc.sleep(1000)
118118
continue
119119
try:
120-
res = self.queue.get(timeout=5)
120+
hash, path, widget_id = self.queue.get(timeout=5)
121121
except queue.Empty:
122+
# TODO: first run of queue. first refresh now?
122123
continue
123-
path, hash, widget_id = res['path'], res['hash'], res['widget_id']
124124
history_path = os.path.join(_addon_data, "{}.history".format(hash))
125125
cache_data = utils.read_json(history_path) if xbmcvfs.exists(history_path) else None
126126
# class Progress(object):
@@ -433,6 +433,7 @@ def __init__(self):
433433
self.totalTime = -1
434434
self.playingTime = 0
435435
self.info = {}
436+
self.path = None
436437

437438
def playing_type(self):
438439
"""
@@ -488,6 +489,7 @@ def onPlayBackStarted(self):
488489
self.totalTime = -1
489490
# self.recordPlay()
490491
self.type = self.playing_type()
492+
self.path = self.getPlayingFile()
491493

492494
def update_playback_time(self=self):
493495
while self.isPlaying():
@@ -515,9 +517,11 @@ def onPlayBackEnded(self):
515517
self.totalTime = -1.0
516518
self.playingTime = 0.0
517519
self.info = {}
518-
cache.save_playback_history(self.type, pp)
520+
cache.save_playback_history(self.type, pp, self.path)
519521
utils.log("recorded playback of {}% {}".format(pp, self.type), "notice")
520522

523+
# TODO: find which cache file has self.path in it. This probably would have changed
524+
521525
# wait for a bit so scrobing can happen
522526
# time.sleep(5)
523527
for hash, path in cache.widgets_changed_by_watching(self.type):
@@ -547,3 +551,13 @@ def onPlayBackSpeedChanged(self, speed):
547551

548552
def onQueueNextItem(self):
549553
pass
554+
555+
class OrderedSetQueue(queue.Queue):
556+
def _init(self, maxsize):
557+
self.queue = {} # Python 3 dict is ordered
558+
def _put(self, item):
559+
self.queue[item] = None
560+
def _get(self):
561+
val = next(iter(self.queue.keys()))
562+
del self.queue[val]
563+
return val

0 commit comments

Comments
 (0)