From 18a0d713ec833ac8b0ddc20b133d5a849c677628 Mon Sep 17 00:00:00 2001 From: Dean Gardiner Date: Mon, 19 Sep 2016 09:57:15 +1200 Subject: [PATCH] Fixed an issue where records could be duplicated if watched history changes while the scanner is running --- tests/tasks/history/duplicates/scan_tests.py | 8 ++------ trakt_tools/tasks/history/duplicates/scan/main.py | 4 ++++ trakt_tools/tasks/history/duplicates/scan/models.py | 9 ++++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/tasks/history/duplicates/scan_tests.py b/tests/tasks/history/duplicates/scan_tests.py index d69e1e9..f1377aa 100644 --- a/tests/tasks/history/duplicates/scan_tests.py +++ b/tests/tasks/history/duplicates/scan_tests.py @@ -110,9 +110,7 @@ def test_duplicate_single_episode(): ] # Validate records - assert [ - r.id for r in episode.records - ] == [ + assert sorted(episode.records.keys()) == [ 1, 2 ] @@ -185,9 +183,7 @@ def test_duplicate_single_movie(): ] # Validate records - assert [ - r.id for r in movie.records - ] == [ + assert sorted(movie.records.keys()) == [ 1, 2 ] diff --git a/trakt_tools/tasks/history/duplicates/scan/main.py b/trakt_tools/tasks/history/duplicates/scan/main.py index 43b8c5a..946514e 100644 --- a/trakt_tools/tasks/history/duplicates/scan/main.py +++ b/trakt_tools/tasks/history/duplicates/scan/main.py @@ -152,6 +152,10 @@ def map_item(self, store, current, create_shows=True): # Try add record to existing group record = Record.from_item(current) + if store[key].has_record(record): + log.debug('Ignoring duplicate record: %r', record) + return True + if store[key].add(record, self.delta_max): return True diff --git a/trakt_tools/tasks/history/duplicates/scan/models.py b/trakt_tools/tasks/history/duplicates/scan/models.py index ba52cc9..fb86178 100644 --- a/trakt_tools/tasks/history/duplicates/scan/models.py +++ b/trakt_tools/tasks/history/duplicates/scan/models.py @@ -30,7 +30,7 @@ def __init__(self, key, title, year, season=None, number=None): self.children = {} self.groups = {} - self.records = [] + self.records = {} @property def duplicated(self): @@ -52,7 +52,7 @@ def add(self, current, delta_max): records.append(current) # Add record to list - self.records.append(current) + self.records[current.id] = current return True return False @@ -65,7 +65,10 @@ def create_group(self, record): self.groups[record.watched_at] = [record] # Add record to list - self.records.append(record) + self.records[record.id] = record + + def has_record(self, record): + return record.id in self.records def __repr__(self): fragments = []