Restructure channel cleanup and fetch to reuse existing channels where possible

This commit is contained in:
DevilXD committed 2022-03-03 20:18:54 +01:00
1 parent a10c0bf389
commit 5286c39d41
1 file changed
+21 -15
+21 -15
View File
@@ -54,8 +54,7 @@ gql_logger = logging.getLogger("TwitchDrops.gql")
def viewers_key(channel: Channel) -> int: def viewers_key(channel: Channel) -> int:
viewers = channel.viewers if (viewers := channel.viewers) is not None:
if viewers is not None:
return viewers return viewers
return -1 return -1
@@ -210,17 +209,19 @@ class Twitch:
self.gui.games.set_games(games) self.gui.games.set_games(games)
self.change_state(State.GAME_SELECT) self.change_state(State.GAME_SELECT)
elif self._state is State.GAME_SELECT: elif self._state is State.GAME_SELECT:
self.game = self.gui.games.get_selection() new_game: Game | None = self.gui.games.get_selection()
if self.game is None: if new_game is None:
if first_select: if first_select:
# on first select, let the user make the choice # on first select, let the user make the choice
first_select = False first_select = False
else: else:
self.game = self.gui.games.set_first() new_game = self.gui.games.set_first()
if self.game is not None: if new_game is not None:
# restart the watch loop immediately on new game selected # restart the watch loop immediately on new game selected
self.restart_watching() self.restart_watching()
# signal channel cleanup that we're removing everything # signal channel cleanup that we're removing everything
if new_game != self.game:
self.game = new_game
full_cleanup = True full_cleanup = True
self.change_state(State.CHANNELS_CLEANUP) self.change_state(State.CHANNELS_CLEANUP)
elif self._state is State.CHANNELS_CLEANUP: elif self._state is State.CHANNELS_CLEANUP:
@@ -250,6 +251,7 @@ class Twitch:
for channel in to_remove: for channel in to_remove:
del channels[channel.id] del channels[channel.id]
channel.remove() channel.remove()
del to_remove
if self.game is not None: if self.game is not None:
self.change_state(State.CHANNELS_FETCH) self.change_state(State.CHANNELS_FETCH)
else: else:
@@ -262,25 +264,28 @@ class Twitch:
# pre-display the active drop without substracting a minute # pre-display the active drop without substracting a minute
if (active_drop := self.get_active_drop()) is not None: if (active_drop := self.get_active_drop()) is not None:
active_drop.display(countdown=False) active_drop.display(countdown=False)
# gather ACLs from campaigns # start with all current channels
new_channels: OrderedSet[Channel] = OrderedSet(self.channels.values())
# gather and add ACL channels from campaigns
# NOTE: we consider only campaigns that can be progressed # NOTE: we consider only campaigns that can be progressed
# NOTE: we use a separate set so that we can set them online separately
no_acl = False no_acl = False
new_channels: OrderedSet[Channel] = OrderedSet() acl_channels: OrderedSet[Channel] = OrderedSet()
for campaign in self.inventory[self.game]: for campaign in self.inventory[self.game]:
if any(drop.can_earn() for drop in campaign.drops): if any(drop.can_earn() for drop in campaign.drops):
acl = campaign.allowed_channels if campaign.allowed_channels:
if acl: acl_channels.update(campaign.allowed_channels)
new_channels.update(acl)
else: else:
no_acl = True no_acl = True
# set them online if possible # remove all ACL channels from the separate set that already exist
await asyncio.gather(*(channel.check_online() for channel in new_channels)) acl_channels.difference_update(new_channels)
# use the separate set to set them online if possible
if acl_channels:
await asyncio.gather(*(channel.check_online() for channel in acl_channels))
if no_acl: if no_acl:
# if there's at least one campaign without an ACL, # if there's at least one campaign without an ACL,
# add a list of live channels with drops enabled # add a list of live channels with drops enabled
new_channels.update(await self.get_live_streams()) new_channels.update(await self.get_live_streams())
# merge current channels into new ones
new_channels.update(self.channels.values())
# sort them descending by viewers, # sort them descending by viewers,
# then by priority so that prioritized ones are first # then by priority so that prioritized ones are first
# NOTE: We can drop OrderedSet now because there's no more channels being added # NOTE: We can drop OrderedSet now because there's no more channels being added
@@ -303,6 +308,7 @@ class Twitch:
WebsocketTopic.as_str("Channel", "StreamState", channel.id) WebsocketTopic.as_str("Channel", "StreamState", channel.id)
for channel in to_remove for channel in to_remove
) )
del to_remove
# set our new channel list # set our new channel list
channels.clear() channels.clear()
self.gui.channels.clear() self.gui.channels.clear()