-
Notifications
You must be signed in to change notification settings - Fork 5
/
spiderP.php
48 lines (48 loc) · 2.38 KB
/
spiderP.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
error_reporting(E_ALL&~E_WARNING);
function get($url){
sleep(1);
echo $url."\n";
while(($res = file_get_contents($url)) == False) sleep(1);
return $res;
}
$prefix = "problem/";
$handler = fopen("problems_list.md", "w");
$home = get("https://www.luogu.com.cn/problem/list?_contentOnly=1");
$home_arr = json_decode($home, true);
$page_total = ceil($home_arr['currentData']['problems']['count'] / $home_arr['currentData']['problems']['perPage']);
for($i = 1; $i <= $page_total; $i++){
$content = get("https://www.luogu.com.cn/problem/list?page=$i&_contentOnly=1");
$content_arr = json_decode($content, true);
foreach($content_arr['currentData']['problems']['result'] as $index => $key){
$id = $content_arr['currentData']['problems']['result'][$index]['pid'];
$name = $content_arr['currentData']['problems']['result'][$index]['title'];
echo $id." ".$name."\n";
fwrite($handler, "- [$id $name](problem/$id.md)\n");
if(!file_exists($prefix.$id.".md")){
$pro = get("https://www.luogu.com.cn/problem/$id?_contentOnly=1");
$pro_arr = json_decode($pro, true);
$background = $pro_arr['currentData']['problem']['background'];
$description = $pro_arr['currentData']['problem']['description'];
$hint = $pro_arr['currentData']['problem']['hint'];
$input_format = $pro_arr['currentData']['problem']['inputFormat'];
$output_format = $pro_arr['currentData']['problem']['outputFormat'];
$samples = $pro_arr['currentData']['problem']['samples'];
$md = fopen($prefix.$id.".md", "w");
fwrite($md, "# $name\n\n");
if(isset($backgroune)) fwrite($md, "## 题目背景\n\n$background\n\n");
fwrite($md, "## 题目描述\n\n$description\n\n## 输入格式\n\n$input_format\n\n## 输出格式\n\n$output_format\n\n");
for($j = 0; $j < count($samples); $j++){
$id = $j + 1;
$input = trim($samples[$j][0]);
$output = trim($samples[$j][1]);
fwrite($md, "## 样例 #$id\n\n");
fwrite($md, "### 样例输入 #$id\n```\n$input\n```\n\n### 样例输出 #$id\n\n```\n$output\n```\n\n");
}
fwrite($md, "## 提示\n\n$hint\n");
fclose($md);
}
}
}
fclose($handler);
?>