-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-problems.php
More file actions
84 lines (66 loc) · 2.21 KB
/
parse-problems.php
File metadata and controls
84 lines (66 loc) · 2.21 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
use TaskChecker\ProblemParser\Parser;
use TaskChecker\ProblemParser\ProblemSerializer;
require_once __DIR__ . '/../src/bootstrap.php';
function printUsage($defaultSavePath)
{
fwrite(
STDERR,
"Usage: php script.php [--save-path=/path/to/save.json] path1:URL1 [path2:URL2 ...]\n\n" .
"Parses HTML files at given paths, extracts problem descriptions and saves them to
a JSON file (default location is $defaultSavePath)\n" .
"See documentation for details\n"
);
}
$prefix = 'ProblemParser:';
$problemService = $app['problem_service'];
$defaultSavePath = $problemService->getProblemListLocation();
$savePath = $defaultSavePath;
$useOtherPath = false;
$filesAndUrls = [];
foreach (array_slice($argv, 1) as $arg) {
if (in_array($arg, ['-h', '--help'])) {
printUsage($defaultSavePath);
exit(0);
}
if (preg_match("~^--save-path=(.+)$~", $arg, $m)) {
if ($useOtherPath) {
throw new \Exception("$prefix --save-path can be specified only once");
}
$useOtherPath = true;
$savePath = $m[1];
continue;
}
$parts = explode(':', $arg, 2);
if (count($parts) == 2) {
list($file, $url) = $parts;
$filesAndUrls[$file] = $url;
continue;
}
throw new \Exception("$prefix Invalid argument: $arg");
}
if (!$filesAndUrls) {
fwrite(STDERR, "$prefix No file names given\n\n");
printUsage($defaultSavePath);
exit(1);
}
$problemParser = $app['problem_parser'];
$problems = [];
foreach ($filesAndUrls as $path => $url) {
if (!is_file($path)) {
throw new \Exception("$prefix File doesn't exist or is not a file: $path");
}
$html = file_get_contents($path);
$newProblems = $problemParser->parsePage($html, $url);
$problems = array_merge($problems, $newProblems);
}
$problemIds = array_map(function ($p) {return $p->getId();}, $problems);
$problemCount = array_count_values($problemIds);
foreach ($problemCount as $id => $count) {
if ($count > 1) {
throw new \Exception("$prefix Problem with id $id is repeated twice");
}
}
$serializer = new ProblemSerializer;
$string = $serializer->serialize($problems);
file_put_contents($savePath, $string);