|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\VirtualCrawler\UVa; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\VirtualCrawler\CrawlerBase; |
|
6
|
|
|
use App\Models\ProblemModel; |
|
7
|
|
|
use KubAT\PhpSimple\HtmlDomParser; |
|
8
|
|
|
use Auth; |
|
9
|
|
|
use Requests; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
|
|
12
|
|
|
class UVa extends CrawlerBase |
|
13
|
|
|
{ |
|
14
|
|
|
public $oid=7; |
|
15
|
|
|
/** |
|
16
|
|
|
* Initial |
|
17
|
|
|
* |
|
18
|
|
|
* @return Response |
|
|
|
|
|
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($action='crawl_problem', $con='all', $cached=false) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
set_time_limit(0); // Pandora's box, engage! |
|
23
|
|
|
if ($action=='judge_level') { |
|
24
|
|
|
$this->judge_level(); |
|
25
|
|
|
} else { |
|
26
|
|
|
if ($con == 'all') { |
|
27
|
|
|
$this->crawler(1, true); |
|
28
|
|
|
$this->crawler(2, true); |
|
29
|
|
|
} else if ($con[0] == 'c') { |
|
30
|
|
|
$this->crawler(substr($con, 1), true); |
|
31
|
|
|
} else { |
|
32
|
|
|
$this->crawler($con, false); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function judge_level() |
|
38
|
|
|
{ |
|
39
|
|
|
// TODO |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function crawler($con, $isCategory, $solves = -1) |
|
44
|
|
|
{ |
|
45
|
|
|
$problemModel=new ProblemModel(); |
|
46
|
|
|
if ($isCategory) { |
|
47
|
|
|
$res=Requests::get("https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=$con"); |
|
48
|
|
|
$count = preg_match_all('/<td><a href="index\.php\?option=com_onlinejudge&Itemid=8&category=(\d+)(?:&page=show_problem&problem=(\d+)"[\S\s]*?<td align="right">[\S\s]*?<td align="right">(\d+)[\S\s]*?>([\d.]+)%<\/div>)?/i', $res->body, $matches); |
|
49
|
|
|
for ($i = 0; $i < $count; ++$i) { |
|
50
|
|
|
if ($matches[2][$i]) { |
|
51
|
|
|
$this->crawler($matches[2][$i], false, intval($matches[3][$i] * $matches[4][$i] / 100 + .5)); // solve count here is not accurate |
|
52
|
|
|
} else { |
|
53
|
|
|
$this->crawler($matches[1][$i], true); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} else { |
|
57
|
|
|
$res=Requests::get("https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=$con"); |
|
58
|
|
|
preg_match('/<h3>(\d+) - (.*?)<\/h3>\s*Time limit: ([\d.]+) seconds/', $res->body, $match); |
|
59
|
|
|
$this->pro['pcode']='UVA'.$match[1]; |
|
60
|
|
|
$this->pro['OJ']=$this->oid; |
|
61
|
|
|
$this->pro['contest_id']=null; |
|
62
|
|
|
$this->pro['index_id']=$con; |
|
63
|
|
|
$this->pro['origin']="https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=$con"; |
|
64
|
|
|
$this->pro['title']=$match[2]; |
|
65
|
|
|
$this->pro['time_limit']=intval($match[3] * 1000); |
|
66
|
|
|
$this->pro['memory_limit']=0; |
|
67
|
|
|
$this->pro['solved_count']=$solves; // Won't crawler specially for efficiency purpose |
|
68
|
|
|
$this->pro['input_type']='standard input'; |
|
69
|
|
|
$this->pro['output_type']='standard output'; |
|
70
|
|
|
$this->pro['description']="<a href=\"/external/gym/UVa$match[1].pdf\">[Attachment Link]</a>"; |
|
71
|
|
|
$this->pro['input']=''; |
|
72
|
|
|
$this->pro['output']=''; |
|
73
|
|
|
$this->pro['note']=''; |
|
74
|
|
|
$this->pro['sample']=[]; |
|
75
|
|
|
$this->pro['source']='Here'; |
|
76
|
|
|
$this->pro['file']=1; |
|
77
|
|
|
$pf = substr($match[1], 0, strlen($match[1]) - 2); |
|
78
|
|
|
$res=Requests::get("https://uva.onlinejudge.org/external/$pf/p$match[1].pdf"); |
|
79
|
|
|
file_put_contents(base_path("public/external/gym/UVa$match[1].pdf"), $res->body); |
|
80
|
|
|
|
|
81
|
|
|
$problem=$problemModel->pid($this->pro['pcode']); |
|
82
|
|
|
|
|
83
|
|
|
if ($problem) { |
|
84
|
|
|
$problemModel->clearTags($problem); |
|
85
|
|
|
$new_pid=$this->update_problem($this->oid); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
$new_pid=$this->insert_problem($this->oid); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// $problemModel->addTags($new_pid, $tag); // not present |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|