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