Passed
Push — master ( 8bcc81...ee584d )
by
unknown
03:48
created

UVa::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 3
dl 0
loc 13
rs 9.9
c 0
b 0
f 0
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
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\VirtualCrawler\UVa\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    public function __construct($action='crawl_problem', $con='all', $cached=false)
0 ignored issues
show
Unused Code introduced by
The parameter $cached is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

20
    public function __construct($action='crawl_problem', $con='all', /** @scrutinizer ignore-unused */ $cached=false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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&amp;Itemid=8&amp;category=(\d+)(?:&amp;page=show_problem&amp;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);
0 ignored issues
show
Unused Code introduced by
The assignment to $new_pid is dead and can be removed.
Loading history...
86
            } else {
87
                $new_pid=$this->insert_problem($this->oid);
88
            }
89
90
            // $problemModel->addTags($new_pid, $tag); // not present
91
        }
92
    }
93
}
94