Passed
Pull Request — master (#125)
by Chenyi
22:46
created

SPOJ::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

21
    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...
22
    {
23
        set_time_limit(0); // Pandora's box, engage!
24
        if ($action=='judge_level') {
25
            $this->judge_level();
26
        } else if($con=="all"){
27
            $this->getSpojProblem();
28
        }else{
29
            //TODO
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)
47
    {
48
        foreach ($dom->find('img') as $ele) {
49
            $url='https://www.spoj.com'.$ele->src;
50
            $res=Requests::get($url, ['Referer' => 'https://www.spoj.com']);
51
            $ext=['image/jpeg'=>'.jpg', 'image/png'=>'.png', 'image/gif'=>'.gif', 'image/bmp'=>'.bmp'];
52
            if (isset($res->headers['content-type'])) {
53
                $cext=$ext[$res->headers['content-type']];
54
            } else {
55
                $pos=strpos($ele->src, '.');
56
                if ($pos===false) {
57
                    $cext='';
58
                } else {
59
                    $cext=substr($ele->src, $pos);
60
                }
61
            }
62
            $fn=$this->pro['index_id'].'_'.($this->imgi++).$cext;
63
            $dir=base_path("public/external/spoj/img");
64
            if (!file_exists($dir)) {
65
                mkdir($dir, 0755, true);
66
            }
67
            file_put_contents(base_path("public/external/spoj/img/$fn"), $res->body);
68
            $ele->src='/external/spoj/img/'.$fn;
69
        }
70
        return $dom;
71
    }
72
73
    public function getProblemDetail($pid) {
74
        $cate = [];
75
76
        $url = "https://www.spoj.com/problems/".$pid;
77
        $res = Requests::get($url);
78
        if (strpos("<h1>Not Found</h1>",$res->body) !== false) {
79
            header('HTTP/1.1 404 Not Found');
80
            die();
81
        }else{
82
            $this->pro['title'] = trim(self::find('/<h2 id="problem-name".* - (.*)<\/h2>/sU',$res->body));
83
            $temp = $res->body;
84
			$temp = explode('<tr><td>Time limit:</td><td>', $temp)[1];
85
			$this->pro['time_limit']=trim(explode('s			</td></tr>', $temp)[0]); //as for html arrange
86
            $temp = $res->body;
87
            $temp = explode('<tr><td>Memory limit:</td><td>', $temp)[1];
88
			$this->pro['memory_limit']=trim(explode('MB</td></tr>' , $temp)[0]);
89
            $temp = $res->body;
90
			$temp = explode('<tr><td>Languages:</td><td>', $temp)[1];
91
			$this->pro['note'] = "Languages Limit:".explode('</td></tr>', $temp)[0];
92
93
            if(strpos($res->body, "<tr><td>Resource:</td><td>")!==false)
94
			{
95
				$temp = $res->body;
96
				$temp = explode('<tr><td>Resource:</td><td>', $temp)[1];
97
				$this->pro['source'] = strip_tags(explode('</td></tr>', $temp)[0]);
98
			}else {
99
                $this->pro['source'] = "SPOJ-".$pid;
100
            }
101
102
            if(strpos($res->body,"No tags")===false)
103
			{
104
				$temp = $res->body;
105
				$temp = explode('<div id="problem-tags" class="col-lg-12 text-center">', $temp)[1];
106
				$temp = explode('</div>', $temp)[0];
107
				$cat = explode('<a href="/problems/tag/', $temp);
108
				for($i = 1;$i < count($cat); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
109
				{
110
					$temp = $cat[$i];
111
					$temp = explode('">', $temp)[0];
112
					array_push($cate,$temp);
113
				}
114
			}
115
116
            $temp = $res->body;
117
			$temp = explode('<div id="problem-body">', $temp)[1];
118
			$content = explode('<div class="text-center">', $temp)[0];
119
120
            $this->pro['description'] = $this->cacheImage(HtmlDomParser::str_get_html(explode('<h3>Input</h3>', $content)[0], true, true, DEFAULT_TARGET_CHARSET, false));
121
            $content = explode('<h3>Input</h3>', $content)[1];
122
            $this->pro['input'] = explode('<h3>Output</h3>', $content)[0];
123
            $content = explode('<h3>Output</h3>', $content)[1];
124
            $tgis->pro['output'] = explode('<h3>Example</h3>', $content)[0];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tgis seems to be never defined.
Loading history...
125
            $content = explode('<h3>Example</h3>', $content)[1];
0 ignored issues
show
Unused Code introduced by
The assignment to $content is dead and can be removed.
Loading history...
126
127
            //TODO: Get compiler of each problem. 
128
        }
129
    }
130
131
    public function getSpojProblem() {
132
        $types = ['classical','challenge','partial','tutorial','riddle','basics'];
133
134
        foreach($types as $type) {
135
            $iterator = 0;
136
            $endPos = false;
137
            while($endPos == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
138
                $url = "https://www.spoj.com/problems/".$type."/sort=0,start=".$iterator*50;
139
                $res = Requests::get($url);
140
                $problemTable = explode('<td align="left">', $res->body);
141
                for($cnt = 1; $cnt < count($problemTable); $cnt++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
142
                    $problemLink = $problemTable[$cnt];
143
                    $problemLink = explode('<a href="/problems/', $problemLink)[1];
144
					$pid=explode('">', $problemLink)[0];
145
146
                    $problemLink = $problemTable[$cnt];
147
                    $problemLink = explode('See the best solutions.">', $problemLink)[1];
148
					$solved_count=explode('</a></td>', $problemLink)[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $solved_count is dead and can be removed.
Loading history...
149
150
                    $problemLink = $problemTable[$cnt];
151
                    $index = self::find('/<td align="center">([\s]*?)<\/td>/',$problemLink);
152
153
                    $this->pro['pcode'] = "SPOJ-".$pid;
154
                    $this->pro['OJ'] = 10;
155
                    $this->pro['contest_id'] = null;
156
                    $this->pro['index_id'] = $index;
157
                    $this->pro['origin'] = "https://www.spoj.com/problems/".$pid;
158
                    $this->pro['input_type']='standard input';
159
                    $this->pro['output_type']='standard output';
160
                    $this->getProblemDetail($pid);
161
                    
162
                    $problem=$problemModel->pid($this->pro['pcode']);
163
164
                    if ($problem) {
165
                        $problemModel->clearTags($problem);
166
                        $new_pid=$this->update_problem($this->oid);
167
                    }else {
168
                        $new_pid=$this->insert_problem($this->oid);
169
                    }
170
                }
171
                if(count($table) < 50) $endPos = true;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table seems to be never defined.
Loading history...
172
                $iterator++;
173
            }
174
        }
175
    }
176
}