Improve on the CALL logging, added useful comments

This commit is contained in:
DevilXD
2022-12-14 14:43:02 +01:00
parent f9b8d8e9a8
commit 07708e9ab1
2 changed files with 30 additions and 24 deletions

View File

@@ -272,15 +272,20 @@ class Channel:
"""
await asyncio.sleep(ONLINE_DELAY.total_seconds())
self._pending_stream_up = None # for 'display' to work properly
await self.update_stream(trigger_events=True)
await self.update_stream(trigger_events=True) # triggers 'display' via the event
def set_online(self):
def check_online(self):
"""
Sets the channel status to PENDING_ONLINE, where after ONLINE_DELAY,
it's going to be set to ONLINE.
Sets up a task that will wait ONLINE_DELAY duration,
and then check for the stream being ONLINE OR OFFLINE.
This is called externally, if we receive an event about this happening.
This is also called when an already online channel needs a delayed status update.
If the channel is OFFLINE, it sets the channel's status to PENDING_ONLINE,
where after ONLINE_DELAY, it's going to be set to ONLINE.
If the channel is ONLINE already, after ONLINE_DELAY,
it's status is going to be double-checked to ensure it's actually ONLINE.
This is called externally, if we receive an event about the status possibly being ONLINE
or having to be updated.
"""
if self._pending_stream_up is None:
self._pending_stream_up = asyncio.create_task(self._online_delay())
@@ -290,7 +295,7 @@ class Channel:
"""
Sets the channel status to OFFLINE. Cancels PENDING_ONLINE if applicable.
This is called externally, if we receive an event about this happening.
This is called externally, if we receive an event indicating the channel is now OFFLINE.
"""
needs_display: bool = False
if self._pending_stream_up is not None:

View File

@@ -1099,7 +1099,7 @@ class Twitch:
if msg_type == "viewcount":
if not channel.online:
# if it's not online for some reason, set it so
channel.set_online()
channel.check_online()
else:
viewers = message["viewers"]
channel.viewers = viewers
@@ -1108,7 +1108,7 @@ class Twitch:
elif msg_type == "stream-down":
channel.set_offline()
elif msg_type == "stream-up":
channel.set_online()
channel.check_online()
elif msg_type == "commercial":
# skip these
pass
@@ -1132,22 +1132,16 @@ class Twitch:
if channel is None:
logger.error(f"Broadcast settings update for a non-existing channel: {channel_id}")
return
if message["old_status"] != message["status"]:
status_change = f"\n{message['old_status']} -> {message['status']}"
else:
status_change = ''
if message["old_game"] != message["game"]:
game_change = f"\n{message['old_game']} -> {message['game']}"
game_change = f", game changed: {message['old_game']} -> {message['game']}"
else:
game_change = ''
logger.log(
CALL, f"Channel update from websocket: {channel.name}{status_change}{game_change}"
)
logger.log(CALL, f"Channel update from websocket: {channel.name}{game_change}")
# There's no information about channel tags here, but this event is triggered
# when the tags change. We can use this to just update the stream data after the change.
# Use 'set_online' to introduce a delay, allowing for multiple title and tags
# changes before we update. This eventually calls 'on_channel_update' below.
channel.set_online()
channel.check_online()
def on_channel_update(
self, channel: Channel, stream_before: Stream | None, stream_after: Stream | None
@@ -1173,20 +1167,27 @@ class Twitch:
logger.info(f"{channel.name} goes ONLINE")
else:
# Channel was OFFLINE and stays that way
# Nothing to do here for now
return
logger.log(CALL, f"{channel.name} stays OFFLINE")
else:
watching_channel = self.watching_channel.get_with_default(None)
if (
watching_channel is not None
and watching_channel == channel
and not self.can_watch(channel)
and watching_channel == channel # the watching channel was the one updated
and not self.can_watch(channel) # we can't watch it anymore
):
# NOTE: In these cases, channel was the watching channel
if stream_after is None:
# Channel going OFFLINE
self.print(_("status", "goes_offline").format(channel=channel.name))
# if the channel stays online, we silently trigger a switch
else:
# Channel stays ONLINE, but we can't watch it anymore
logger.info(
f"{channel.name} status has been updated, switching... "
f"(🎁: {stream_before.drops_enabled and '' or ''} -> "
f"{stream_after.drops_enabled and '' or ''})"
)
self.change_state(State.CHANNEL_SWITCH)
# NOTE: In these cases, it wasn't the watching channel
elif stream_after is None:
logger.info(f"{channel.name} goes OFFLINE")
else:
@@ -1250,7 +1251,7 @@ class Twitch:
assert msg_type == "drop-progress"
if drop is not None:
drop_text = (
f"{drop.name}({drop.campaign.game}, "
f"{drop.name} ({drop.campaign.game}, "
f"{message['data']['current_progress_min']}/"
f"{message['data']['required_progress_min']})"
)