Passed
Push — master ( 0a812d...61283c )
by John
04:22
created

POJ::cacheImage()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 19
nop 1
dl 0
loc 32
rs 8.5866
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 HDU extends CrawlerBase
14
{
15
    public $oid=8;
16
    private $con;
17
    private $imgi;
18
19
    /**
20
     * Initial
21
     *
22
     * @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...
23
     */
24
    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

24
    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...
25
    {
26
        set_time_limit(0); // Pandora's box, engage!
27
        if ($action=='judge_level') {
28
            $this->judge_level();
29
        } else {
30
            $this->crawler($con);
31
        }
32
    }
33
34
    public function judge_level()
35
    {
36
        // TODO
37
    }
38
39
    private static function find($pattern, $subject)
40
    {
41
        if (preg_match($pattern, $subject, $matches)) {
42
            return $matches[1];
43
        }
44
        return null;
45
    }
46
47
    private function cacheImage($dom)
48
    {
49
        foreach ($dom->find('img') as $ele) {
50
            $src=str_replace('../../..', '', $ele->src);
51
            if (strpos($src, '://')!==false) {
52
                $url=$src;
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
53
            } elseif ($src[0]=='/') {
54
                $url='http://acm.hdu.edu.cn'.$src;
55
            } else {
56
                $url='http://acm.hdu.edu.cn/'.$src;
57
            }
58
            $res=Requests::get($src, ['Referer' => 'http://acm.hdu.edu.cn']);
59
            $ext=['image/jpeg'=>'.jpg', 'image/png'=>'.png', 'image/gif'=>'.gif', 'image/bmp'=>'.bmp'];
60
            if (isset($res->headers['content-type'])) {
61
                $cext=$ext[$res->headers['content-type']];
62
            } else {
63
                $pos=strpos($ele->src, '.');
64
                if ($pos===false) {
65
                    $cext='';
66
                } else {
67
                    $cext=substr($ele->src, $pos);
68
                }
69
            }
70
            $fn=$this->con.'_'.($this->imgi++).$cext;
71
            $dir=base_path("public/external/hdu/img");
72
            if (!file_exists($dir)) {
73
                mkdir($dir, 0755, true);
74
            }
75
            file_put_contents(base_path("public/external/hdu/img/$fn"), $res->body);
76
            $ele->src='/external/hdu/img/'.$fn;
77
        }
78
        return $dom;
79
    }
80
81
    public function crawler($con)
82
    {
83
        if($con == "all") {
84
            return ;
85
        }
86
        $this->con = $con;
87
        $this->imgi = 1;
88
        $problemModel = new ProblemModel();
89
        $res = Requests::get("http://acm.hdu.edu.cn/showproblem.php?pid={$con}");
90
        if (strpos("No such problem",$res->body) !== false) {
91
            header('HTTP/1.1 404 Not Found');
92
            die();
93
        }
94
        else if(strpos("Invalid Parameter.",$res->body) !== false) {
95
            header('HTTP/1.1 404 Not Found');
96
            die();
97
        }
98
        else {
99
            $this->pro['pcode'] = "HDU".$con;
100
            $this->pro['OJ'] = 1;
101
            $this->pro['contest_id'] = null;
102
            $this->pro['index_id'] = $con;
103
            $this->pro['origin'] = "http://acm.hdu.edu.cn/showproblem.php?pid={$con}";
104
            $this->pro['title'] = self::find("/<h1 style='color:#1A5CC8'>([\s\S]*?)<\/h1>/",$res->body);
105
            $this->pro['time_limit'] = self::find('/Time Limit:.*\/(.*) MS/',$res->body);
106
            $this->pro['memory_limit'] = self::find('/Memory Limit:.*\/(.*) K/',$res->body);
107
            $this->pro['solved_count'] = self::find("/Accepted Submission(s): ([\d+]*?)/",$res->body);
108
            $this->pro['input_type']='standard input';
109
            $this->pro['output_type']='standard output';
110
            $this->pro['description'] = $this->cacheImage(self::find("/this->problem Description.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body));
111
            $this->pro['input'] = self::find("/<div class=panel_title align=left>Input.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body);
112
            $this->pro['output'] = self::find("/<div class=panel_title align=left>Output.*<div class=panel_content>(.*)<\/div><div class=panel_bottom>/sU",$res->body);
113
            $this->pro['sample'] = [];
114
            $this->pro['sample']['sample_input'] = self::find("/<pre><div.*>(.*)<\/div><\/pre>/sU",$res->body);
115
            $this->pro['sample']['sample_output'] = self::find("/<div.*>Sample Output<\/div><div.*><pre><div.*>(.*)<\/div><\/pre><\/div>/sU",$res->body);
116
            $this->pro['note'] = self::find("/<i>Hint<\/i><\/div>(.*)<\/div><i style='font-size:1px'>/sU",$res->body);
117
            $this->pro['source'] = self::find("/<div class=panel_title align=left>Source<\/div> (.*)<div class=panel_bottom>/sU",$res->body);
118
            $this->pro['force_raw'] = 0;
119
            $problem=$problemModel->pid($this->pro['pcode']);
120
121
            if ($problem) {
122
                $problemModel->clearTags($problem);
123
                $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...
124
            } else {
125
                $new_pid=$this->insert_problem($this->oid);
126
            }
127
128
            // $problemModel->addTags($new_pid, $tag); // not present
129
        }
130
    }
131
}
132