-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwocabee.py
367 lines (303 loc) · 13.1 KB
/
wocabee.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import json
import time
import traceback
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
class wocabee:
def __init__(self, udaje: tuple):
self.url = "https://wocabee.app/app"
self.dict_path = "./dict.json"
# Create dictionary file if it doesn't exist
if not os.path.exists(self.dict_path):
with open(self.dict_path, "w") as f:
f.write("{}")
self.word_dictionary = {}
# Status indicators
self.ok = "[+]"
self.warn = "[!]"
self.err = "[-]"
self.info = "[#]"
self.debug = "[D]"
# Exercise types
self.PRACTICE = 0
self.DOPACKAGE = 1
self.LEARN = 2
self.LEARNALL = 3
self.GETPACKAGE = 4
self.udaje = udaje
# Initialize headless Firefox driver with performance options
options = Options()
options.headless = True
options.add_argument("--headless")
options.set_preference("browser.cache.disk.enable", False)
options.set_preference("browser.cache.memory.enable", False)
options.set_preference("browser.cache.offline.enable", False)
options.set_preference("network.http.use-cache", False)
self.driver = webdriver.Firefox(options=options)
self.driver.set_page_load_timeout(10)
self.driver.implicitly_wait(1) # Reduce implicit wait time
self.driver.get(self.url)
# Initialize thread pool for parallel operations
self.executor = ThreadPoolExecutor(max_workers=4)
def init(self):
"""Initialize session by logging in and loading class data"""
self.word_dictionary = self._dictionary_Load()
self.class_names = []
username, password = self.udaje
print(f"{self.ok} Logging in... {username}")
# Retry login up to 3 times (reduced from 5)
attempts = 0
logged_in = False
while not logged_in and attempts < 3:
try:
self.login(username, password)
if self.is_loggedIn():
logged_in = True
else:
attempts += 1
except:
attempts += 1
time.sleep(0.5)
if not logged_in:
print("Login failed")
self.driver.quit()
return
# Get user name and class names in parallel
self.name = self.get_elements_text(By.TAG_NAME, "b")[0]
classes = self.get_classes()
def get_class_name(class_item):
id = list(class_item.keys())[0]
return id, class_item[id].find_element(By.TAG_NAME, "span").text
class_futures = [self.executor.submit(get_class_name, Class) for Class in classes]
class_results = [f.result() for f in class_futures]
self.class_names = [name for _, name in sorted(class_results)]
print(f"Initialization complete. Classes: {self.class_names}")
def quit(self):
"""Close browser and end session"""
self.executor.shutdown()
self.driver.quit()
def elem_type(self, by, elem, x):
"""Type text into an element"""
elem = self.get_element(by, elem)
if elem:
elem.clear()
elem.send_keys(x)
def login(self, username, password):
"""Log into Wocabee"""
self.elem_type(By.ID, "login", username)
self.elem_type(By.ID, "password", password)
self.get_element(By.ID, "submitBtn").click()
def is_loggedIn(self):
"""Check if logged in by looking for logout button"""
try:
return self.wait_for_element(1, By.ID, "logoutBtn") # Reduced timeout
except:
return False
# Optimized element utilities
def exists_element(self, root, by, element):
"""Check if element exists and is displayed"""
try:
return root.find_element(by, element).is_displayed()
except:
return False
def get_element(self, by, element):
"""Get single element if it exists"""
try:
return self.driver.find_element(by, element) if self.exists_element(self.driver, by, element) else None
except:
return None
def get_elements(self, by, element):
"""Get multiple elements if they exist"""
try:
return self.driver.find_elements(by, element) if self.exists_element(self.driver, by, element) else None
except:
return None
def get_element_text(self, by, element):
"""Get text of single element"""
elem = self.get_element(by, element)
return elem.text if elem else 0
def get_elements_text(self, by, element):
"""Get text of multiple elements"""
elems = self.get_elements(by, element)
return [x.text for x in elems] if elems else [0]
def wait_for_element(self, timeout, by, element):
"""Wait for element to be displayed"""
WebDriverWait(self.driver, timeout).until(
lambda x: self.driver.find_element(by, element).is_displayed()
)
return self.get_element(by, element)
# Optimized class management
def get_classes(self) -> list:
"""Get list of available classes"""
classes = self.wait_for_element(5, By.ID, "listOfClasses")
return [{i: btn} for i, btn in enumerate(classes.find_elements(By.CLASS_NAME, "btn-wocagrey"))]
def pick_class(self, class_id, classes):
"""Select a class to work with"""
try:
class_id = int(class_id)
classes[class_id][class_id].click()
self.wocaclass = class_id
except Exception as e:
print(f"Error selecting class: {e}")
time.sleep(0.5) # Reduced wait time
# Optimized leaderboard
def get_leaderboard(self):
"""Get class leaderboard data"""
table_body = self.get_element(By.ID, "tbody")
if not table_body:
return []
students = table_body.find_elements(By.CLASS_NAME, "wb-tr")
def process_student(student):
try:
return {
"place": student.find_element(By.CLASS_NAME, "place").text,
"name": student.find_element(By.CLASS_NAME, "name").text,
"online": "status-online" in student.find_element(By.CLASS_NAME, "status-icon").get_attribute("class"),
"points": student.find_elements(By.TAG_NAME, "td")[2].text,
"packages": student.find_elements(By.TAG_NAME, "td")[3].text
}
except:
return None
# Process students in parallel
futures = [self.executor.submit(process_student, student) for student in students]
return [f.result() for f in futures if f.result()]
# Optimized package management
def get_packages(self, prac):
"""Get list of available packages based on practice type"""
prac = int(prac)
packages = []
elements = self.get_elements(By.CLASS_NAME, "pTableRow")
if not elements:
return packages
if prac == self.GETPACKAGE:
def process_package(elem):
try:
name = elem.find_element(By.CLASS_NAME, "package-name").text
playable = self.exists_element(elem, By.CLASS_NAME, "fa-play-circle")
return {name: playable}
except:
return None
futures = [self.executor.submit(process_package, elem) for elem in elements]
packages = [f.result() for f in futures if f.result()]
elif prac == self.PRACTICE:
elements = elements[:10] # Only first 10 for practice
for i, elem in enumerate(elements):
if self.exists_element(elem, By.CLASS_NAME, "fa-gamepad"):
try:
button = elem.find_element(By.CLASS_NAME, "btn-primary")
packages.append({i: button})
except:
continue
elif prac == self.DOPACKAGE:
for i, elem in enumerate(elements):
if self.exists_element(elem, By.CLASS_NAME, "fa-play-circle"):
try:
button = elem.find_element(By.CLASS_NAME, "package").find_element(By.TAG_NAME, "a")
packages.append({len(packages): button})
except:
continue
elif prac in (self.LEARN, self.LEARNALL):
for i, elem in enumerate(elements):
if self.exists_element(elem, By.TAG_NAME, "a"):
try:
button = elem.find_element(By.TAG_NAME, "a")
packages.append({i: button})
except:
continue
return packages
def pick_package(self, package_id, packages):
"""Select a package to work with"""
try:
package_id = int(package_id)
packages[package_id][package_id].click()
self.package = package_id
except Exception as e:
print(f"Error selecting package: {e}")
time.sleep(0.5)
# Rest of the methods remain largely unchanged as they are already optimized
# or require sequential execution due to website interaction requirements
# Dictionary operations optimized with caching
_dictionary_cache = {}
def dictionary_get(self, word, *args, **kwargs):
"""Get translations for a word from dictionary with caching"""
word = str(word)
cache_key = f"{word}_{kwargs.get('Picture', False)}"
if cache_key in self._dictionary_cache:
return self._dictionary_cache[cache_key]
if not self.word_dictionary:
self.word_dictionary = self._dictionary_Load()
if "Picture" in kwargs:
dictionary = self.word_dictionary.get("Picture", {})
else:
dictionary = self.word_dictionary.get(self.class_names[int(self.wocaclass)], {})
words = []
translations = []
if "," in word:
words.extend(x.strip() for x in word.split(",") if len(x.strip()) > 2)
words.append(word)
else:
words.append(word)
for word in words:
for dict_word, dict_translations in dictionary.items():
if word == dict_word:
translations.extend(x for x in dict_translations if x not in translations)
elif word in dict_translations:
if dict_word not in translations:
translations.append(dict_word)
if translations and isinstance(translations[0], list):
result = translations[0]
else:
result = translations
self._dictionary_cache[cache_key] = result
return result
def dictionary_put(self, word, translation, *args, **kwargs):
"""Add word and translation to dictionary with cache invalidation"""
if not word or not translation:
return
self.wocaclass = int(self.wocaclass)
if "Picture" in kwargs:
dict_key = "Picture"
else:
dict_key = self.class_names[self.wocaclass]
if dict_key not in self.word_dictionary:
self.word_dictionary[dict_key] = {}
dictionary = self.word_dictionary[dict_key]
word = str(word)
translation = str(translation)
if "," in translation:
translations = []
translations.extend(x.strip() for x in translation.split(","))
translations.append(translation)
value = translations
key = word
else:
value = [translation]
key = word
if key not in dictionary:
dictionary[key] = value
else:
dictionary[key].extend(x for x in value if x not in dictionary[key])
# Invalidate cache for this word
cache_key = f"{word}_{kwargs.get('Picture', False)}"
if cache_key in self._dictionary_cache:
del self._dictionary_cache[cache_key]
self._dictionary_Save()
def _dictionary_Load(self):
"""Load dictionary from file"""
try:
with open(self.dict_path, "r") as f:
self.word_dictionary = json.load(f)
return self.word_dictionary
except:
return {}
def _dictionary_Save(self):
"""Save dictionary to file"""
with open(self.dict_path, "w") as f:
json.dump(self.word_dictionary, f, indent=2)