Make it possible to exit the app during inventory loading

This commit is contained in:
DevilXD committed 2022-08-02 21:19:54 +02:00
1 parent 8e28ee5959
commit 39d45360e6
2 files changed
+17 -9

No files matched your search

+1 -3
View File
@@ -29,7 +29,7 @@ except ModuleNotFoundError as exc:
from twitch import Twitch from twitch import Twitch
from settings import Settings from settings import Settings
from version import __version__ from version import __version__
from exceptions import CaptchaRequired, ExitRequest from exceptions import CaptchaRequired
from constants import SELF_PATH, FORMATTER, LOG_PATH, WINDOW_TITLE 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()) signal.signal(signal.SIGTERM, lambda *_: client.close())
try: try:
loop.run_until_complete(client.run()) loop.run_until_complete(client.run())
except ExitRequest:
pass
except CaptchaRequired: except CaptchaRequired:
exit_status = 1 exit_status = 1
msg = "Your login attempt was denied by CAPTCHA.\nPlease try again in +12 hours." msg = "Your login attempt was denied by CAPTCHA.\nPlease try again in +12 hours."
+16 -6
View File
@@ -171,7 +171,7 @@ class Twitch:
return 0 return 0
return self.games[game] return self.games[game]
async def run(self): async def _run(self):
""" """
Main method that runs the whole client. Main method that runs the whole client.
@@ -181,11 +181,7 @@ class Twitch:
• Changing the stream that's being watched if necessary • Changing the stream that's being watched if necessary
""" """
self.gui.start() self.gui.start()
try: await self.check_login()
await self.check_login()
except ExitRequest:
# we've been requested to exit during login, most likely
return
await self.websocket.start() await self.websocket.start()
# NOTE: watch task is explicitly restarted on each new run # NOTE: watch task is explicitly restarted on each new run
if self._watching_task is not None: if self._watching_task is not None:
@@ -403,6 +399,12 @@ class Twitch:
# we've been requested to exit the application # we've been requested to exit the application
break break
await self._state_change.wait() await self._state_change.wait()
async def run(self):
try:
await self._run()
except ExitRequest:
pass
# post-main-loop code goes here # post-main-loop code goes here
async def _watch_sleep(self, delay: float) -> None: async def _watch_sleep(self, delay: float) -> None:
@@ -921,6 +923,8 @@ class Twitch:
status_update("Fetching inventory...") status_update("Fetching inventory...")
# fetch in-progress campaigns (inventory) # fetch in-progress campaigns (inventory)
response = await self.gql_request(GQL_OPERATIONS["Inventory"]) response = await self.gql_request(GQL_OPERATIONS["Inventory"])
if self.gui.close_requested:
raise ExitRequest()
inventory: JsonType = response["data"]["currentUser"]["inventory"] inventory: JsonType = response["data"]["currentUser"]["inventory"]
ongoing_campaigns: list[JsonType] = inventory["dropCampaignsInProgress"] or [] ongoing_campaigns: list[JsonType] = inventory["dropCampaignsInProgress"] or []
# this contains claimed benefit edge IDs, not drop IDs # this contains claimed benefit edge IDs, not drop IDs
@@ -933,6 +937,8 @@ class Twitch:
] ]
# fetch all available campaigns data # fetch all available campaigns data
response = await self.gql_request(GQL_OPERATIONS["Campaigns"]) response = await self.gql_request(GQL_OPERATIONS["Campaigns"])
if self.gui.close_requested:
raise ExitRequest()
available_list: list[JsonType] = response["data"]["currentUser"]["dropCampaigns"] or [] available_list: list[JsonType] = response["data"]["currentUser"]["dropCampaigns"] or []
applicable_statuses = ("ACTIVE", "UPCOMING") applicable_statuses = ("ACTIVE", "UPCOMING")
existing_campaigns: set[str] = set(c.id for c in campaigns) existing_campaigns: set[str] = set(c.id for c in campaigns)
@@ -958,6 +964,8 @@ class Twitch:
): ):
fetched_campaigns.append(await coro) fetched_campaigns.append(await coro)
status_update(f"Fetching campaigns... ({i}/{len(available_campaigns)})") status_update(f"Fetching campaigns... ({i}/{len(available_campaigns)})")
if self.gui.close_requested:
raise ExitRequest()
campaigns.extend(fetched_campaigns) campaigns.extend(fetched_campaigns)
campaigns.sort(key=lambda c: c.active, reverse=True) campaigns.sort(key=lambda c: c.active, reverse=True)
campaigns.sort(key=lambda c: c.upcoming and c.starts_at or c.ends_at) 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}) self._drops.update({drop.id: drop for drop in campaign.drops})
await self.gui.inv.add_campaign(campaign) await self.gui.inv.add_campaign(campaign)
self.inventory.append(campaign) self.inventory.append(campaign)
if self.gui.close_requested:
raise ExitRequest()
def get_active_drop(self, channel: Channel | None = None) -> TimedDrop | None: def get_active_drop(self, channel: Channel | None = None) -> TimedDrop | None:
if not self.games: if not self.games: