Skip to content

Commit

Permalink
Another good refactory. This will most likely break 27b9eda . Move in…
Browse files Browse the repository at this point in the history
… two classes Selectors and Javascript for Constants.browser
  • Loading branch information
Tkd-Alex committed Jan 29, 2021
1 parent 13bbe71 commit 022fbb3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 81 deletions.
55 changes: 22 additions & 33 deletions TwitchChannelPointsMiner/classes/TwitchBrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,7 @@
from TwitchChannelPointsMiner.classes.EventPrediction import EventPrediction
from TwitchChannelPointsMiner.utils import bet_condition, get_user_agent
from TwitchChannelPointsMiner.constants.twitch import URL
from TwitchChannelPointsMiner.constants.browser import (
cookiePolicyQuery,
streamCoinsMenuXP,
streamCoinsMenuJS,
streamBetTitleInBet,
streamBetCustomVoteXP,
streamBetCustomVoteJS,
streamBetMainDiv,
streamBetVoteInputXP,
streamBetVoteButtonXP,
streamBetVoteInputJS,
streamBetVoteButtonJS,
localStorageJS,
clearStyleChatJS,
maximizeBetWindowJS,
scrollDownBetWindowJS,
)
from TwitchChannelPointsMiner.constants.browser import Selectors, Javascript

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -124,11 +108,13 @@ def __init_twitch(self):
self.browser.add_cookie(cookie)
time.sleep(random.uniform(2.5, 3.5))

self.__click_when_exist(cookiePolicyQuery, By.CSS_SELECTOR, suppress_error=True)
self.__click_when_exist(
Selectors.cookiePolicy, By.CSS_SELECTOR, suppress_error=True
)
time.sleep(random.uniform(0.5, 1.5))

# Edit value in localStorage for dark theme, point consent etc.
self.__execute_script(localStorageJS)
self.__execute_script(Javascript.localStorage)
time.sleep(random.uniform(0.5, 1.5))
self.__blank()

Expand Down Expand Up @@ -290,11 +276,14 @@ def start_bet(self, event: EventPrediction):
self.browser.get(event.streamer.chat_url)
time.sleep(random.uniform(3, 5))
self.__click_when_exist(
cookiePolicyQuery, By.CSS_SELECTOR, suppress_error=True, timeout=1.5
Selectors.cookiePolicy,
By.CSS_SELECTOR,
suppress_error=True,
timeout=1.5,
)

# Hide the chat ... Don't ask me why
self.__execute_script(clearStyleChatJS, suppress_error=True)
self.__execute_script(Javascript.clearStyleChat, suppress_error=True)

if self.__bet_chains_methods(event) is True:
return self.currently_is_betting, time.time() - start_time
Expand Down Expand Up @@ -324,7 +313,7 @@ def place_bet(self, event: EventPrediction):
try:
WebDriverWait(self.browser, 1).until(
expected_conditions.visibility_of_element_located(
(By.XPATH, streamBetMainDiv)
(By.XPATH, Selectors.betMainDivXP)
)
)
div_bet_is_open = True
Expand Down Expand Up @@ -388,9 +377,9 @@ def place_bet(self, event: EventPrediction):

def __open_coins_menu(self, event: EventPrediction):
logger.info(f"Opening coins menu for {event}", extra={"emoji": ":wrench:"})
status = self.__click_when_exist(streamCoinsMenuXP, By.XPATH)
status = self.__click_when_exist(Selectors.coinsMenuXP, By.XPATH)
if status is False:
status = self.__execute_script(streamCoinsMenuJS)
status = self.__execute_script(Javascript.streamCoinsMenu)

if status is True:
time.sleep(random.uniform(0.01, 0.1))
Expand All @@ -400,11 +389,11 @@ def __open_coins_menu(self, event: EventPrediction):

