-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
59 lines (48 loc) · 1.38 KB
/
parser.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
import requests
import puz
import os.path
from clue import Clue
from cell import Cell
from state import State
# date format = yy.mm.dd
def save_puzzle(date):
filename = date + ".puz"
url = "http://www.jacobshufro.com/xwords2/puzs/" + filename
response = requests.get(url)
with open("puzs/" + filename, 'wb') as f:
f.write(response.content)
def create_clues(date):
clues = []
add_clues_across(clues, date)
add_clues_down(clues, date)
return clues
def add_clues_across(clues, date):
path_name = "puzs/" + date + ".puz"
if not os.path.isfile(path_name):
save_puzzle(date)
p = puz.read(path_name)
numbering = p.clue_numbering()
for clue in numbering.across:
answer = ''.join(
p.solution[clue['cell'] + i]
for i in range(clue['len']))
clues.append(Clue(clue['num'], Clue.ACROSS, clue['clue'], answer))
return clues
def add_clues_down(clues, date):
path_name = "puzs/" + date + ".puz"
if not os.path.isfile(path_name):
save_puzzle(date)
p = puz.read(path_name)
numbering = p.clue_numbering()
for clue in numbering.down:
answer = ''.join(
p.solution[clue['cell'] + i]
for i in range(clue['len']))
clues.append(Clue(clue['num'], Clue.DOWN, clue['clue'], answer))
return clues
def create_puzzle(date):
path_name = "puzs/" + date + ".puz"
if not os.path.isfile(path_name):
save_puzzle(date)
p = puz.read(path_name)
return p