-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
85 lines (74 loc) · 2.58 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
# Imports
import time
from selenium import webdriver
import keyboard
class MonkeyTyper:
def __init__(self) -> None:
# Disable all notifications from selenium
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
# Set up webdriver
self.driver = webdriver.Chrome(options=options)
# Open monkeytye
self.driver.get("https://monkeytype.com/")
self._switch_to_words()
# This is the function that switches to the words tab
def _switch_to_words(self):
"""
Switches to words tab
Basically just clicks buttons and refreshes the page
"""
f = self.driver.find_elements(by="class name", value="text-button")
time.sleep(2)
f[3].click()
time.sleep(2)
f = self.driver.find_elements(by="class name", value="text-button")
for element in f:
if element.get_attribute("wordcount") == "10":
element.click()
break
# Reloads the page
self.driver.refresh()
# Gets the sentence using the page's HTML
def _get_sentence(self):
"""
Gets the sentence
All sentences in monkeytype are in `word`
In every word, there are <letter> tags
This function collects everything into one sentence and returns it
"""
time.sleep(2)
# Clicks on wordsWrapper
wordsWrapper = self.driver.find_element(by="id", value="wordsWrapper")
try:
wordsWrapper.click()
except:
pass
# gets all the words and make a sentence
words = self.driver.find_elements(by="class name", value="word")
sentence = ""
for word in words:
# All letters are in <letter> tags
letters = word.find_elements(by="tag name", value="letter")
for letter in letters:
if not "correct" in letter.get_attribute("class"):
# If the letter is not correct, adds it to the sentence
sentence += letter.text
sentence += " "
return sentence
# Starts the typing
def start(self):
"""
Gets the sentence and starts typing
"""
sentence = self._get_sentence()
keyboard.write(sentence, delay=0.04)
keyboard.press_and_release("enter")
keyboard.press_and_release("tab")
if __name__ == "__main__":
typer = MonkeyTyper()
time.sleep(10) # Sleeping to allow the user to do stuff before starting
i = 0
while True:
typer.start()
i += 1