Spaces:
Build error
Build error
Add automatic login to rs wordle to the selenium player
Browse filesNow the player can login to rs wordle using its google login with selenium
Fixed a bug in the number of guesses made by the player
rs_wordle_player/firebase_connector.py
CHANGED
|
@@ -20,7 +20,7 @@ class FirebaseConnector():
|
|
| 20 |
return db
|
| 21 |
|
| 22 |
def get_credentials_path(self):
|
| 23 |
-
credentials_path = os.getenv('
|
| 24 |
return credentials_path
|
| 25 |
|
| 26 |
def get_user(self):
|
|
|
|
| 20 |
return db
|
| 21 |
|
| 22 |
def get_credentials_path(self):
|
| 23 |
+
credentials_path = os.getenv('RS_FIREBASE_CREDENTIALS_PATH')
|
| 24 |
return credentials_path
|
| 25 |
|
| 26 |
def get_user(self):
|
rs_wordle_player/rs_wordle_player.py
CHANGED
|
@@ -34,13 +34,13 @@ def play_game(player, fb_connector, env, model_path):
|
|
| 34 |
states = []
|
| 35 |
finished = False
|
| 36 |
attempts = 0
|
| 37 |
-
while not finished or attempts
|
| 38 |
attempts += 1
|
| 39 |
new_attempt = suggest(env, words, states, model_path)
|
| 40 |
player.play_word(new_attempt)
|
| 41 |
words, states = get_attempts(fb_connector)
|
| 42 |
finished = states[-1] == '22222'
|
| 43 |
-
return
|
| 44 |
|
| 45 |
|
| 46 |
def play():
|
|
@@ -50,12 +50,12 @@ def play():
|
|
| 50 |
env = get_env()
|
| 51 |
goal_word = fb.today_word()
|
| 52 |
if goal_word and len(goal_word) == 5:
|
| 53 |
-
|
| 54 |
else:
|
| 55 |
print("Can't play, today's word is not 5 characters long")
|
| 56 |
-
|
| 57 |
player.finish()
|
| 58 |
-
return
|
| 59 |
|
| 60 |
|
| 61 |
if __name__ == '__main__':
|
|
|
|
| 34 |
states = []
|
| 35 |
finished = False
|
| 36 |
attempts = 0
|
| 37 |
+
while not finished or attempts < 6:
|
| 38 |
attempts += 1
|
| 39 |
new_attempt = suggest(env, words, states, model_path)
|
| 40 |
player.play_word(new_attempt)
|
| 41 |
words, states = get_attempts(fb_connector)
|
| 42 |
finished = states[-1] == '22222'
|
| 43 |
+
return words, finished
|
| 44 |
|
| 45 |
|
| 46 |
def play():
|
|
|
|
| 50 |
env = get_env()
|
| 51 |
goal_word = fb.today_word()
|
| 52 |
if goal_word and len(goal_word) == 5:
|
| 53 |
+
words, won = play_game(player, fb, env, model_path)
|
| 54 |
else:
|
| 55 |
print("Can't play, today's word is not 5 characters long")
|
| 56 |
+
words, won = None, None
|
| 57 |
player.finish()
|
| 58 |
+
return words, won
|
| 59 |
|
| 60 |
|
| 61 |
if __name__ == '__main__':
|
rs_wordle_player/selenium_player.py
CHANGED
|
@@ -1,25 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 4 |
from selenium.webdriver.common.keys import Keys
|
| 5 |
|
| 6 |
|
| 7 |
class SeleniumPlayer():
|
| 8 |
|
| 9 |
def __init__(self):
|
|
|
|
| 10 |
self.driver = self.get_driver()
|
|
|
|
| 11 |
|
| 12 |
def get_driver(self):
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
return driver
|
| 16 |
|
| 17 |
def get_wordle_url(self):
|
| 18 |
load_dotenv()
|
| 19 |
return os.getenv('RS_WORDLE_URL')
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def play_word(self, word):
|
| 22 |
-
element = self.driver.
|
| 23 |
# simulate typing the letters in the word into the input field
|
| 24 |
element.send_keys(word)
|
| 25 |
# simulate pressing the Enter key
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from selenium import webdriver
|
| 5 |
+
from selenium.webdriver.chrome.options import Options
|
| 6 |
+
from selenium.webdriver.common.by import By
|
| 7 |
from selenium.webdriver.common.keys import Keys
|
| 8 |
|
| 9 |
|
| 10 |
class SeleniumPlayer():
|
| 11 |
|
| 12 |
def __init__(self):
|
| 13 |
+
self.wordle_url = self.get_wordle_url()
|
| 14 |
self.driver = self.get_driver()
|
| 15 |
+
self.log_in()
|
| 16 |
|
| 17 |
def get_driver(self):
|
| 18 |
+
chrome_options = Options()
|
| 19 |
+
chrome_options.add_experimental_option("detach", True)
|
| 20 |
+
driver = webdriver.Chrome(options=chrome_options)
|
| 21 |
+
driver.get(self.wordle_url)
|
| 22 |
return driver
|
| 23 |
|
| 24 |
def get_wordle_url(self):
|
| 25 |
load_dotenv()
|
| 26 |
return os.getenv('RS_WORDLE_URL')
|
| 27 |
|
| 28 |
+
def get_credentials(self):
|
| 29 |
+
load_dotenv()
|
| 30 |
+
username = os.getenv('RS_WORDLE_USER')
|
| 31 |
+
password = os.getenv('RS_WORDLE_PASSWORD')
|
| 32 |
+
return username, password
|
| 33 |
+
|
| 34 |
+
def logged_in(self):
|
| 35 |
+
return self.driver.current_url != self.wordle_url + '/login'
|
| 36 |
+
|
| 37 |
+
def log_in(self):
|
| 38 |
+
if not self.logged_in():
|
| 39 |
+
login_div = self.driver.find_element(By.CLASS_NAME, 'login-button')
|
| 40 |
+
login_btns = login_div.find_elements(By.TAG_NAME, 'button')
|
| 41 |
+
login_btn = login_btns[0]
|
| 42 |
+
login_btn.click()
|
| 43 |
+
time.sleep(10)
|
| 44 |
+
wordle_window = self.driver.window_handles[0]
|
| 45 |
+
login_window = self.driver.window_handles[1]
|
| 46 |
+
self.driver.switch_to.window(login_window)
|
| 47 |
+
username, password = self.get_credentials()
|
| 48 |
+
element = self.driver.find_element(By.ID, 'identifierId')
|
| 49 |
+
element.send_keys(username)
|
| 50 |
+
element.send_keys(Keys.ENTER)
|
| 51 |
+
time.sleep(10)
|
| 52 |
+
element = self.driver.find_element(By.NAME, 'password')
|
| 53 |
+
element.send_keys(password)
|
| 54 |
+
element.send_keys(Keys.ENTER)
|
| 55 |
+
time.sleep(10)
|
| 56 |
+
self.driver.switch_to.window(wordle_window)
|
| 57 |
+
|
| 58 |
def play_word(self, word):
|
| 59 |
+
element = self.driver.find_element(By.TAG_NAME, 'html')
|
| 60 |
# simulate typing the letters in the word into the input field
|
| 61 |
element.send_keys(word)
|
| 62 |
# simulate pressing the Enter key
|