Compare commits

..

1 Commits

Author SHA1 Message Date
dmunozv04
d2a03717be Send a no-op command after the watchdog is triggered 2025-03-07 00:23:05 +01:00

View File

@@ -31,6 +31,7 @@ class YtLoungeApi(pyytlounge.YtLoungeApi):
self.logger = logger
self.shorts_disconnected = False
self.auto_play = True
self.noop_attempted = False # Track if we've already tried noop
if config:
self.mute_ads = config.mute_ads
self.skip_ads = config.skip_ads
@@ -42,8 +43,27 @@ class YtLoungeApi(pyytlounge.YtLoungeApi):
await asyncio.sleep(
35
) # YouTube sends at least a message every 30 seconds (no-op or any other)
if not self.noop_attempted:
# First time the watchdog is triggered, try sending a noop to keep connection alive
# YouTube responds with a LoungeStatus event after a noop if it is still connected
self.noop_attempted = True
self.logger.info("Watchdog triggered - sending noop command")
try:
await self.noop()
self.subscribe_task_watchdog = asyncio.create_task(self._watchdog())
except Exception as e:
self.logger.error(f"Error sending noop command: {e}")
self._cancel_subscription()
else:
# If we already tried noop and the watchdog is triggered again, cancel subscription
self.logger.warning("Watchdog triggered again after noop attempt, cancelling subscription")
self._cancel_subscription()
def _cancel_subscription(self):
try:
self.subscribe_task.cancel()
self.noop_attempted = False
except BaseException:
pass
@@ -54,25 +74,27 @@ class YtLoungeApi(pyytlounge.YtLoungeApi):
self.subscribe_task_watchdog.cancel()
except BaseException:
pass # No watchdog task
self.noop_attempted = False
self.subscribe_task = asyncio.create_task(super().subscribe(callback))
self.subscribe_task_watchdog = asyncio.create_task(self._watchdog())
create_task(self.debug_command("bugchomp "))
return self.subscribe_task
# Process a lounge subscription event
def _process_event(self, event_type: str, args: List[Any]):
self.logger.debug(f"process_event({event_type}, {args})")
# (Re)start the watchdog
# (Re)start the watchdog and reset noop attempt flag
try:
self.subscribe_task_watchdog.cancel()
except BaseException:
pass
finally:
self.noop_attempted = False
self.subscribe_task_watchdog = asyncio.create_task(self._watchdog())
# A bunch of events useful to detect ads playing, and the next video before it starts playing (that way we
# can get the segments)
if event_type == "onStateChange":
create_task(self.debug_command("exp 0 "))
data = args[0]
# print(data)
# Unmute when the video starts playing
@@ -192,9 +214,6 @@ class YtLoungeApi(pyytlounge.YtLoungeApi):
# the _command_offset (TODO: move to upstream if it works)
async def _command(self, command: str, command_parameters: dict = None) -> bool:
async with self._command_mutex:
self.logger.debug(
f"Send command: {command}, Parameters: {command_parameters}"
)
return await super()._command(command, command_parameters)
async def change_web_session(self, web_session: ClientSession):
@@ -203,9 +222,7 @@ class YtLoungeApi(pyytlounge.YtLoungeApi):
if self.conn is not None:
await self.conn.close()
self.session = web_session
async def debug_command(self, debug_command: str):
await super()._command(
"sendDebugCommand",
{"debugCommand": debug_command},
)
async def noop(self):
# No-op command to keep the connection alive
await super()._command("noop")