implémentation de la recherche PC + du VNC + requirement.txt + joli logger + séparation en plus de fichiers

This commit is contained in:
piair
2024-01-18 16:35:17 +01:00
parent 48e201043b
commit 4c376acf17
8 changed files with 159 additions and 100 deletions

61
V7/actions/cards.py Normal file
View File

@ -0,0 +1,61 @@
from playwright.sync_api import sync_playwright, expect, Page, BrowserContext
from playwright_stealth import stealth_sync
from actions.quiz import play
from tools.logger import *
from tools.logger import *
def all_cards(page: Page):
# input("1")
page.goto("https://rewards.bing.com")
page.wait_for_load_state('load')
daily_cards(page)
more_cards(page)
def daily_cards(page: Page):
for i in range(1, 4):
card = page.locator(f'#daily-sets > mee-card-group:nth-child(7) > div > mee-card:nth-child({i}) > div')
if "mee-icon-AddMedium" not in card.inner_html():
info(f'Daily Card {i} : {"Validated" if "mee-icon-AddMedium" not in card.inner_html() else "Failure"}')
continue
with page.expect_popup() as page1_info:
card.click()
page1 = page1_info.value
stealth_sync(page1)
page1.wait_for_load_state('load')
try:
selector = page1.get_by_role("button", name="Accepter")
expect(selector).to_be_visible(timeout=10_000)
selector.click()
except AssertionError as e:
pass # The RGPD button should not be there in the first place
play(page1)
page1.close()
page.reload()
info(f'Daily Card {i} : {"Validated" if "mee-icon-AddMedium" not in card.inner_html() else "Failure"}')
def more_cards(page: Page):
c = 1
while c < 15: # todo I don't really know why it stops without raising any errors, but I guess it's fine
card = page.locator(f"#more-activities > div > mee-card:nth-child({c})")
try:
expect(card).to_be_visible(timeout=10_000)
except Exception as e: # The card probably does not exist
debug(e)
break
if "mee-icon-AddMedium" in card.inner_html():
info(f"Playing more cards {c}.")
with page.expect_popup() as page1_info:
card.click()
page1 = page1_info.value
stealth_sync(page1)
page1.wait_for_load_state('load')
play(page1)
page1.close()
page.reload()
info(f'More Card {c} : {"Validated" if "mee-icon-AddMedium" not in card.inner_html() else "Failure"}')
c += 1

View File

@ -31,17 +31,19 @@ def detect_quiz_type(page: Page) -> int:
info("Detecting quiz type.")
if "bt_PollRadio" in page.content():
return 1
elif "rqQuestionState" in page.content():
# The quiz is already started
return page.content().count("rqAnswerOption") - 1
else:
try:
# RGPD
selector = page.locator("rqStartQuiz")
selector = page.locator("#rqStartQuiz")
expect(selector).to_be_visible(timeout=10_000)
selector.click()
return page.content().count("rqAnswerOption") - 1
except AssertionError as e:
if "rqQuestionState" in page.content():
# The quiz is already started
warning("Detected via recovery mode.")
return page.content().count("rqAnswerOption") - 1
return 0
@ -59,13 +61,14 @@ def play_quiz_8(page: Page):
element = page.locator(f'#rqAnswerOption{i - 1}')
# todo can probably be optimised using filter and has_text
if 'iscorrectoption="True"' in element.evaluate("el => el.outerHTML"):
correct_answers.append(f'rqAnswerOption{i - 1}')
correct_answers.append(f'#rqAnswerOption{i - 1}')
shuffle(correct_answers)
for answer_id in correct_answers:
page.locator(answer_id).click()
page.wait_for_timeout(1000)
page.wait_for_timeout(3000) # todo check if there is a better method than hardcoded timout
page.wait_for_timeout(3000) # todo check if there is a better method than hardcoded timout
def play_quiz_4(page: Page):
@ -106,6 +109,7 @@ def play_quiz_2(page: Page):
page.locator(f"#rqAnswerOption{correct_answer_value}").click()
page.wait_for_timeout(2000)
info("Quiz 2 successful.")
page.wait_for_timeout(3000) # todo check if there is a better method than hardcoded timout
def play_poll(page: Page):
@ -113,3 +117,4 @@ def play_poll(page: Page):
answer_elem = page.locator(f"#btoption{choice([0, 1])}")
answer_elem.click()
info("Poll successful.")
page.wait_for_timeout(3000) # todo check if there is a better method than hardcoded timout

View File

@ -21,3 +21,5 @@ def pc_search(page: Page) -> None:
page.get_by_label("Enter your search here -").click()
page.get_by_label("Enter your search here -").fill(word)
page.get_by_label("Enter your search here -").press("Enter")
page.wait_for_load_state("load")
page.wait_for_timeout(3000) # todo check if there is a better method than hardcoded timout