Passed
Pull Request — master (#103)
by Chenyi
04:00
created

POJ::crawler()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 45
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 39
nc 5
nop 1
dl 0
loc 45
rs 8.9848
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\VirtualCrawler\HDU;
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
13
class POJ extends CrawlerBase 
14
{
15
    public $oid=6;
16
    private $con;
17
    private $imgi;
18
    /**
19
     * Initial
20
     *
21
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\VirtualCrawler\HDU\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
22
     */
23
    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

23
    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...
24
    {
25
        set_time_limit(0); // Pandora's box, engage!
26
        if ($action=='judge_level') {
27
            $this->judge_level();
28
        } else {
29
            $this->ContestHunter($con);
0 ignored issues
show
Bug introduced by
The method ContestHunter() does not exist on App\Http\Controllers\VirtualCrawler\HDU\POJ. ( Ignorable by Annotation )

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

29
            $this->/** @scrutinizer ignore-call */ 
30
                   ContestHunter($con);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
        }
31
    }
32
33
    public function judge_level()
34
    {
35
        // TODO
36
    }
37
38
    private static function find($pattern, $subject)
39
    {
40
        if (preg_match($pattern, $subject, $matches)) {
41
            return $matches[1];
42
        }
43
        return null;
44
    }
45
46
    private function cacheImage($dom)
0 ignored issues
show
Unused Code introduced by
The method cacheImage() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
47
    {
48
        foreach ($dom->find('img') as $ele) {
49
            $src=str_replace('../../..', '', $ele->src);
50
            if (strpos($src, '://')!==false) {
51
                $url=$src;
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
52
            } elseif ($src[0]=='/') {
53
                $url='http://acm.hdu.edu.cn'.$src;
54
            } else {
55
                $url='http://acm.hdu.edu.cn/'.$src;
56
            }
57
            $res=Requests::get($src, ['Referer' => 'http://acm.hdu.edu.cn']);
58
            $ext=['image/jpeg'=>'.jpg', 'image/png'=>'.png', 'image/gif'=>'.gif', 'image/bmp'=>'.bmp'];
59
            if (isset($res->headers['content-type'])) {
60
                $cext=$ext[$res->headers['content-type']];
61
            } else {
62
                $pos=strpos($ele->src, '.');
63
                if ($pos===false) {
64
                    $cext='';
65
                } else {
66
                    $cext=substr($ele->src, $pos);
67
                }
68
            }
69
            $fn=$this->con.'_'.($this->imgi++).$cext;
70
            $dir=base_path("public/external/hdu/img");
71
            if (!file_exists($dir)) {
72
                mkdir($dir, 0755, true);
73
            }
74
            file_put_contents(base_path("public/external/hdu/img/$fn"), $res->body);
75
            $ele->src='/external/hdu/img/'.$fn;
76
        }
77
        return $dom;
78
    }
79
80
    public function crawler($con) 
81
    {
82
        if($con == "all") {
83
            return ;
84
        }
85
        $this->con = $con;
86
        $this->imgi = 1;
87
        $problemModel = new ProblemModel();
88
        $res = Request::get("http://acm.hdu.edu.cn/showproblem.php?pid={$con}");
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\VirtualCrawler\HDU\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
89
        if (strpos("No such problem",$res->body) !== false) {
90
            header('HTTP/1.1 404 Not Found');
91
            die();
92
        } 
93
        else if(strpos("Invalid Parameter.",$res->body) !== false) {
94
            header('HTTP/1.1 404 Not Found');
95
            die();
96
        }
97
        else {
98
            $this->pro['pcode'] = "HDU".$con;
99
            $this->pro['OJ'] = 1;
100
            $this->pro['contest_id'] = null;
101
            $this->pro['index_id'] = $con;
102
            $this->pro['origin'] = "http://acm.hdu.edu.cn/showproblem.php?pid={$con}";
103
            $this->pro['title'] = find("/<h1 style='color:#1A5CC8'>([\s\S]*?)<\/h1>/",$res->body);
0 ignored issues
show
Bug introduced by
The function find was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

103
            $this->pro['title'] = /** @scrutinizer ignore-call */ find("/<h1 style='color:#1A5CC8'>([\s\S]*?)<\/h1>/",$res->body);
Loading history...
104
            $this->pro['time_limit'] = find('/Time Limit:.*\/(.*) MS/',$res->body);
105
            $this->pro['memory_limit'] = find('/Memory Limit:.*\/(.*) K/',$res->body);
106
            $this->pro['solved_count'] = find("/Accepted Submission(s): ([\d+]*?)/",$res->body);
107
            $this->pro['input_type']='standard input';
108
            $this->pro['output_type']='standard output';
109
            $this->pro['description'] = cacheImage(find("/this->problem Description.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body));
0 ignored issues
show
Bug introduced by
The function cacheImage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

109
            $this->pro['description'] = /** @scrutinizer ignore-call */ cacheImage(find("/this->problem Description.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body));
Loading history...
110
            $this->pro['input'] = find("/<div class=panel_title align=left>Input.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body);
111
            $this->pro['output'] = find("/<div class=panel_title align=left>Output.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body);
112
            $this->pro['sample'] = [];
113
            $this->pro['sample']['sample_input'] = find("/<pre><div.*>(.*)<\/div><\/pre>/sU",$res->body);
114
            $this->pro['sample']['sample_output'] = find("/<div.*>Sample Output<\/div><div.*><pre><div.*>(.*)<\/div><\/pre><\/div>/sU",$res->body);
115
            $this->pro['note'] = find("/<i>Hint<\/i><\/div>(.*)<\/div><i style='font-size:1px'>/sU",$res->body);
116
            $this->pro['source'] = find("/<div class=panel_title align=left>Source<\/div> (.*)<div class=panel_bottom>/sU",$res->body);
117
            $this->pro['force_raw'] = 0;
118
            $problem=$problemModel->pid($this->pro['pcode']);
119
120
            if ($problem) {
121
                $problemModel->clearTags($problem);
122
                $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...
123
            } else {
124
                $new_pid=$this->insert_problem($this->oid);
125
            }
126
127
            // $problemModel->addTags($new_pid, $tag); // not present
128
        }
129
    }
130
}