Skip to content

Commit

Permalink
Use the session for interact with helix api. Again check if the div i…
Browse files Browse the repository at this point in the history
…s open and not close. Reduce the start time for thread if we have lost time to open the div
  • Loading branch information
Tkd-Alex committed Jan 22, 2021
1 parent abc9408 commit 87da61b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 43 deletions.
8 changes: 1 addition & 7 deletions TwitchChannelPointsMiner/classes/Twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
)
Expand Down
61 changes: 35 additions & 26 deletions TwitchChannelPointsMiner/classes/TwitchBrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand All @@ -399,53 +400,61 @@ 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(
expected_conditions.visibility_of_element_located(
(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}"
Expand Down
6 changes: 2 additions & 4 deletions TwitchChannelPointsMiner/classes/TwitchWebSocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 16 additions & 6 deletions TwitchChannelPointsMiner/classes/WebSocketsPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -226,22 +226,32 @@ 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],),
)
place_bet_thread.daemon = True
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:
Expand Down

0 comments on commit 87da61b

Please sign in to comment.