-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
228 lines (163 loc) · 6.74 KB
/
main.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import datetime
import time
import os
import json
from modules.WordleAnalyzer import WordleAnalyzer
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.by import By
def GetShadowRootObject (driver, tagname):
query = f"return document.getElementsByTagName ('{tagname}')[0].shadowRoot"
return driver.execute_script (query)
def FindScriptFile (driver, jsHeader='/games/wordle/main'):
page = driver.find_elements(By.TAG_NAME, 'script')
for element in page:
link = str(element.get_attribute('src'))
if jsHeader in link and '.js' == link[-3:]:
return link
def GotoTab (driver, tabIndex):
assert len (driver.window_handles) > tabIndex
driver.switch_to.window(driver.window_handles[tabIndex])
def OpenLinkNewTab (driver, link):
driver.execute_script(f"window.open('{link}','_blank')")
def AddWordList (wordLists, wordListString):
wordListString = wordListString.replace("[","").replace("]", "").replace("\"", "")
wordList = wordListString.split(",")
for word in wordList:
word = word.strip()
if word not in wordLists and len (word) == 5:
wordLists.append(word)
return wordLists
def ExtractWordList (driver):
GotoTab (driver, 1)
time.sleep(5)
fileContent = driver.find_elements(by=By.TAG_NAME, value="body")[0].text
wordLists = []
# Get List A
startIndex = fileContent.find("var mo=")
subContent = fileContent[startIndex:]
# print (subContent)
restartIndex = subContent.find("[")
endIndex = subContent.find("]")
wordListString = subContent[restartIndex:endIndex]
wordLists = AddWordList (wordLists, wordListString)
# Get List B
startIndex = subContent.find("[")
subContent = fileContent[startIndex:]
restartIndex = subContent.find("[")
endIndex = subContent.find("]")
wordListString = subContent[restartIndex:endIndex]
wordLists = AddWordList (wordLists, wordListString)
# GotoTab (driver, 0)
return wordLists
def GuessWord (driver, word):
# For now, always assume word is valid.
GotoTab (driver, 0)
element = driver.find_elements(by=By.TAG_NAME, value="body")[0]
assert len (word) == 5
element.send_keys(word)
element.send_keys(Keys.RETURN)
todate = datetime.datetime.now().date().isoformat()
time.sleep(3)
state = driver.save_screenshot(f'./images/{todate}.png')
# print (f'Screenshot saved: {state}')
def SaveBase (driver):
# For now, always assume word is valid.
GotoTab (driver, 0)
element = driver.find_elements(by=By.TAG_NAME, value="body")[0]
time.sleep(3)
state = driver.save_screenshot(f'./images/base.png')
# print (f'Base saved: {state}')
def FindWord (attempted, ignoreLetters, knownLetters, lettersWithPlaces):
for i, word in enumerate(wordList):
ignoreWord = False
if len(ignoreLetters) > 0:
for char in word:
if char in ignoreLetters:
ignoreWord = True
break
if len(lettersWithPlaces) > 0:
for char, position in lettersWithPlaces:
if not (word[position] == char):
ignoreWord = True
break
if len(knownLetters) > 0:
for char, position in knownLetters:
if not char in word or (word[position] == char):
ignoreWord = True
break
if ignoreWord:
continue
return word
if __name__ == "__main__":
binaryPath = 'C:\\Users\\adamthahir\\Documents\\geckodriver.exe'
driverPath = 'C:\\Users\\adamthahir\\Documents\\geckodriver.exe'
site = 'https://www.nytimes.com/games/wordle/index.html'
jsHeader = '/games/wordle/main'
if os.path.exists("config.json"):
with open ("config.json") as f:
data = json.load(f)
binaryPath = data['binary'] if 'binary' in data.keys() else binaryPath
driverPath = data['driver'] if 'driver' in data.keys() else driverPath
site = data['site'] if 'site' in data.keys() else site
jsHeader = data['jsHeader'] if 'jsHeader' in data.keys() else jsHeader
binary = FirefoxBinary(binaryPath)
driver = webdriver.Firefox(executable_path=driverPath)
driver.get(site)
time.sleep(2)
driver.find_elements(by=By.TAG_NAME, value="body")[0].click()
scriptLink = FindScriptFile(driver, jsHeader)
OpenLinkNewTab(driver, scriptLink)
wordList = ExtractWordList (driver)
print (f'Number of words find: {len(wordList)}')
SaveBase(driver)
todate = datetime.datetime.now().date().isoformat()
analyzer = WordleAnalyzer(todate)
analyzer.MakeBoard()
attempts = 0
attempted = []
ignoreLetters = []
knownLetters = []
lettersWithPlaces = []
gameWon = None
while True:
if len (attempted) == 0:
# I like starting with the word AGILE
word = "agile"
else:
word = FindWord(attempted, ignoreLetters, knownLetters, lettersWithPlaces)
if not word == None:
GuessWord (driver, word)
attempts += 1
yellows, greens = analyzer.RunAttempt(attempts==1, checkWin=word==None)
if yellows == None or greens == None:
todate = datetime.datetime.now().date().isoformat()
driver.save_screenshot(f'./images/{todate}.png')
yellows, greens = analyzer.RunAttempt(attempts==1, checkWin=word==None)
if type (yellows) != list or type(greens) != list:
gameWon = yellows
if attempts >= 6 or len(greens) >= 5 or not gameWon == None:
analyzer.Cleanup()
state = gameWon if not gameWon == None else len(greens) >= 5
print (f'\nExit. Attempts: {attempts-1} || Solved: {state}')
break
yellowLetters = [word[i] for i in yellows]
greenLetters = [word[i] for i in greens]
if len(yellows) > 0:
for yellow in yellows:
attempted.append(word[yellow])
knownLetters.append( (word[yellow], yellow) )
if len(greens) > 0:
for green in greens:
attempted.append(word[green])
lettersWithPlaces.append( (word[green], green) )
for i, char in enumerate(word):
if not char in yellowLetters and not char in greenLetters and not char in attempted:
attempted.append(word[i])
ignoreLetters.append(word[i])
print (f'\nWord: {word}')
print (f'Yellows: {yellows}')
print (f'Greens: {greens}')
print (f'ignore: {ignoreLetters}')
time.sleep(3)