def __click_on_bet(self, event, maximize_div=True):
logger.info(f"Clicking on the bet for {event}", extra={"emoji": ":wrench:"})
if self.__click_when_exist(streamBetTitleInBet, By.CSS_SELECTOR) is True:
if self.__click_when_exist(Selectors.betTitle, By.CSS_SELECTOR) is True:
time.sleep(random.uniform(0.01, 0.1))
if maximize_div is True:
# Edit the css for make the window full-screen in browser. Another useless change
self.__execute_script(maximizeBetWindowJS, suppress_error=True)
self.__execute_script(Javascript.maximizeBetWindow, suppress_error=True)
self.__debug(event, "click_on_bet")
return True
return False
Expand All @@ -417,12 +406,12 @@ def __enable_custom_bet_value(self, event, scroll_down=True):

if scroll_down is True:
time.sleep(random.uniform(0.01, 0.1))
if self.__execute_script(scrollDownBetWindowJS) is False:
if self.__execute_script(Javascript.scrollDownBetWindow) is False:
logger.error("Unable to scroll down in the bet window!")

status = self.__click_when_exist(streamBetCustomVoteXP, By.CSS_SELECTOR)
status = self.__click_when_exist(Selectors.betCustomVote, By.CSS_SELECTOR)
if status is False:
status = self.__execute_script(streamBetCustomVoteJS)
status = self.__execute_script(Javascript.betCustomVote)

if status is True:
time.sleep(random.uniform(0.01, 0.1))
Expand All @@ -439,11 +428,11 @@ def __enable_custom_bet_value(self, event, scroll_down=True):
def __send_text_on_bet(self, event, selector_index, text):
self.__debug(event, "before__send_text")
status = self.__send_text(
f"{streamBetVoteInputXP}[{selector_index}]", text, By.XPATH
f"{Selectors.betVoteInputXP}[{selector_index}]", text, By.XPATH
)
if status is False:
status = self.__execute_script(
streamBetVoteInputJS.format(int(selector_index) - 1, int(text))
Javascript.betVoteInput.format(int(selector_index) - 1, int(text))
)

if status is True:
Expand All @@ -453,11 +442,11 @@ def __send_text_on_bet(self, event, selector_index, text):

def __click_on_vote(self, event, selector_index):
status = self.__click_when_exist(
f"{streamBetVoteButtonXP}[{selector_index}]", By.XPATH
f"{Selectors.betVoteButtonXP}[{selector_index}]", By.XPATH
)
if status is False:
status = self.__execute_script(
streamBetVoteButtonJS.format(int(selector_index) - 1)
Javascript.betVoteButton.format(int(selector_index) - 1)
)

if status is True:
Expand Down
95 changes: 47 additions & 48 deletions TwitchChannelPointsMiner/constants/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,57 @@
},
}

# XPath Selector and Javascript helpers
cookiePolicyQuery = 'button[data-a-target="consent-banner-accept"]'

streamCoinsMenuXP = '//div[@data-test-selector="community-points-summary"]//button'
streamCoinsMenuJS = 'document.querySelector("[data-test-selector=\'community-points-summary\']").getElementsByTagName("button")[0].click();'
class Selectors:
# XPath Selector and Javascript helpers
cookiePolicy = 'button[data-a-target="consent-banner-accept"]'
coinsMenuXP = '//div[@data-test-selector="community-points-summary"]//button'
betTitle = '[data-test-selector="predictions-list-item__title"]'
betCustomVote = "button[data-test-selector='prediction-checkout-active-footer__input-type-toggle']"
betMainDivXP = "//div[@id='channel-points-reward-center-body']//div[contains(@class,'custom-prediction-button')]"
betVoteInputXP = f"({betMainDivXP}//input)"
betVoteButtonXP = f"({betMainDivXP}//button)"

streamBetTitleInBet = '[data-test-selector="predictions-list-item__title"]'

streamBetCustomVoteXP = (
"button[data-test-selector='prediction-checkout-active-footer__input-type-toggle']"
)
streamBetCustomVoteJS = f'document.querySelector("{streamBetCustomVoteXP}").click();'
class Javascript:
# Helpers for selenium. I think is very useless
coinsMenu = 'document.querySelector("[data-test-selector=\'community-points-summary\']").getElementsByTagName("button")[0].click();'
betVoteInput = 'document.getElementById("channel-points-reward-center-body").getElementsByTagName("input")[{}].value = {};'
betVoteButton = 'document.getElementById("channel-points-reward-center-body").getElementsByTagName("button")[{}].click();'
betCustomVote = f'document.querySelector("{Selectors.betCustomVote}").click();'

