This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
guesser.py
113 lines (95 loc) · 3.55 KB
/
guesser.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
import argparse
from re import findall
from sys import exit
from time import sleep
from selenium import webdriver
def init_argparse() -> argparse.ArgumentParser:
"""Define and manage arguments passed to guesser via terminal."""
parser = argparse.ArgumentParser(
description='A simple selenium automation to win at https://skribbl.io game.')
required = parser.add_argument_group('required arguments')
required.add_argument('--lang', help='Select desired language (eng, ger)',
required=True)
return parser
args = init_argparse().parse_args()
if args.lang == 'eng':
lang = 'English'
elif args.lang == 'ger':
lang = 'German'
else:
print('Specified lang not found')
exit(0)
def build_my_regexp(word: str) -> str:
"""Build regexp based on displayed characters tips."""
built_regexp = r'\b'
for letter in word:
if letter == '_':
built_regexp += r'\w'
else:
built_regexp += letter
built_regexp += r'\b'
return built_regexp
def search_wordlist(current_regexp: str) -> set:
"""Return a set of lowercase matching words because the website is case-insensitive)."""
with open(f'{lang}_wordlist.txt', 'r') as in_file:
wordlist = set(map(str.lower, findall(current_regexp, in_file.read())))
return wordlist
def try_and_guess(driver, word: str) -> bool:
sleep(0.5)
guess_form = driver.find_element_by_id('inputChat')
guess_form.send_keys(word)
guess_form.submit()
sleep(0.5)
messages = driver.find_element_by_id('boxMessages')
messages_extractor = [element.get_attribute('innerHTML') for element in
messages.find_elements_by_tag_name('span')]
for _ in messages_extractor:
current_message = messages_extractor.pop()
if current_message == 'Inizio :D':
return False
elif 'guessed the word!' in current_message:
return True
def play_to_win(driver) -> str:
old_word = ''
attempted = set()
win = False
current_word = driver.find_element_by_id('currentWord').text
while win is False:
if '_' not in current_word:
print('I think that it\'s not your turn now!')
break
elif old_word != current_word:
old_word = current_word
sleep(2)
guess_form = driver.find_element_by_id('inputChat')
guess_form.send_keys('Inizio :D')
guess_form.submit()
my_regexp = build_my_regexp(current_word)
my_wordlist = {word for word in search_wordlist(my_regexp) if word not in attempted}
for word in my_wordlist:
win = try_and_guess(driver, word)
if win:
break
else:
current_word = driver.find_element_by_id('currentWord').text
attempted.add(word)
if current_word != old_word:
break
return old_word
if __name__ == '__main__':
my_driver = webdriver.Chrome()
my_driver.maximize_window()
my_driver.get('https://skribbl.io/')
input('Press enter when ready...')
print('Started!')
my_old_word = ''
while True:
try:
my_word = my_driver.find_element_by_id('currentWord').text
if '_' not in my_word:
print('I think that it\'s not your turn now!')
elif my_word != my_old_word:
my_old_word = play_to_win(my_driver)
except:
print('Exception caught but kept running...')
sleep(5)