forked from charlesbel/Microsoft-Rewards-Farmer
-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve KeyError in getEdgeVersions and bug in getLanguageCountry (
#237) 1. keyError in getEdgeVersions Looks like users "randomly" get response json with capitalized or uncapitalized key. Thus, add temp function to ignore the capitalization. 3. Key Error in `getLanguageCountry()` country in config.yaml is using key 'geolocation', while it's fetched by the wrong key 'location' ```yaml default: geolocation: US # Replace with your country code https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ``` ```python @staticmethod def getLanguageCountry(language: str, country: str) -> tuple[str, str]: if not country: country = CONFIG.get("default").get("location") ```
- Loading branch information
Showing
6 changed files
with
178 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,3 +187,4 @@ runbot.bat | |
/google_trends.dir | ||
/google_trends.bak | ||
/config-private.yaml | ||
mypy.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import contextlib | ||
import random | ||
import time | ||
|
||
from selenium.common import TimeoutException | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.remote.webelement import WebElement | ||
|
||
from src.browser import Browser | ||
|
||
|
||
class Activities: | ||
def __init__(self, browser: Browser): | ||
self.browser = browser | ||
self.webdriver = browser.webdriver | ||
|
||
def openDailySetActivity(self, cardId: int): | ||
# Open the Daily Set activity for the given cardId | ||
element = self.webdriver.find_element(By.XPATH, | ||
f'//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[{cardId}]/div/card-content/mee-rewards-daily-set-item-content/div/a', ) | ||
self.browser.utils.click(element) | ||
self.browser.utils.switchToNewTab(timeToWait=8) | ||
|
||
def openMorePromotionsActivity(self, cardId: int): | ||
# Open the More Promotions activity for the given cardId | ||
element = self.webdriver.find_element(By.CSS_SELECTOR, | ||
f"#more-activities > .m-card-group > .ng-scope:nth-child({cardId + 1}) .ds-card-sec") | ||
self.browser.utils.click(element) | ||
self.browser.utils.switchToNewTab(timeToWait=5) | ||
|
||
def completeSearch(self): | ||
# Simulate completing a search activity | ||
time.sleep(random.randint(10, 15)) | ||
self.browser.utils.closeCurrentTab() | ||
|
||
def completeSurvey(self): | ||
# Simulate completing a survey activity | ||
# noinspection SpellCheckingInspection | ||
self.webdriver.find_element(By.ID, f"btoption{random.randint(0, 1)}").click() | ||
time.sleep(random.randint(10, 15)) | ||
self.browser.utils.closeCurrentTab() | ||
|
||
def completeQuiz(self): | ||
# Simulate completing a quiz activity | ||
with contextlib.suppress(TimeoutException): | ||
startQuiz = self.browser.utils.waitUntilQuizLoads() | ||
self.browser.utils.click(startQuiz) | ||
self.browser.utils.waitUntilVisible( | ||
By.ID, "overlayPanel", 5 | ||
) | ||
currentQuestionNumber: int = self.webdriver.execute_script( | ||
"return _w.rewardsQuizRenderInfo.currentQuestionNumber" | ||
) | ||
maxQuestions = self.webdriver.execute_script( | ||
"return _w.rewardsQuizRenderInfo.maxQuestions" | ||
) | ||
numberOfOptions = self.webdriver.execute_script( | ||
"return _w.rewardsQuizRenderInfo.numberOfOptions" | ||
) | ||
for _ in range(currentQuestionNumber, maxQuestions + 1): | ||
if numberOfOptions == 8: | ||
answers = [] | ||
for i in range(numberOfOptions): | ||
isCorrectOption = self.webdriver.find_element( | ||
By.ID, f"rqAnswerOption{i}" | ||
).get_attribute("iscorrectoption") | ||
if isCorrectOption and isCorrectOption.lower() == "true": | ||
answers.append(f"rqAnswerOption{i}") | ||
for answer in answers: | ||
element = self.webdriver.find_element(By.ID, answer) | ||
self.browser.utils.click(element) | ||
self.browser.utils.waitUntilQuestionRefresh() | ||
elif numberOfOptions in [2, 3, 4]: | ||
correctOption = self.webdriver.execute_script( | ||
"return _w.rewardsQuizRenderInfo.correctAnswer" | ||
) | ||
for i in range(numberOfOptions): | ||
if ( | ||
self.webdriver.find_element( | ||
By.ID, f"rqAnswerOption{i}" | ||
).get_attribute("data-option") | ||
== correctOption | ||
): | ||
element = self.webdriver.find_element(By.ID, f"rqAnswerOption{i}") | ||
self.browser.utils.click(element) | ||
|
||
self.browser.utils.waitUntilQuestionRefresh() | ||
break | ||
self.browser.utils.closeCurrentTab() | ||
|
||
def completeABC(self): | ||
# Simulate completing an ABC activity | ||
counter = self.webdriver.find_element( | ||
By.XPATH, '//*[@id="QuestionPane0"]/div[2]' | ||
).text[:-1][1:] | ||
numberOfQuestions = max(int(s) for s in counter.split() if s.isdigit()) | ||
for question in range(numberOfQuestions): | ||
element = self.webdriver.find_element(By.ID, f"questionOptionChoice{question}{random.randint(0, 2)}") | ||
self.browser.utils.click(element) | ||
time.sleep(random.randint(10, 15)) | ||
element = self.webdriver.find_element(By.ID, f"nextQuestionbtn{question}") | ||
self.browser.utils.click(element) | ||
time.sleep(random.randint(10, 15)) | ||
time.sleep(random.randint(1, 7)) | ||
self.browser.utils.closeCurrentTab() | ||
|
||
def completeThisOrThat(self): | ||
# Simulate completing a This or That activity | ||
startQuiz = self.browser.utils.waitUntilQuizLoads() | ||
self.browser.utils.click(startQuiz) | ||
self.browser.utils.waitUntilVisible( | ||
By.XPATH, '//*[@id="currentQuestionContainer"]/div/div[1]', 10 | ||
) | ||
time.sleep(random.randint(10, 15)) | ||
for _ in range(10): | ||
correctAnswerCode = self.webdriver.execute_script( | ||
"return _w.rewardsQuizRenderInfo.correctAnswer" | ||
) | ||
answer1, answer1Code = self.getAnswerAndCode("rqAnswerOption0") | ||
answer2, answer2Code = self.getAnswerAndCode("rqAnswerOption1") | ||
answerToClick: WebElement | ||
if answer1Code == correctAnswerCode: | ||
answerToClick = answer1 | ||
elif answer2Code == correctAnswerCode: | ||
answerToClick = answer2 | ||
|
||
self.browser.utils.click(answerToClick) | ||
time.sleep(random.randint(10, 15)) | ||
|
||
time.sleep(random.randint(10, 15)) | ||
self.browser.utils.closeCurrentTab() | ||
|
||
def getAnswerAndCode(self, answerId: str) -> tuple[WebElement, str]: | ||
# Helper function to get answer element and its code | ||
answerEncodeKey = self.webdriver.execute_script("return _G.IG") | ||
answer = self.webdriver.find_element(By.ID, answerId) | ||
answerTitle = answer.get_attribute("data-option") | ||
return ( | ||
answer, | ||
self.browser.utils.getAnswerCode(answerEncodeKey, answerTitle), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4ff9d79
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will cause assertion error in getGoogleTrends, where
status_code=400, requests.codes.ok=200
"400 Bad Request: This HTTP status code indicates that the server could not understand the request due to invalid syntax. The client should not repeat the request without modifications."
refer to issue #246