-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrabulizer.py
More file actions
executable file
·98 lines (82 loc) · 3.39 KB
/
scrabulizer.py
File metadata and controls
executable file
·98 lines (82 loc) · 3.39 KB
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
#!/usr/bin/env python
import requests
import re
LETTER_SCORES = {
'A': 2, 'B': 5, 'C': 3, 'D': 3, 'E': 1, 'F': 5,
'G': 4, 'H': 4, 'I': 2, 'J': 10, 'K': 6, 'L': 3,
'M': 4, 'N': 2, 'O': 2, 'P': 4, 'Q': 10, 'R': 2,
'S': 2, 'T': 2, 'U': 4, 'V': 6, 'W': 6, 'X': 9,
'Y': 5, 'Z': 10
}
COLUMNS = "ABCDEFGH"
def format_move(move_tuple):
(word, x, y, direction) = move_tuple
x, y = int(x), int(y) + 1
if direction == '0':
direction = 'horizontal'
else:
direction = 'vertical'
return "{} ({}, {}) ({})".format(word, COLUMNS[x], y, direction)
def scrape_scrabulizer(board, rack, bonuses, dry_run=False):
if dry_run:
return []
print("Querying Scrabulizer...")
url = 'https://www.scrabulizer.com/solver/results'
headers = {
'origin': 'http://www.scrabulizer.com',
'accept-language': 'en-US,en;q=0.9',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'x-prototype-version': '1.7',
'x-requested-with': 'XMLHttpRequest',
'x-js-version': '3',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept': 'text/javascript, text/html, application/xml, text/xml, */*',
'referer': 'http://www.scrabulizer.com/',
'authority': 'www.scrabulizer.com',
'dnt': '1'
}
options = {
'dictionary': 4,
'opponent_count': 1,
'design': '',
'sort_by': 0,
'tc_': 0,
'ts_': 0,
'boardWidth': 8,
'boardHeight': 7,
'bingo1': 0,
'bingo2': 0,
'bingo3': 0,
'bingo4': 0,
'bingo5': 0,
'bingo6': 0,
'bingo7': 35,
'bingo8': 50,
'rackLength': 7
}
blank_board = {"s_{0}_{1}".format(x, y): "" for x in range(0, 16) for y in range(0, 16)}
blank_bonuses = {"b_{0}_{1}".format(x, y): "" for x in range(0, 16) for y in range(0, 16)}
counts = {"tc{}".format(key): 1 for (key, value) in LETTER_SCORES.items()}
scores = {"ts{}".format(key): value for (key, value) in LETTER_SCORES.items()}
# Merge all default dicts into one
base_board = { k: v for d in [options, blank_board, blank_bonuses, counts, scores] for k, v in d.items() }
letters = {"s_{0}_{1}".format(x, y): letter for ((x, y), letter) in board.items()}
bonuses = {"b_{0}_{1}".format(x, y): bonus for ((x, y), bonus) in bonuses.items()}
rack = {'rack': ''.join(rack)}
payload = {k: v for d in [base_board, letters, bonuses, rack] for k, v in d.items()}
req = requests.post(url, headers=headers, data=payload)
words = []
if req.status_code == 200:
pattern = re.compile("moves = ([^;]+);")
match = pattern.search(req.text)
if match:
words_string = match.groups()[0]
words_pattern = re.compile("\[\"(\w+)\",\s*\[(\d+),\s*(\d+)\],\s*(\d+),")
words = [format_move(move) for move in words_pattern.findall(words_string)]
else:
print("Unexpected Scrabulizer format. Couldn't find moves list.")
return []
else:
print("Unknown Scrabulizer response: {} {}".format(req.status_code, req.reason))
return []
return words