From 87da61b01d9ab7d0bc5346a3b1bd66f718fe0728 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 22 Jan 2021 18:52:52 +0100 Subject: [PATCH] Use the session for interact with helix api. Again check if the div is open and not close. Reduce the start time for thread if we have lost time to open the div --- TwitchChannelPointsMiner/classes/Twitch.py | 8 +-- .../classes/TwitchBrowser.py | 61 +++++++++++-------- .../classes/TwitchWebSocket.py | 6 +- .../classes/WebSocketsPool.py | 22 +++++-- 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/TwitchChannelPointsMiner/classes/Twitch.py b/TwitchChannelPointsMiner/classes/Twitch.py index a555e526..25d71cca 100644 --- a/TwitchChannelPointsMiner/classes/Twitch.py +++ b/TwitchChannelPointsMiner/classes/Twitch.py @@ -239,13 +239,7 @@ def get_followers(self, first=100): def __do_helix_request(self, query, response_as_json=True): url = f"https://api.twitch.tv/helix/{query.strip('/')}" - response = requests.get( - url, - headers={ - "Authorization": f"Bearer {self.twitch_login.get_auth_token()}", - "Client-Id": TWITCH_CLIENT_ID, - }, - ) + response = self.twitch_login.session.get(url) logger.debug( f"Query: {query}, Status code: {response.status_code}, Content: {response.json()}" ) diff --git a/TwitchChannelPointsMiner/classes/TwitchBrowser.py b/TwitchChannelPointsMiner/classes/TwitchBrowser.py index 0528cc2e..91f85cb5 100644 --- a/TwitchChannelPointsMiner/classes/TwitchBrowser.py +++ b/TwitchChannelPointsMiner/classes/TwitchBrowser.py @@ -357,6 +357,7 @@ def __send_text( return False def start_bet(self, event: EventPrediction): + start_time = time.time() if self.currently_is_betting: logger.info( f"Sorry, unable to start {event}. The browser it's currently betting another event" @@ -378,11 +379,11 @@ def start_bet(self, event: EventPrediction): self.__execute_script(clearStyleChatJS, suppress_error=True) if self.__bet_chains_methods(event) is True: - return self.currently_is_betting + return self.currently_is_betting, time.time() - start_time logger.error( f"Attempt {attempt+1} failed!", extra={"emoji": ":wrench:"} ) - return False + return False, time.time() - start_time def __bet_chains_methods(self, event): if self.__open_coins_menu(event) is True: @@ -399,6 +400,7 @@ def place_bet(self, event: EventPrediction): if event.status == "ACTIVE": if event.box_fillable and self.currently_is_betting: + div_bet_is_open = False self.__debug(event, "place_bet") try: WebDriverWait(self.browser, 1).until( @@ -406,46 +408,53 @@ def place_bet(self, event: EventPrediction): (By.XPATH, streamBetMainDiv) ) ) + div_bet_is_open = True except TimeoutException: logger.info( "The bet div was not found, maybe It was closed. Attempt to open again, hope to be in time", extra={"emoji": ":wrench:"}, ) - if self.__bet_chains_methods(event) is True: + div_bet_is_open = self.__bet_chains_methods(event) + if div_bet_is_open is True: logger.info( "Success! Bet div is now open, we can complete the bet", extra={"emoji": ":wrench:"}, ) - decision = event.bet.calculate(event.streamer.channel_points) - if decision["choice"]: - selector_index = 1 if decision["choice"] == "A" else 2 - logger.info( - f"Decision: {event.bet.get_outcome(selector_index - 1)}", - extra={"emoji": ":wrench:"}, - ) - - try: + if div_bet_is_open is True: + decision = event.bet.calculate(event.streamer.channel_points) + if decision["choice"]: + selector_index = 1 if decision["choice"] == "A" else 2 logger.info( - f"Going to write: {decision['amount']} channel points on input {decision['choice']}", + f"Decision: {event.bet.get_outcome(selector_index - 1)}", extra={"emoji": ":wrench:"}, ) - if ( - self.__send_text_on_bet( - event, selector_index, decision["amount"] - ) - is True - ): + + try: logger.info( - f"Going to place the bet for {event}", + f"Going to write: {decision['amount']} channel points on input {decision['choice']}", extra={"emoji": ":wrench:"}, ) - if self.__click_on_vote(event, selector_index) is True: - self.__debug(event, "click_on_vote") - event.bet_placed = True - time.sleep(random.uniform(5, 10)) - except Exception: - logger.error("Exception raised", exc_info=True) + if ( + self.__send_text_on_bet( + event, selector_index, decision["amount"] + ) + is True + ): + logger.info( + f"Going to place the bet for {event}", + extra={"emoji": ":wrench:"}, + ) + if self.__click_on_vote(event, selector_index) is True: + self.__debug(event, "click_on_vote") + event.bet_placed = True + time.sleep(random.uniform(5, 10)) + except Exception: + logger.error("Exception raised", exc_info=True) + else: + logger.info( + "Sorry, unable to complete the bet. The bet div still closed" + ) else: logger.info( f"Sorry, unable to complete the bet. Event box fillable: {event.box_fillable}, the browser is betting: {self.currently_is_betting}" diff --git a/TwitchChannelPointsMiner/classes/TwitchWebSocket.py b/TwitchChannelPointsMiner/classes/TwitchWebSocket.py index 7a43d14c..1ad8165f 100644 --- a/TwitchChannelPointsMiner/classes/TwitchWebSocket.py +++ b/TwitchChannelPointsMiner/classes/TwitchWebSocket.py @@ -66,9 +66,7 @@ def reset(self, parent_pool): self.last_ping = time.time() def elapsed_last_pong(self): - elapsed = timedelta(seconds=int(time.time() - float(self.last_pong))) - return elapsed.total_seconds() // 60 + return (time.time - self.last_pong) // 60 def elapsed_last_ping(self): - elapsed = timedelta(seconds=int(time.time() - float(self.last_ping))) - return elapsed.total_seconds() // 60 + return (time.time - self.last_ping) // 60 diff --git a/TwitchChannelPointsMiner/classes/WebSocketsPool.py b/TwitchChannelPointsMiner/classes/WebSocketsPool.py index 171d13e5..6372f92c 100644 --- a/TwitchChannelPointsMiner/classes/WebSocketsPool.py +++ b/TwitchChannelPointsMiner/classes/WebSocketsPool.py @@ -207,7 +207,7 @@ def on_message(ws, message): event_dict["prediction_window_seconds"] ) prediction_window_seconds -= ( - 30 if prediction_window_seconds <= 120 else 60 + 25 if prediction_window_seconds <= 120 else 50 ) event = EventPrediction( ws.streamers[streamer_index], @@ -226,14 +226,24 @@ def on_message(ws, message): ): ws.events_predictions[event_id] = event if ws.twitch_browser.currently_is_betting is False: - if ws.twitch_browser.start_bet( + ( + start_bet_status, + execution_time, + ) = ws.twitch_browser.start_bet( ws.events_predictions[event_id] - ): + ) + if start_bet_status is True: # place_bet_thread = threading.Timer(event.closing_bet_after(current_timestamp), ws.twitch.make_predictions, (ws.events_predictions[event_id],)) - place_bet_thread = threading.Timer( + execution_time = round(execution_time, 2) + start_after = ( event.closing_bet_after( current_timestamp - ), + ) + - execution_time + ) + start_after = max(1, start_after) + place_bet_thread = threading.Timer( + start_after, ws.twitch_browser.place_bet, (ws.events_predictions[event_id],), ) @@ -241,7 +251,7 @@ def on_message(ws, message): place_bet_thread.start() logger.info( - f"Place the bet after: {event.closing_bet_after(current_timestamp)}s for: {ws.events_predictions[event_id]}", + f"Place the bet after: {start_after}s for: {ws.events_predictions[event_id]}", extra={"emoji": ":alarm_clock:"}, ) else: