Skip to content

Commit

Permalink
Prevent activity from being processed twice
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiRigal committed Apr 30, 2022
1 parent 9187cda commit d6e9b60
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ def __init__(self, user_config_path: str):
# Plex
self.plex = PlexServer(self.config.get("plex.url"), self.config.get("plex.token"))
self.plex_user_id = self.get_plex_user_id()
self.session_states = dict() # session_key: session_state
self.default_streams = dict() # item_key: (audio_stream_id, substitle_stream_id)
self.user_clients = dict() # client_identifier: user_id
self.newly_added = dict() # episode_id: added_at
self.session_states = {} # session_key: session_state
self.default_streams = {} # item_key: (audio_stream_id, substitle_stream_id)
self.user_clients = {} # client_identifier: user_id
self.newly_added = {} # episode_id: added_at
self.recent_activities = {} # (user_id, item_id): timestamp
# Scheduler
self.scheduler = None
if self.config.get("scheduler.enable"):
Expand Down Expand Up @@ -172,11 +173,19 @@ def process_activity(self, activity: dict):
if item is None or not isinstance(item, Episode):
return

# Skip if this item has already been seen in the last 5 seconds
activity_key = (user_id, media_key)
if activity_key in self.recent_activities and \
self.recent_activities[activity_key] < datetime.now() + timedelta(seconds=5):
return
self.recent_activities[activity_key] = datetime.now()

# Change tracks if needed
item.reload()
user = PlexUtils.get_user_from_user_id(self.plex, user_id)
if user is None:
return
logger.debug(f"[Activity] User: {user.name} | Episode: {item}")
self.change_default_tracks_if_needed(user.name, item)

def process_timeline_message(self, message: dict):
Expand Down

0 comments on commit d6e9b60

Please sign in to comment.