Test Setup Failed
Push — master ( 24e507...e2b9fb )
by
unknown
04:37
created

PTA::crawling()   C

Complexity

Conditions 10
Paths 56

Size

Total Lines 106
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 73
nc 56
nop 1
dl 0
loc 106
rs 6.7224
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\VirtualCrawler\PTA;
4
5
use App\Http\Controllers\VirtualCrawler\CrawlerBase;
6
use App\Models\ProblemModel;
7
use App\Models\CompilerModel;
8
use KubAT\PhpSimple\HtmlDomParser;
9
use Auth,Requests,Exception;
10
11
class PTA extends CrawlerBase
12
{
13
    public $oid=6;
14
    private $con, $imgi;
15
    /**
16
     * Initial
17
     *
18
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\VirtualCrawler\PTA\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    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

20
    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...
21
    {
22
        set_time_limit(0); // Pandora's box, engage!
23
        if ($action=='judge_level') {
24
            $this->judge_level();
25
        } else {
26
            $this->crawling($con);
27
        }
28
    }
29
30
    public function judge_level()
31
    {
32
        // TODO
33
    }
34
35
    public function crawling($conType)
36
    {
37
        $start=time();
38
        $f = fopen(__DIR__."/pta_status.log", "w") or die("Unable to open file!");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            fwrite(/** @scrutinizer ignore-type */ $f, "General Detail API Success at {$now}".PHP_EOL);
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $new_pid is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

140
        fclose(/** @scrutinizer ignore-type */ $f);
Loading history...
141
    }
142
}
143