| Conditions | 31 |
| Paths | 3900 |
| Total Lines | 132 |
| Code Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 34 | public function crawling($con) |
||
| 35 | { |
||
| 36 | if ($con == 'all') { |
||
| 37 | // TODO |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | try { |
||
| 42 | $dom = HtmlDomParser::file_get_html('https://vijos.org/p/'.$con, false, null, 0, -1, true, true, DEFAULT_TARGET_CHARSET, false); |
||
| 43 | } |
||
| 44 | catch (Exception $e) { |
||
| 45 | if (strpos($e->getMessage(), '404 Not Found') !== false) { |
||
| 46 | header('HTTP/1.1 404 Not Found'); |
||
| 47 | die(); |
||
| 48 | } |
||
| 49 | if (strpos($e->getMessage(), '403 Forbidden') !== false) { |
||
| 50 | header('HTTP/1.1 403 Forbidden'); |
||
| 51 | die(); |
||
| 52 | } |
||
| 53 | throw $e; |
||
| 54 | } |
||
| 55 | |||
| 56 | $mainDiv = $dom->find(".section__body", 0); |
||
| 57 | |||
| 58 | $eles = $mainDiv->children(); |
||
| 59 | array_push($eles, null); |
||
| 60 | $this->pro['description'] = null; |
||
| 61 | $this->pro['input'] = null; |
||
| 62 | $this->pro['output'] = null; |
||
| 63 | $this->pro['sample'] = []; |
||
| 64 | $this->pro['note'] = null; |
||
| 65 | $this->pro['sampleDesc'] = null; |
||
| 66 | $this->pro['limit'] = null; |
||
| 67 | $patterns = [ |
||
| 68 | 'description' => '<h1>描述</h1>', |
||
| 69 | '_format' => '<h1>格式</h1>', |
||
| 70 | 'input' => '<h2>输入格式</h2>', |
||
| 71 | 'output' => '<h2>输出格式</h2>', |
||
| 72 | '_sample' => '/^<h1>样例\d+<\/h1>$/u', |
||
| 73 | '__sampleInput' => '/^<h2>样例输入\d+<\/h2>$/u', |
||
| 74 | '__sampleOutput' => '/^<h2>样例输出\d+<\/h2>$/u', |
||
| 75 | 'limit' => '<h1>限制</h1>', |
||
| 76 | 'note' => '<h1>提示</h1>', |
||
| 77 | 'sampleDesc' => '/<h1>样例(说明|解释)<\/h1>|<h2>样例说明1<\/h2>/', // P2036 has <h2>样例说明1</h2> |
||
| 78 | 'source' => '<h1>来源</h1>', |
||
| 79 | ]; |
||
| 80 | $lastPart = ''; |
||
| 81 | $content = ''; |
||
| 82 | $cursample = []; |
||
| 83 | foreach ($eles as $ele) { |
||
| 84 | $html = $ele ? $ele->outertext : null; |
||
| 85 | $match = !$ele; |
||
| 86 | if (!$match) { |
||
| 87 | foreach ($patterns as $key=>$value) { |
||
| 88 | if ($value[0] != '/' && $html == $value || $value[0] == '/' && preg_match($value, $html)) { |
||
| 89 | $match = $key; |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | if (!$lastPart) { |
||
| 95 | if ($match) $lastPart = $match; |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | if ($match) { |
||
| 99 | if ($lastPart[0] != '_') { |
||
| 100 | $this->pro[$lastPart] = $content; |
||
| 101 | $content = ''; |
||
| 102 | } else if ($lastPart == '__sampleOutput') { // Assume output always follows by input |
||
| 103 | array_push($this->pro['sample'], $cursample); |
||
| 104 | $cursample = []; |
||
| 105 | } |
||
| 106 | $lastPart = $match; |
||
| 107 | } else { |
||
| 108 | if ($lastPart[1] != '_') { |
||
| 109 | if ($lastPart != 'source') $content .= $html; |
||
| 110 | else $content .= $ele->innertext; |
||
| 111 | } else { // Code |
||
| 112 | $code = trim($ele->find('code', 0)->innertext); |
||
| 113 | if ($lastPart == '__sampleInput') { if (isset($cursample['sampleInput'])) die($con); } |
||
| 114 | else { if (isset($cursample['sampleOutput'])) die($con); } |
||
| 115 | if (count($ele->children()) != 1) die($con); |
||
| 116 | if ($lastPart == '__sampleInput') $cursample['sample_input'] = $code; |
||
| 117 | else $cursample['sample_output'] = $code; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | if (!$ele) break; |
||
| 121 | } |
||
| 122 | |||
| 123 | $this->pro['time_limit'] = 1000; |
||
| 124 | $this->pro['memory_limit'] = 262144; |
||
| 125 | if ($this->pro['sampleDesc']) { |
||
| 126 | $this->pro['note'] = '<h3>样例说明</h3>'.$this->pro['sampleDesc'].$this->pro['note']; |
||
| 127 | } |
||
| 128 | if ($this->pro['limit']) { |
||
| 129 | $this->pro['note'] = $this->pro['limit'].$this->pro['note']; |
||
| 130 | $this->pro['time_limit'] = 0; |
||
| 131 | $this->pro['memory_limit'] = 0; |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->pro['pcode'] = 'VIJ'.$con; |
||
| 135 | $this->pro['OJ'] = $this->oid; |
||
| 136 | $this->pro['contest_id'] = null; |
||
| 137 | $this->pro['index_id'] = $con; |
||
| 138 | $this->pro['origin'] = 'https://vijos.org/p/'.$con; |
||
| 139 | $this->pro['title'] = $dom->find('.section__header', 0)->find('h1', 0)->innertext; |
||
| 140 | $this->pro['input_type'] = 'standard input'; |
||
| 141 | $this->pro['output_type'] = 'standard output'; |
||
| 142 | |||
| 143 | $this->pro['markdown'] = 0; |
||
| 144 | $this->pro['tot_score'] = 100; |
||
| 145 | $this->pro["partial"] = 1; |
||
| 146 | $this->pro['source'] = 'Vijos'; // Force Override |
||
| 147 | |||
| 148 | $info = $dom->find(".horizontal", 0); |
||
| 149 | preg_match('/<dt>已通过<\/dt>[\s\S]*<dd>(\d+)<\/dd>/', $info->innertext, $match); |
||
| 150 | $this->pro['solved_count'] = $match[1]; |
||
| 151 | |||
| 152 | $problemModel=new ProblemModel(); |
||
| 153 | $problem=$problemModel->pid($this->pro['pcode']); |
||
| 154 | |||
| 155 | if ($problem) { |
||
| 156 | $problemModel->clearTags($problem); |
||
| 157 | $new_pid=$this->update_problem($this->oid); |
||
| 158 | } else { |
||
| 159 | $new_pid=$this->insert_problem($this->oid); |
||
| 160 | } |
||
| 161 | |||
| 162 | $tags = $info->find('.hasjs--hide', 0); |
||
| 163 | if ($tags) { |
||
| 164 | foreach ($tags->find('a') as $tag) { |
||
| 165 | $problemModel->addTags($new_pid, $tag->innertext); |
||
| 166 | } |
||
| 170 |