1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Babel\Crawl; |
4
|
|
|
|
5
|
|
|
use App\Models\ProblemModel; |
6
|
|
|
use KubAT\PhpSimple\HtmlDomParser; |
7
|
|
|
use Auth; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
class CrawlerBase |
11
|
|
|
{ |
12
|
|
|
public $pro=[ |
13
|
|
|
'file'=> 0, |
14
|
|
|
'file_url'=> null, |
15
|
|
|
'pcode'=>'', |
16
|
|
|
'solved_count'=>'', |
17
|
|
|
'time_limit'=>'', |
18
|
|
|
'memory_limit'=>'', |
19
|
|
|
'title'=>'', |
20
|
|
|
'OJ'=>'', |
21
|
|
|
'description'=>'', |
22
|
|
|
'input'=>'', |
23
|
|
|
'output'=>'', |
24
|
|
|
'note'=>'', |
25
|
|
|
'input_type'=>'', |
26
|
|
|
'output_type'=>'', |
27
|
|
|
'contest_id'=>'', |
28
|
|
|
'index_id'=>'', |
29
|
|
|
'origin'=>'', |
30
|
|
|
'source'=>'', |
31
|
|
|
'sample'=>[], |
32
|
|
|
'markdown'=>0, |
33
|
|
|
'tot_score'=>1, |
34
|
|
|
'partial'=>0, |
35
|
|
|
'special_compiler'=>null, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public $data=null; |
39
|
|
|
public $command=null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initial |
43
|
|
|
* |
44
|
|
|
* @return Response |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function _resetPro() |
51
|
|
|
{ |
52
|
|
|
$this->pro=[ |
53
|
|
|
'file'=> 0, |
54
|
|
|
'pcode'=>'', |
55
|
|
|
'solved_count'=>'', |
56
|
|
|
'time_limit'=>'', |
57
|
|
|
'memory_limit'=>'', |
58
|
|
|
'title'=>'', |
59
|
|
|
'OJ'=>'', |
60
|
|
|
'description'=>'', |
61
|
|
|
'input'=>'', |
62
|
|
|
'output'=>'', |
63
|
|
|
'note'=>'', |
64
|
|
|
'input_type'=>'', |
65
|
|
|
'output_type'=>'', |
66
|
|
|
'contest_id'=>'', |
67
|
|
|
'index_id'=>'', |
68
|
|
|
'origin'=>'', |
69
|
|
|
'source'=>'', |
70
|
|
|
'sample'=>[], |
71
|
|
|
'markdown'=>0, |
72
|
|
|
'tot_score'=>1, |
73
|
|
|
'partial'=>0, |
74
|
|
|
'special_compiler'=>null, |
75
|
|
|
'order_index'=>null, |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function importCommandLine($commandTemp) |
80
|
|
|
{ |
81
|
|
|
$this->command=$commandTemp; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function cmp($a, $b) |
85
|
|
|
{ |
86
|
|
|
return ($a[1]>$b[1]) ?-1 : 1; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function _cacheImage($data) |
90
|
|
|
{ |
91
|
|
|
if (!isset($data["ori"]) || !isset($data["path"]) || !isset($data["baseurl"]) || !isset($data["space_deli"]) || !isset($data["cookie"])) { |
92
|
|
|
throw new Exception("data is not completely exist in cacheImage"); |
93
|
|
|
} |
94
|
|
|
$ori=$data["ori"]; |
95
|
|
|
$path=$data["path"]; |
96
|
|
|
$baseurl=$data["baseurl"]; |
97
|
|
|
$space_deli=$data["space_deli"]; |
98
|
|
|
$cookie=$data["cookie"]; |
99
|
|
|
|
100
|
|
|
$para["path"]=$path; |
|
|
|
|
101
|
|
|
$para["base"]=$baseurl; |
102
|
|
|
$para["trans"]=!$space_deli; |
103
|
|
|
$para["cookie"]=$cookie; |
104
|
|
|
|
105
|
|
|
if ($space_deli) { |
106
|
|
|
$reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\' >]*)[^>]*>/si"; |
107
|
|
|
} else { |
108
|
|
|
$reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\'>]*)[^>]*>/si"; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return preg_replace_callback($reg, function($matches) use ($para) { |
112
|
|
|
global $config; |
113
|
|
|
$url=trim($matches[1]); |
114
|
|
|
if (stripos($url, "http://")===false && stripos($url, "https://")===false) { |
115
|
|
|
if ($para["trans"]) { |
116
|
|
|
$url=str_replace(" ", "%20", $url); |
117
|
|
|
} |
118
|
|
|
$url=$para["base"].$url; |
119
|
|
|
} |
120
|
|
|
$name=basename($url); |
121
|
|
|
$name="images/".strtr($name, ":", "_"); |
122
|
|
|
$result=str_replace(trim($matches[1]), "online_Judges/spoj/images/".strtr(basename($url), ":", "_"), $matches[0]); |
123
|
|
|
$ch=curl_init(); |
124
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
125
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
126
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
127
|
|
|
if ($para["cookie"]!="") { |
128
|
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $para["cookie"]); |
129
|
|
|
} |
130
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
131
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
132
|
|
|
$content=curl_exec($ch); |
133
|
|
|
curl_close($ch); |
134
|
|
|
$fp=fopen($name, "wb"); |
135
|
|
|
fwrite($fp, $content); |
|
|
|
|
136
|
|
|
fclose($fp); |
137
|
|
|
return $result; |
138
|
|
|
}, $ori); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function procInfo($data) |
142
|
|
|
{ |
143
|
|
|
if (isset($data["path"])) { |
144
|
|
|
$path=$data["path"]; |
145
|
|
|
} else { |
146
|
|
|
throw new Exception("path is not exist in data"); |
147
|
|
|
} |
148
|
|
|
if (isset($data["baseurl"])) { |
149
|
|
|
$baseurl=$data["baseurl"]; |
150
|
|
|
} else { |
151
|
|
|
throw new Exception("baseurl is not exist in data"); |
152
|
|
|
} |
153
|
|
|
if (isset($data["space_deli"])) { |
154
|
|
|
$space_deli=$data["space_deli"]; |
155
|
|
|
} else { |
156
|
|
|
$space_deli=true; |
157
|
|
|
} |
158
|
|
|
if (isset($data["cookie"])) { |
159
|
|
|
$cookie=$data["cookie"]; |
160
|
|
|
} else { |
161
|
|
|
$cookie=""; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie); |
|
|
|
|
165
|
|
|
$this->pro["input"]=$this->_cacheImage($this->pro["input"], $path, $baseurl, $space_deli, $cookie); |
166
|
|
|
$this->pro["output"]=$this->_cacheImage($this->pro["output"], $path, $baseurl, $space_deli, $cookie); |
167
|
|
|
$this->pro["note"]=$this->_cacheImage($this->pro["note"], $path, $baseurl, $space_deli, $cookie); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getUrl($url) |
171
|
|
|
{ |
172
|
|
|
$ch=curl_init(); |
173
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
174
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
175
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
176
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
177
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
178
|
|
|
$content=curl_exec($ch); |
179
|
|
|
curl_close($ch); |
180
|
|
|
return $content; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function insertProblem($oid=2) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$problemModel=new ProblemModel(); |
186
|
|
|
return $problemModel->insertProblem($this->pro); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function updateProblem($oid=2) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$problemModel=new ProblemModel(); |
192
|
|
|
return $problemModel->updateProblem($this->pro); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function line($line) |
196
|
|
|
{ |
197
|
|
|
if (is_null($this->command)) { |
198
|
|
|
echo $line; |
199
|
|
|
} else { |
200
|
|
|
$this->command->line($line); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|