streamBetMainDiv = "//div[@id='channel-points-reward-center-body']//div[contains(@class,'custom-prediction-button')]"
streamBetVoteInputXP = f"({streamBetMainDiv}//input)"
streamBetVoteButtonXP = f"({streamBetMainDiv}//button)"
# Some Javascript code that should help the script
localStorage = """
window.localStorage.setItem("volume", 0);
window.localStorage.setItem("channelPointsOnboardingDismissed", true);
window.localStorage.setItem("twilight.theme", 1);
window.localStorage.setItem("mature", true);
window.localStorage.setItem("rebrand-notice-dismissed", true);
window.localStorage.setItem("emoteAnimationsEnabled", false);
window.localStorage.setItem("chatPauseSetting", "ALTKEY");
"""
clearStyleChat = """
var item = document.querySelector('[data-test-selector="chat-scrollable-area__message-container"]');
if (item) {
var parent = item.closest("div.simplebar-scroll-content");
if(parent) parent.hidden = true;
}
var header = document.querySelector('[data-test-selector="channel-leaderboard-container"]');
if(header) header.hidden = true;
"""
maximizeBetWindow = """
var absolute = document.querySelector('[aria-describedby="channel-points-reward-center-body"]').closest("div.tw-absolute")
if(absolute) absolute.classList.remove("tw-absolute")
streamBetVoteInputJS = 'document.getElementById("channel-points-reward-center-body").getElementsByTagName("input")[{}].value = {};'
streamBetVoteButtonJS = 'document.getElementById("channel-points-reward-center-body").getElementsByTagName("button")[{}].click();'
document.getElementsByClassName("reward-center__content")[0].style.width = "44rem";
document.getElementsByClassName("reward-center__content")[0].style.height = "55rem";
# Some Javascript code that should help the script
localStorageJS = """
window.localStorage.setItem("volume", 0);
window.localStorage.setItem("channelPointsOnboardingDismissed", true);
window.localStorage.setItem("twilight.theme", 1);
window.localStorage.setItem("mature", true);
window.localStorage.setItem("rebrand-notice-dismissed", true);
window.localStorage.setItem("emoteAnimationsEnabled", false);
window.localStorage.setItem("chatPauseSetting", "ALTKEY");
"""
clearStyleChatJS = """
var item = document.querySelector('[data-test-selector="chat-scrollable-area__message-container"]');
if (item) {
var parent = item.closest("div.simplebar-scroll-content");
if(parent) parent.hidden = true;
}
var header = document.querySelector('[data-test-selector="channel-leaderboard-container"]');
if(header) header.hidden = true;
"""
maximizeBetWindowJS = """
var absolute = document.querySelector('[aria-describedby="channel-points-reward-center-body"]').closest("div.tw-absolute")
if(absolute) absolute.classList.remove("tw-absolute")
document.getElementsByClassName("reward-center__content")[0].style.width = "44rem";
document.getElementsByClassName("reward-center__content")[0].style.height = "55rem";
document.querySelector('[aria-describedby="channel-points-reward-center-body"]').style["max-height"] = "55rem";
document.querySelector('[aria-describedby="channel-points-reward-center-body"]').style["max-height"] = "55rem";
document.getElementsByClassName("reward-center-body")[0].style["max-width"] = "44rem";
// document.getElementsByClassName("reward-center-body")[0].style["min-height"] = "55rem";
"""
scrollDownBetWindowJS = """
var scrollable = document.getElementById("channel-points-reward-center-body").closest("div.simplebar-scroll-content");
scrollable.scrollTop = scrollable.scrollHeight;
"""
document.getElementsByClassName("reward-center-body")[0].style["max-width"] = "44rem";
// document.getElementsByClassName("reward-center-body")[0].style["min-height"] = "55rem";
"""
scrollDownBetWindow = """
var scrollable = document.getElementById("channel-points-reward-center-body").closest("div.simplebar-scroll-content");
scrollable.scrollTop = scrollable.scrollHeight;
"""

0 comments on commit 022fbb3

Please sign in to comment.