-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduolingo.py
334 lines (317 loc) · 13.5 KB
/
duolingo.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import json
import os
import signal
import time
from selenium.common.exceptions import ElementNotInteractableException, NoSuchElementException, StaleElementReferenceException, ElementClickInterceptedException
from selenium.webdriver import DesiredCapabilities, Firefox, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
loop_count = 0
player_locator = (By.XPATH, "//button[contains(@data-test,'player-')]")
player_skip_locator = (By.CSS_SELECTOR, "button[data-test='player-skip']")
continue_locator = (By.XPATH, "//span[text() = 'Continue']")
check_locator = (By.XPATH, "//span[text() = 'Check']")
player_practice_again_locator = (By.CSS_SELECTOR, "button[data-test='player-practice-again']")
translations_filename = "translations.json"
start = time.time()
translations = json.load(open(translations_filename, "r"))
end = time.time()
print("Loaded {} translations from disk in {} seconds.".format(len(translations), end - start))
# setup
profile_path = '/Users/alaileon/Library/Application Support/Firefox/Profiles/ld5n6nou.default-esr'
profile = FirefoxProfile(profile_path)
profile.set_preference("dom.webdriver.enabled", False)
profile.set_preference('useAutomationExtension', False)
profile.set_preference('detach', True)
profile.update_preferences()
desired = DesiredCapabilities.FIREFOX
driver = Firefox(firefox_profile=profile, desired_capabilities=desired, service_log_path=os.devnull)
driver.set_page_load_timeout(30)
# rate of challenge solving
main_throttle = 6
def keyboardInterruptHandler(sig, frame):
start = time.time()
json.dump(translations, open(translations_filename, "w"))
end = time.time()
print("Wrote {} translations to disk in {} seconds.".format(len(translations), end - start))
exit(0)
signal.signal(signal.SIGINT, keyboardInterruptHandler)
def input_translate():
global loop_count
print("parsing hint tokens")
original_tokens = driver.find_elements(By.CSS_SELECTOR, "[data-test='hint-token']")
try:
original_sentence = ' '.join(elem.text for elem in original_tokens)
except StaleElementReferenceException:
return
print("looking for " + original_sentence)
if original_sentence in translations:
print("found " + original_sentence)
try:
driver.find_element(By.CSS_SELECTOR, "textarea[data-test='challenge-translate-input']").send_keys(translations[original_sentence], Keys.RETURN)
except ElementNotInteractableException:
print("can't type into the input box, probably waiting for the end screen")
loop_count = loop_count + 1
driver.implicitly_wait(10)
driver.find_element(*player_practice_again_locator)
return
else:
loop_count = 0
try:
driver.find_element(*player_skip_locator).click()
translated_sentence=driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution')]/following-sibling::div").text
translations[original_sentence] = translated_sentence
print("New translation: " + original_sentence + " => " + translated_sentence)
except NoSuchElementException:
pass
print("clicking continue after input challenge")
driver.find_element(*continue_locator).click()
def assist_translate():
original_question = driver.find_element(By.CSS_SELECTOR, "h1[data-test='challenge-header'] > span").text
print("original question: " + original_question)
original_word = original_question.split('"')[1]
print("original word: " + original_word)
print("looking for " + original_word)
if original_word in translations:
translation = translations[original_word]
if isinstance(translation, str):
print("found translation for " + original_word + ": " + translation)
try:
driver.find_element(By.XPATH, "//div[text() = '"+translation+"']").click()
driver.find_element(*check_locator).click()
except NoSuchElementException as e:
driver.find_element(*player_skip_locator).click()
translated_word=driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution:')]/following-sibling::div").text
translations[original_word] = [translation, translated_word]
else:
print("found translations for " + original_word + ": " + str(translation))
for i in translation:
try:
driver.find_element(By.XPATH, "//div[text() = '"+i+"']").click()
driver.find_element(*check_locator).click()
except NoSuchElementException as e:
print("better luck next time" + i)
else:
print("did not find translation for " + original_word + ", picking first")
random_pick = driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge-judge-text']")
random_pick_text = random_pick.text.strip()
random_pick.click()
driver.find_element(*check_locator).click()
try:
print("looking for blame-correct")
driver.find_element(By.XPATH, "//div[contains(@data-test,'blame-correct')]")
print("found blame-correct")
translations[original_word] = random_pick_text
print("New translation: " + original_word + " => " + random_pick_text)
driver.implicitly_wait(10)
except NoSuchElementException as e:
driver.implicitly_wait(10)
try:
translated_word=driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution:')]/following-sibling::div").text
translations[original_word] = translated_word
print("New translation: " + original_word + " => " + translated_word)
except NoSuchElementException:
pass
driver.find_element(*continue_locator).click()
def judge_translate():
original_question = driver.find_element(By.CSS_SELECTOR, "h1[data-test='challenge-header'] > span").text
print("original question: " + original_question)
original_word = original_question.split('"')[1]
print("original word: " + original_word)
print("looking for " + original_word)
if original_word in translations:
translation = translations[original_word]
if isinstance(translation, str):
print("found translation for " + original_word + ": " + translation)
try:
driver.find_element(By.XPATH, "//div[text() = '"+translation+"']").click()
driver.find_element(*check_locator).click()
except NoSuchElementException as e:
driver.find_element(*player_skip_locator).click()
translated_word=driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution:')]/following-sibling::div").text
translations[original_word] = [translation, translated_word]
else:
print("found translations for " + original_word + ": " + str(translation))
for i in translation:
try:
driver.find_element(By.XPATH, "//div[text() = '"+i+"']").click()
driver.find_element(*check_locator).click()
except NoSuchElementException as e:
print("better luck next time" + i)
else:
print("did not find translation for " + original_word + ", picking first")
random_pick = driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge-judge-text']")
random_pick_text = random_pick.text.strip()
random_pick.click()
driver.find_element(*check_locator).click()
try:
print("looking for blame-correct")
driver.find_element(By.XPATH, "//div[contains(@data-test,'blame-correct')]")
print("found blame-correct")
translations[original_word] = random_pick_text
print("New translation: " + original_word + " => " + random_pick_text)
driver.implicitly_wait(10)
except NoSuchElementException as e:
driver.implicitly_wait(10)
try:
translated_word=driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution:')]/following-sibling::div").text
translations[original_word] = translated_word
print("New translation: " + original_word + " => " + translated_word)
except NoSuchElementException:
pass
driver.find_element(*continue_locator).click()
def name_translate():
original_question = driver.find_element(By.CSS_SELECTOR, "h1[data-test='challenge-header'] > span").text
print("original question: " + original_question)
original_question = original_question.replace('“', '"')
original_question = original_question.replace('”', '"')
original_phrase = original_question.split('"')[1]
print("original phrase: " + original_phrase)
if original_phrase in translations:
translation = translations[original_phrase]
print("found answer for " + original_phrase + ": " + str(translation))
if isinstance(translation, str):
print("finding input box")
driver.find_element(By.CSS_SELECTOR, "input[data-test='challenge-text-input']").send_keys(translation, Keys.RETURN)
print("found input box")
else:
try:
driver.find_element(By.XPATH, "//div[text() = '"+translation[0]+"']").click()
driver.find_element(By.CSS_SELECTOR, "input[data-test='challenge-text-input']").send_keys(translation[1], Keys.RETURN)
except NoSuchElementException:
driver.find_element(By.CSS_SELECTOR, "input[data-test='challenge-text-input']").send_keys(" ".join(translation), Keys.RETURN)
driver.find_element(*check_locator).click()
else:
print("did not find answer for " + original_phrase + ", skipping")
driver.find_element(*player_skip_locator).click()
answers = driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution')]/following-sibling::div").text
answer = answers.split(",")[0]
if len(answer.split(" ")) > 1:
article = answer.split(" ")[0]
text = " ".join(answer.split(" ")[1:])
translations[original_phrase] = [article, text]
else:
translations[original_phrase] = answer
driver.find_element(*continue_locator).click()
def gapfill_translate():
print("parsing hint tokens")
original_tokens = driver.find_elements(By.CSS_SELECTOR, "[data-test='hint-token']")
original_sentence = ' '.join(elem.text for elem in original_tokens)
print("looking for " + original_sentence)
if original_sentence in translations:
translation = translations[original_sentence]
print("found answer for " + original_sentence + ": " + translation)
driver.find_element(By.XPATH, "//div[text() = '"+translation+"']").click()
driver.find_element(*check_locator).click()
else:
print("did not find answer for " + original_sentence + ", picking first")
random_pick = driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge-judge-text']")
random_pick_text = random_pick.text.strip()
random_pick.click()
driver.find_element(*check_locator).click()
try:
print("looking for blame-correct")
driver.find_element(By.XPATH, "//div[contains(@data-test,'blame-correct')]")
print("found blame-correct")
translations[original_sentence] = random_pick_text
print("New translation: " + original_sentence + " => " + random_pick_text)
driver.implicitly_wait(10)
except NoSuchElementException as e:
driver.implicitly_wait(10)
try:
translated_word=driver.find_element(By.XPATH, "//h2[contains(text(), 'Correct solution:')]/following-sibling::div").text
translations[original_sentence] = translated_word
print("New translation: " + original_sentence + " => " + translated_word)
except NoSuchElementException:
pass
driver.find_element(*continue_locator).click()
driver.maximize_window()
driver.get("https://www.duolingo.com/practice")
while True:
try:
if loop_count > 5:
# just nuke everything if we detect a loop, protecting against network issues or other strange problems
driver.get("https://www.duolingo.com/practice")
loop_count = 0
while True:
driver.implicitly_wait(10)
print("looking for any player-* button")
driver.find_element(*player_locator)
time.sleep(main_throttle)
driver.implicitly_wait(0)
try:
print("looking for player-practice-again")
driver.find_element(*player_practice_again_locator).click()
print("clicked player-practice-again")
continue
except NoSuchElementException as e:
pass
try:
print("looking for challenge type:")
print(driver.find_element(By.XPATH, "//div[contains(@data-test,'challenge')]").get_attribute("data-test"))
except NoSuchElementException as e:
print("did not find a challenge type:")
pass
try:
print("looking for challenge-translate")
driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge challenge-translate']")
print("found challenge-translate")
input_translate()
driver.implicitly_wait(10)
break
except NoSuchElementException as e:
pass
try:
print("looking for challenge-assist")
driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge challenge-assist']")
print("found challenge-assist")
assist_translate()
driver.implicitly_wait(10)
break
except NoSuchElementException as e:
pass
try:
print("looking for challenge-name")
driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge challenge-name']")
print("found challenge-name")
name_translate()
driver.implicitly_wait(10)
break
except NoSuchElementException as e:
pass
try:
print("looking for challenge-gapFill")
driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge challenge-gapFill']")
print("found challenge-gapFill")
gapfill_translate()
driver.implicitly_wait(10)
break
except NoSuchElementException as e:
pass
try:
print("looking for challenge-judge")
driver.find_element(By.CSS_SELECTOR, "div[data-test='challenge challenge-judge']")
print("found challenge-judge")
judge_translate()
driver.implicitly_wait(10)
break
except NoSuchElementException as e:
pass
try:
print("clicking player-skip")
driver.find_element(*player_skip_locator).click()
except NoSuchElementException as e:
print("didn't find player-skip")
pass
print("clicking continue")
try:
driver.find_element(*continue_locator).click()
except ElementClickInterceptedException:
continue
except Exception as e:
# ignore literally everything else, get back on that rat wheel
print(e)
try:
driver.get("https://www.duolingo.com/practice")
except Exception as e:
print(e)