From 5308e7cf2e206bc1ab8d15d3da688c9dfab40b9a Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 29 Jan 2021 22:14:53 +0100 Subject: [PATCH] claim_drops_startup as request in issue comment https://github.com/Tkd-Alex/Twitch-Channel-Points-Miner-v2/issues/21\#issuecomment-770012631 Signed-off-by: Alessandro Maggio --- README.md | 1 + .../TwitchChannelPointsMiner.py | 5 ++++ TwitchChannelPointsMiner/classes/Twitch.py | 28 ++++++++++++++++--- .../classes/WebSocketsPool.py | 2 +- example.py | 1 + 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4651deb5..1d0033ee 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ twitch_miner = TwitchChannelPointsMiner( follow_raid=True, # Follow raid to obtain more points watch_streak=True, # If a streamer go online change the priotiry of streamers array and catch the watch screak. Issue #11 drops_events=True, # If you want to auto claim game drops from Twitch inventory Issue #21 + claim_drops_startup=False, # If you want to auto claim all drops from Twitch inventory on startup logger_settings=LoggerSettings( save=True, # If you want to save logs in file (suggested) console_level=logging.INFO, # Level of logs - use logging.DEBUG for more info) diff --git a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py index 3037f7e5..314ec4f3 100644 --- a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py +++ b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py @@ -40,6 +40,7 @@ def __init__( make_predictions: bool = True, follow_raid: bool = True, watch_streak: bool = False, + claim_drops_startup: bool = False, drops_events: bool = False, logger_settings: LoggerSettings = LoggerSettings(), browser_settings: BrowserSettings = BrowserSettings(), @@ -56,6 +57,7 @@ def __init__( self.follow_raid = follow_raid self.watch_streak = watch_streak self.drops_events = drops_events + self.claim_drops_startup = claim_drops_startup self.streamers = [] self.events_predictions = {} self.minute_watcher_thread = None @@ -90,6 +92,9 @@ def run(self, streamers: list = [], followers=False): self.twitch.login() + if self.claim_drops_startup is True: + self.twitch.claim_all_drops_from_inventory() + # Clear streamers array # Remove duplicate 3. Preserving Order: Use OrderedDict (askpython .com) streamers = [streamer_name.lower().strip() for streamer_name in streamers] diff --git a/TwitchChannelPointsMiner/classes/Twitch.py b/TwitchChannelPointsMiner/classes/Twitch.py index 5c680cc6..9ef52562 100644 --- a/TwitchChannelPointsMiner/classes/Twitch.py +++ b/TwitchChannelPointsMiner/classes/Twitch.py @@ -11,6 +11,7 @@ import logging import random import copy +import random from pathlib import Path @@ -20,6 +21,7 @@ StreamerDoesNotExistException, TimeBasedDropNotFound, ) +from TwitchChannelPointsMiner.utils import get_streamer_index from TwitchChannelPointsMiner.constants.twitch import CLIENT_ID, API, GQLOperations logger = logging.getLogger(__name__) @@ -143,22 +145,40 @@ def claim_bonus(self, streamer, claim_id, less_printing=False): } self.post_gql_request(json_data) - def claim_drop(self, streamer, drop_instance_id): - logger.info(f"Claiming the drop for {streamer}!", extra={"emoji": ":package:"}) + def claim_drop(self, drop_instance_id, streamer=None): + if streamer is not None: + logger.info( + f"Claiming the drop for {streamer}!", extra={"emoji": ":package:"} + ) + else: + logger.info( + f"Startup claim drop {drop_instance_id}", extra={"emoji": ":package:"} + ) json_data = copy.deepcopy(GQLOperations.DropsPage_ClaimDropRewards) json_data["variables"] = {"input": {"dropInstanceID": drop_instance_id}} self.post_gql_request(json_data) def search_drop_in_inventory(self, streamer, drop_id): - response = self.post_gql_request(GQLOperations.Inventory) - inventory = response["data"]["currentUser"]["inventory"] + inventory = self.__get_inventory() for campaign in inventory["dropCampaignsInProgress"]: for drop in campaign["timeBasedDrops"]: if drop["id"] == drop_id: return drop["self"] raise TimeBasedDropNotFound + def claim_all_drops_from_inventory(self, streamers): + inventory = self.__get_inventory() + for campaign in inventory["dropCampaignsInProgress"]: + for drop in campaign["timeBasedDrops"]: + if drop["self"]["dropInstanceID"] is not None: + self.claim_drop(drop["dropInstanceID"]) + time.sleep(random.uniform(10, 30)) + + def __get_inventory(self): + response = self.post_gql_request(GQLOperations.Inventory) + return response["data"]["currentUser"]["inventory"] + # Load the amount of current points for a channel, check if a bonus is available def load_channel_points_context(self, streamer, less_printing=False): json_data = copy.deepcopy(GQLOperations.ChannelPointsContext) diff --git a/TwitchChannelPointsMiner/classes/WebSocketsPool.py b/TwitchChannelPointsMiner/classes/WebSocketsPool.py index d6fc4450..9a5ad1e3 100644 --- a/TwitchChannelPointsMiner/classes/WebSocketsPool.py +++ b/TwitchChannelPointsMiner/classes/WebSocketsPool.py @@ -295,8 +295,8 @@ def on_message(ws, message): ) if drop["dropInstanceID"] is not None: ws.twitch.claim_drop( - ws.streamers[streamer_index], drop["dropInstanceID"], + ws.streamers[streamer_index], ) except TimeBasedDropNotFound: logger.error( diff --git a/example.py b/example.py index 28262c85..65a4e386 100644 --- a/example.py +++ b/example.py @@ -12,6 +12,7 @@ follow_raid=True, # Follow raid to obtain more points watch_streak=True, # If a streamer go online change the priotiry of streamers array and catch the watch screak. Issue #11 drops_events=True, # If you want to auto claim game drops from Twitch inventory Issue #21 + claim_drops_startup=False, # If you want to auto claim all drops from Twitch inventory on startup logger_settings=LoggerSettings( save=True, # If you want to save logs in file (suggested) console_level=logging.INFO, # Level of logs - use logging.DEBUG for more info)