-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
141 lines (116 loc) · 4.94 KB
/
Copy pathpython.py
File metadata and controls
141 lines (116 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random
import time
driver = webdriver.Chrome() # or webdriver.Chrome() if using Chrome
driver.get("https://library.hackmit.org/u/OverwhelmedOrca_93576be8")
def get_book_titles():
book_elements = driver.find_elements(By.CLASS_NAME, "css-i1z3c1")
return [book.text for book in book_elements]
def get_excluded_books():
try:
review_section = driver.find_element(By.XPATH, "//h1[contains(text(), 'The Hack Times Weekly Review')]/..")
book_titles = review_section.find_elements(By.CLASS_NAME, "book-title")
return [title.text for title in book_titles]
except Exception as e:
print(f"Error finding excluded books: {e}")
return []
def make_guess(favorite, least_favorite):
favorite_input = driver.find_element(By.ID, "best")
least_favorite_input = driver.find_element(By.ID, "worst")
submit_button = driver.find_element(By.CLASS_NAME, "styled-button")
favorite_input.clear()
favorite_input.send_keys(favorite)
least_favorite_input.clear()
least_favorite_input.send_keys(least_favorite)
submit_button.click()
def check_result():
try:
error_div = WebDriverWait(driver, 3).until(
EC.presence_of_element_located((By.CLASS_NAME, "error"))
)
error_text = error_div.text
favorite_correct = "was not our favorite book this week" not in error_text
least_favorite_correct = "was not our least favorite book this week" not in error_text
return error_text, favorite_correct, least_favorite_correct
except Exception as e:
print(f"Error in check_result: {e}")
return "Unable to determine result", False, False
def click_retry():
try:
retry_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Retry')]"))
)
retry_button.click()
except:
print("Couldn't find or click the Retry button.")
def check_for_wait_message():
try:
error_div = driver.find_element(By.CLASS_NAME, "error")
return "Please wait a bit before retrying" in error_div.text
except:
return False
def retry_with_wait():
while True:
print("\nOut of guesses. Attempting to retry...")
time.sleep(2)
click_retry()
driver.refresh()
time.sleep(2) # Wait for page to load after refresh
if not check_for_wait_message():
print("Retry successful!")
return
print("Need to wait before retrying. Waiting for 21 seconds...")
time.sleep(21)
def main():
excluded_books = get_excluded_books()
print(f"Excluded books: {excluded_books}") # Debug print
all_books = get_book_titles()
books = [book for book in all_books if book not in excluded_books]
favorite = None
least_favorite = None
guesses_left = 5
while True:
if guesses_left == 0:
retry_with_wait()
guesses_left = 5
favorite = None
least_favorite = None
# Update excluded books and available books
excluded_books = get_excluded_books()
print(f"Updated excluded books: {excluded_books}") # Debug print
all_books = get_book_titles()
books = [book for book in all_books if book not in excluded_books]
continue
if favorite is None:
favorite = random.choice(books)
if least_favorite is None:
least_favorite = random.choice([b for b in books if b != favorite])
print(f"\nMaking a guess:")
print(f"Favorite: {favorite}")
print(f"Least Favorite: {least_favorite}")
print(f"Guesses left: {guesses_left}")
make_guess(favorite, least_favorite)
time.sleep(2) # Wait for the page to update
result, favorite_correct, least_favorite_correct = check_result()
print(f"\nResult: {result}")
print(f"Favorite book guess: {'Correct' if favorite_correct else 'Wrong'}")
print(f"Least favorite book guess: {'Correct' if least_favorite_correct else 'Wrong'}")
print("-" * 50)
if "Unable to determine result" in result:
print("Error occurred. Please check the website manually.")
break
if favorite_correct and least_favorite_correct:
print("Both guesses are correct! Stopping the script.")
break
if not favorite_correct:
favorite = None
if not least_favorite_correct:
least_favorite = None
guesses_left -= 1
print("\nThe browser window will remain open. You can close it manually when you're done.")
input("Press Enter to exit the script...")
if __name__ == "__main__":
main()