From 39d45360e638885776e77549794f94b6502f5762 Mon Sep 17 00:00:00 2001 From: DevilXD Date: Tue, 2 Aug 2022 21:19:54 +0200 Subject: [PATCH] Make it possible to exit the app during inventory loading --- main.py | 4 +--- twitch.py | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 2c1ae38..009d0c5 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ except ModuleNotFoundError as exc: from twitch import Twitch from settings import Settings from version import __version__ -from exceptions import CaptchaRequired, ExitRequest +from exceptions import CaptchaRequired from constants import SELF_PATH, FORMATTER, LOG_PATH, WINDOW_TITLE @@ -156,8 +156,6 @@ signal.signal(signal.SIGINT, lambda *_: client.close()) signal.signal(signal.SIGTERM, lambda *_: client.close()) try: loop.run_until_complete(client.run()) -except ExitRequest: - pass except CaptchaRequired: exit_status = 1 msg = "Your login attempt was denied by CAPTCHA.\nPlease try again in +12 hours." diff --git a/twitch.py b/twitch.py index 08ff3bd..4740ae2 100644 --- a/twitch.py +++ b/twitch.py @@ -171,7 +171,7 @@ class Twitch: return 0 return self.games[game] - async def run(self): + async def _run(self): """ Main method that runs the whole client. @@ -181,11 +181,7 @@ class Twitch: • Changing the stream that's being watched if necessary """ self.gui.start() - try: - await self.check_login() - except ExitRequest: - # we've been requested to exit during login, most likely - return + await self.check_login() await self.websocket.start() # NOTE: watch task is explicitly restarted on each new run if self._watching_task is not None: @@ -403,6 +399,12 @@ class Twitch: # we've been requested to exit the application break await self._state_change.wait() + + async def run(self): + try: + await self._run() + except ExitRequest: + pass # post-main-loop code goes here async def _watch_sleep(self, delay: float) -> None: @@ -921,6 +923,8 @@ class Twitch: status_update("Fetching inventory...") # fetch in-progress campaigns (inventory) response = await self.gql_request(GQL_OPERATIONS["Inventory"]) + if self.gui.close_requested: + raise ExitRequest() inventory: JsonType = response["data"]["currentUser"]["inventory"] ongoing_campaigns: list[JsonType] = inventory["dropCampaignsInProgress"] or [] # this contains claimed benefit edge IDs, not drop IDs @@ -933,6 +937,8 @@ class Twitch: ] # fetch all available campaigns data response = await self.gql_request(GQL_OPERATIONS["Campaigns"]) + if self.gui.close_requested: + raise ExitRequest() available_list: list[JsonType] = response["data"]["currentUser"]["dropCampaigns"] or [] applicable_statuses = ("ACTIVE", "UPCOMING") existing_campaigns: set[str] = set(c.id for c in campaigns) @@ -958,6 +964,8 @@ class Twitch: ): fetched_campaigns.append(await coro) status_update(f"Fetching campaigns... ({i}/{len(available_campaigns)})") + if self.gui.close_requested: + raise ExitRequest() campaigns.extend(fetched_campaigns) campaigns.sort(key=lambda c: c.active, reverse=True) campaigns.sort(key=lambda c: c.upcoming and c.starts_at or c.ends_at) @@ -970,6 +978,8 @@ class Twitch: self._drops.update({drop.id: drop for drop in campaign.drops}) await self.gui.inv.add_campaign(campaign) self.inventory.append(campaign) + if self.gui.close_requested: + raise ExitRequest() def get_active_drop(self, channel: Channel | None = None) -> TimedDrop | None: if not self.games: