-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake_ini.php
More file actions
executable file
·103 lines (85 loc) · 2.27 KB
/
make_ini.php
File metadata and controls
executable file
·103 lines (85 loc) · 2.27 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
99
100
101
<?PHP
if (count($argv) < 3) {
echo $argv[0]." type number\n";
exit;
}
$type = $argv[1];
$cw = $argv[2];
function curl_request($url)
{
$defaults = array(
CURLOPT_POST => 0,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 60,
CURLOPT_CONNECTTIMEOUT => 60,
CURLOPT_FOLLOWLOCATION => true,
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
echo "ERROR";
}
curl_close($ch);
return $result;
}
function make_ini($arr, $name, $url) {
$tmp = <<<EOF
[meta]
url="{$url}"
EOF;
foreach ($arr as $clue => $data) {
$tmp .= '['.$clue."]\n";
foreach($data as $key => $var) {
$tmp .= $key.'='.$var."\n";
}
$tmp .= "\n";
}
file_put_contents($name, $tmp);
}
$url = "http://www.guardian.co.uk/crosswords/{$type}/{$cw}";
if (file_exists($cw)) {
$file = file_get_contents($cw);
} else {
$file = curl_request($url);
$ret = file_put_contents("./".$cw, $file);
}
//get DIVs
if (!preg_match_all("/div id=\"([0-9]+-(across|down))\".*style=\"(.*?)\".*>/", $file, $matches)) {
echo "Something's wrong, can't get the clues\n";
}
define("SQUARE_SIZE", 29);
$clues = array();
foreach($matches[1] as $key => $clue) {
preg_match_all("/<input id=\"{$clue}-[0-9]+\"/", $file, $letters);
preg_match("/<label id=\"{$clue}-clue\".*?<\/span>\s+(.*?)<\/label>/ms", $file, $cluetext);
preg_match("/left: ([0-9]+)px; top: ([0-9]+)px;/", $matches[3][$key], $coords);
preg_match_all("/solutions\[\"{$clue}-[0-9]+\"] = \"(.)\"/ms", $file, $solutions);
$sol = "";
foreach($solutions[1] as $l) {
$sol .= $l;
}
$extra = "";
if (preg_match_all("/words_for_clue\[\"{$clue}\"\] = \['{$clue}',(.*)\];/", $file, $words)) {
$extra = str_replace("'","", $words[1][0]);
}
$clues[$clue] = array
(
'x' => $coords[1]/SQUARE_SIZE,
'y' => $coords[2]/SQUARE_SIZE,
'length' => count($letters[0]),
'clue' => '"'.str_replace('"', '\"', $cluetext[1]).'"',
'solution' => '"'.$sol.'"',
);
if ($extra != "") {
$clues[$clue]['extra'] = $extra;
}
}
//print_r($clues);
make_ini($clues, $type.'-'.$cw.'.ini', $url);
unlink($cw);