| Conditions | 10 |
| Paths | 56 |
| Total Lines | 106 |
| Code Lines | 73 |
| 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 |
||
| 35 | public function crawling($conType) |
||
| 36 | { |
||
| 37 | $start=time(); |
||
| 38 | $f = fopen(__DIR__."/pta_status.log", "w") or die("Unable to open file!"); |
||
| 39 | if ($conType == 'all') { |
||
| 40 | // Here is the script |
||
| 41 | // |
||
| 42 | // var a=""; |
||
| 43 | // document.querySelectorAll('a[href^="/problem-sets/"]').forEach(v=>{a+=v.href.split("/")[4]+","}) |
||
| 44 | // console.log(a); |
||
| 45 | |||
| 46 | $conList=[12,13,14,15,16,17,434,994805046380707840,994805148990160896,994805260223102976,994805342720868352]; |
||
| 47 | } else { |
||
| 48 | $conList=[intval($conType)]; |
||
| 49 | } |
||
| 50 | |||
| 51 | foreach ($conList as $con) { |
||
| 52 | $this->con = $con; |
||
| 53 | $this->imgi = 1; |
||
| 54 | $problemModel=new ProblemModel(); |
||
| 55 | $res = Requests::post("https://pintia.cn/api/problem-sets/$con/exams",[ |
||
| 56 | "Content-Type"=>"application/json" |
||
| 57 | ],"{}"); |
||
| 58 | |||
| 59 | if (strpos($res->body, 'PROBLEM_SET_NOT_FOUND') !== false) { |
||
| 60 | header('HTTP/1.1 404 Not Found'); |
||
| 61 | die(); |
||
| 62 | } else { |
||
| 63 | $generalDetails=json_decode($res->body,true); |
||
| 64 | $compilerModel = new CompilerModel(); |
||
| 65 | $list = $compilerModel->list($this->oid); |
||
| 66 | $compilers = []; |
||
| 67 | foreach ($generalDetails['problemSet']['problemSetConfig']['compilers'] as $lcode) { |
||
| 68 | foreach ($list as $compiler) { |
||
| 69 | if ($compiler['lcode'] == $lcode) { |
||
| 70 | array_push($compilers, $compiler['coid']); |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | $this->pro['special_compiler'] = join(',', $compilers); |
||
| 76 | } |
||
| 77 | |||
| 78 | $now=time()-$start; |
||
| 79 | fwrite($f, "General Detail API Success at {$now}".PHP_EOL); |
||
| 80 | |||
| 81 | $probLists = json_decode(Requests::get( |
||
| 82 | "https://pintia.cn/api/problem-sets/$con/problems?type=PROGRAMMING&exam_id=0",[ |
||
| 83 | "Content-Type"=>"application/json" |
||
| 84 | ] |
||
| 85 | )->body, true)["problemSetProblems"]; |
||
| 86 | |||
| 87 | $now=time()-$start; |
||
| 88 | fwrite($f, " Problems List API Success at {$now}".PHP_EOL); |
||
| 89 | |||
| 90 | foreach ($probLists as $prob) { |
||
| 91 | $probDetails = json_decode(Requests::get( |
||
| 92 | "https://pintia.cn/api/problem-sets/$con/problems/{$prob["id"]}?exam_id=0",[ |
||
| 93 | "Content-Type"=>"application/json" |
||
| 94 | ] |
||
| 95 | )->body, true)["problemSetProblem"]; |
||
| 96 | |||
| 97 | $now=time()-$start; |
||
| 98 | fwrite($f, " Problem Details API Success at {$now}".PHP_EOL); |
||
| 99 | |||
| 100 | $this->pro['pcode'] = 'PTA'.$prob["id"]; |
||
| 101 | $this->pro['OJ'] = $this->oid; |
||
| 102 | $this->pro['contest_id'] = $con; |
||
| 103 | $this->pro['index_id'] = $prob["id"]; |
||
| 104 | $this->pro['origin'] = "https://pintia.cn/problem-sets/$con/problems/{$prob["id"]}"; |
||
| 105 | $this->pro['title'] = $prob["title"]; |
||
| 106 | $this->pro['time_limit'] = $probDetails["problemConfig"]["programmingProblemConfig"]["timeLimit"]; |
||
| 107 | $this->pro['memory_limit'] = $probDetails["problemConfig"]["programmingProblemConfig"]["memoryLimit"]; |
||
| 108 | $this->pro['solved_count'] = $prob["acceptCount"]; |
||
| 109 | $this->pro['input_type'] = 'standard input'; |
||
| 110 | $this->pro['output_type'] = 'standard output'; |
||
| 111 | |||
| 112 | $this->pro['description'] = str_replace("](~/","](https://images.ptausercontent.com/",$probDetails["content"]); |
||
| 113 | $this->pro['description'] = str_replace("$$","$",$this->pro['description']); |
||
| 114 | $this->pro['markdown'] = 1; |
||
| 115 | $this->pro['tot_score'] = $probDetails["score"]; |
||
| 116 | $this->pro["partial"] = 1; |
||
| 117 | $this->pro['input'] = null; |
||
| 118 | $this->pro['output'] = null; |
||
| 119 | $this->pro['note'] = null; |
||
| 120 | $this->pro['sample'] = []; |
||
| 121 | $this->pro['source'] = $generalDetails["problemSet"]["name"]; |
||
| 122 | |||
| 123 | $problem=$problemModel->pid($this->pro['pcode']); |
||
| 124 | |||
| 125 | if ($problem) { |
||
| 126 | $problemModel->clearTags($problem); |
||
| 127 | $new_pid=$this->update_problem($this->oid); |
||
| 128 | } else { |
||
| 129 | $new_pid=$this->insert_problem($this->oid); |
||
| 130 | } |
||
| 131 | |||
| 132 | $now=time()-$start; |
||
| 133 | fwrite($f, " Problem {$this->pro['pcode']} Success at {$now}".PHP_EOL); |
||
| 134 | |||
| 135 | sleep(1); // PTA Restrictions |
||
| 136 | |||
| 137 | // $problemModel->addTags($new_pid, $tag); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | fclose($f); |
||
| 141 | } |
||
| 143 |