Submit add_to_history data in bulk (requires pytrakt update)
+from __future__ import annotations
+
+from collections import defaultdict
+from typing import TYPE_CHECKING
+
import trakt.sync
+from trakt.utils import timestamp
from plextraktsync.decorators.rate_limit import rate_limit
from plextraktsync.decorators.retry import retry
from plextraktsync.decorators.time_limit import time_limit
from plextraktsync.factory import logging
-from plextraktsync.trakt.PartialTraktMedia import PartialTraktMedia
from plextraktsync.util.remove_empty_values import remove_empty_values
+if TYPE_CHECKING:
+ from typing import Dict, List
+
class TraktMarkWatchedWorker:
# Queue this Worker can handle
@@ -23,8 +31,8 @@ def __call__(self, queues):
queues[self.QUEUE].clear()
def submit(self, items):
- for (m, watched_at) in items:
- result = self.add_to_history(m, watched_at)
+ items = self.normalize(items)
+ result = self.add_to_history(items)
result = remove_empty_values(result.copy())
if result:
self.logger.debug(f"Submitted add_to_history: {result}")
@@ -32,5 +40,16 @@ def submit(self, items):
@rate_limit()
@time_limit()
@retry()
- def add_to_history(self, m: PartialTraktMedia, watched_at: str):
- return trakt.sync.add_to_history(m, watched_at)
+ def add_to_history(self, items: Dict):
+ return trakt.sync.add_to_history(items)
+
+ @staticmethod
+ def normalize(items: List):
+ result = defaultdict(list)
+ for (m, watched_at) in items:
+ result[m.media_type].append({
+ "ids": m.ids["ids"],
+ "watched_at": timestamp(watched_at),
+ })
+
+ return result
