collected items batch sent to trakt every 5 seconds
MODIFY
plextraktsync/cli.py
plextraktsync/cli.py
@@ -158,12 +158,12 @@ def plex_login():
help="Specify what to sync",
)
@click.option(
- "--batch-size",
- "batch_size",
+ "--batch-delay",
+ "batch_delay",
type=int,
- default=1,
+ default=5,
show_default=True,
- help="Batch size for collection submit queue",
+ help="Time in seconds between each collection batch submit to trakt",
)
@click.option(
"--dry-run",

MODIFY
plextraktsync/commands/sync.py
plextraktsync/commands/sync.py
@@ -16,7 +16,7 @@ def sync(
show: str,
movie: str,
ids: List[str],
- batch_size: int,
+ batch_delay: int,
dry_run: bool,
no_progress_bar: bool,
):
@@ -31,7 +31,7 @@ def sync(
shows = sync_option in ["all", "tv", "shows"]
config = factory.run_config().update(
- batch_size=batch_size, dry_run=dry_run, progressbar=not no_progress_bar
+ batch_delay=batch_delay, dry_run=dry_run, progressbar=not no_progress_bar
)
wc = factory.walk_config().update(movies=movies, shows=shows)
w = factory.walker()

MODIFY
plextraktsync/config.py
plextraktsync/config.py
@@ -27,7 +27,7 @@ class RunConfig:
"""
dry_run: bool = False
- batch_size: int = 1
+ batch_delay: int = 5
progressbar: bool = True
def update(self, **kwargs):

MODIFY
plextraktsync/factory.py
plextraktsync/factory.py
@@ -10,7 +10,7 @@ def trakt_api(self):
from plextraktsync.trakt_api import TraktApi
config = self.run_config()
- trakt = TraktApi(batch_size=config.batch_size)
+ trakt = TraktApi(batch_delay=config.batch_delay)
return trakt

MODIFY
plextraktsync/sync.py
plextraktsync/sync.py
@@ -68,6 +68,7 @@ def sync(self, walker: Walker, dry_run=False):
self.sync_ratings(movie, dry_run=dry_run)
self.sync_watched(movie, dry_run=dry_run)
listutil.addPlexItemToLists(movie)
+ walker.trakt.batch.flush(force=True)
shows = set()
for episode in walker.find_episodes():
@@ -78,6 +79,7 @@ def sync(self, walker: Walker, dry_run=False):
if self.config.sync_ratings:
# collect shows for later ratings sync
shows.add(episode.show)
+ walker.trakt.batch.flush(force=True)
for show in walker.walk_shows(shows, title="Syncing show ratings"):
self.sync_ratings(show, dry_run=dry_run)

MODIFY
plextraktsync/trakt_api.py
plextraktsync/trakt_api.py
from __future__ import annotations
from typing import Union
+from time import time
import trakt
import trakt.movies
@@ -92,8 +93,8 @@ class TraktApi:
Trakt API class abstracting common data access and dealing with requests cache.
"""
- def __init__(self, batch_size=None):
- self.batch_size = batch_size
+ def __init__(self, batch_delay=None):
+ self.batch_delay = batch_delay
trakt.core.CONFIG_PATH = pytrakt_file
trakt.core.session = factory.session()
@@ -105,7 +106,7 @@ def device_auth(client_id: str, client_secret: str):
@cached_property
def batch(self):
- return TraktBatch(self, batch_size=self.batch_size)
+ return TraktBatch(self, batch_delay=self.batch_delay)
@cached_property
@nocache
@@ -307,10 +308,11 @@ def flush(self):
class TraktBatch:
- def __init__(self, trakt: TraktApi, batch_size=None):
+ def __init__(self, trakt: TraktApi, batch_delay=None):
self.trakt = trakt
- self.batch_size = batch_size
+ self.batch_delay = batch_delay
self.collection = {}
+ self.last_sent_time = 0
@nocache
@rate_limit()
@@ -334,15 +336,16 @@ def queue_size(self):
return size
- def flush(self):
+ def flush(self, force=False):
"""
- Flush the queue if it's bigger than batch_size
+ Flush the queue every batch_delay seconds
"""
- if not self.batch_size:
+ if not self.batch_delay and force is False:
return
-
- if self.queue_size() >= self.batch_size:
+ elapsed = time() - self.last_sent_time
+ if elapsed >= self.batch_delay or force is True:
self.submit_collection()
+ self.last_sent_time = time()
def add_to_collection(self, media_type: str, item):
"""

MODIFY
tests/test_batch.py
tests/test_batch.py
@@ -27,7 +27,7 @@ def test_batch_size_none():
def test_batch_size_1():
response = load_mock("trakt_sync_collection_response.json")
- b = TraktBatch(trakt, batch_size=1)
+ b = TraktBatch(trakt, batch_delay=1)
b.trakt_sync_collection = Mock(return_value=response)
assert b.queue_size() == 0

collected items batch sent to trakt every 5 seconds