65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
from urllib.request import urlopen
|
|
import re
|
|
from enum import Enum
|
|
from dataclasses import dataclass
|
|
from typing import List
|
|
|
|
@dataclass
|
|
class Sudoku:
|
|
solution: List[int]
|
|
editmask: List[int]
|
|
|
|
class Difficulty(Enum):
|
|
EASY = 1
|
|
MEDIUM = 2
|
|
HARD = 3
|
|
EVIL = 4
|
|
|
|
host = "five.websudoku.com"
|
|
|
|
async def get_sudoku_puzzle_test():
|
|
cheat = [ 1,7,6,5,9,4,3,2,8,
|
|
3,5,8,6,7,2,9,1,4,
|
|
2,9,4,8,1,3,6,5,7,
|
|
7,3,1,9,2,6,8,4,5,
|
|
6,4,5,1,3,8,7,9,2,
|
|
9,8,2,4,5,7,1,3,6,
|
|
5,6,3,2,8,9,4,7,1,
|
|
8,2,9,7,4,1,5,6,3,
|
|
4,1,7,3,6,5,2,8,9 ]
|
|
editmask = [ 1,0,1,0,0,1,0,1,1,
|
|
1,0,1,1,0,1,1,0,1,
|
|
0,1,1,1,1,1,0,1,0,
|
|
1,1,1,1,1,0,1,1,1,
|
|
0,1,1,0,1,0,1,1,0,
|
|
1,1,1,0,1,1,1,1,1,
|
|
0,1,0,1,1,1,1,1,0,
|
|
1,0,1,1,0,1,1,0,1,
|
|
1,1,0,1,0,0,1,0,1 ]
|
|
|
|
return Sudoku(cheat, editmask)
|
|
|
|
def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku:
|
|
"""Makes a web request to websudoku.com to grab a sudoku puzzle
|
|
|
|
Args:
|
|
difficulty: Choose between easy, medium, hard, or evil
|
|
|
|
Returns:
|
|
A Sudoku object which contains the cheat and the editmask
|
|
"""
|
|
d = str(difficulty.value)
|
|
response = urlopen("https://six.websudoku.com/?level="+d, timeout=5)
|
|
|
|
if response.status == 200:
|
|
html_content = response.read().decode('utf-8')
|
|
for line in html_content.split('\n'):
|
|
if "editmask" in line:
|
|
editmask = [ int(n) for n in re.findall(r'\d', line) ]
|
|
if "var cheat" in line:
|
|
cheat = [ int(n) for n in re.findall(r'\d', line) ]
|
|
return Sudoku(cheat, editmask)
|
|
else:
|
|
print("Error getting sudoku puzzle:", response.status, response.reason)
|
|
return None
|