1
|
<?php
|
2
|
|
3
|
class Parser {
|
4
|
|
5
|
private $name;
|
6
|
private $path;
|
7
|
|
8
|
public function __construct() {
|
9
|
$this->name = "DOPR_D_";
|
10
|
$this->path = "http://doprava.plzensky-kraj.cz/opendata/doprava/den/".$this->name;
|
11
|
}
|
12
|
|
13
|
public function doWork($date) {
|
14
|
$zipUrl = $this->path.$date.".zip";
|
15
|
$dir = "download/$date/";
|
16
|
$downloaded = $dir."downloaded.zip";
|
17
|
|
18
|
$result = $this->download($date, $zipUrl, $dir, $downloaded);
|
19
|
if ($result == -1 || $result == 0 || $result == 1) {
|
20
|
|
21
|
$ok = -1;
|
22
|
if ($result == 0 && $this->extract($dir, $downloaded) == 0) {
|
23
|
unlink($downloaded);
|
24
|
$ok = 0;
|
25
|
}
|
26
|
|
27
|
if ($ok == 0 || $result == 1) {
|
28
|
// TODO zpracovani souboru.
|
29
|
// return; odkomentovat v pripade, ze extrahovana data nemaji byt odstranena.
|
30
|
}
|
31
|
|
32
|
$this->deleteDir($dir);
|
33
|
|
34
|
}
|
35
|
}
|
36
|
|
37
|
private function download($date, $zipUrl, $dir, $downloaded) {
|
38
|
if (strpos(get_headers($zipUrl, 1)[0], "404") === FALSE) {
|
39
|
if (!file_exists($dir)) {
|
40
|
if (mkdir($dir)) {
|
41
|
if (copy($zipUrl, $downloaded)) {
|
42
|
// Stazeni probehlo v poradku.
|
43
|
return 0;
|
44
|
} else {
|
45
|
// Nepovedlo se stazeni zip souboru.
|
46
|
return -1;
|
47
|
}
|
48
|
} else {
|
49
|
// Nepodarilo se vytvorit slozku pro data.
|
50
|
return -2;
|
51
|
}
|
52
|
} else {
|
53
|
// Data k vybranemu dni jiz byla stazena.
|
54
|
return 1;
|
55
|
}
|
56
|
} else {
|
57
|
// Pro dany datum neexistuji data.
|
58
|
return -3;
|
59
|
}
|
60
|
}
|
61
|
|
62
|
private function extract($dir, $downloaded) {
|
63
|
$zip = new ZipArchive();
|
64
|
if ($zip->open($downloaded, ZIPARCHIVE::CREATE) === TRUE) {
|
65
|
$zip->extractTo($dir);
|
66
|
$zip->close();
|
67
|
// Extrahovani v poradku dokonceno.
|
68
|
return 0;
|
69
|
} else {
|
70
|
// Nepovedlo se extrahovani obsahu zipu.
|
71
|
return -1;
|
72
|
}
|
73
|
}
|
74
|
|
75
|
private function deleteDir($path) {
|
76
|
if (is_dir($path)) {
|
77
|
$files = scandir($path);
|
78
|
foreach ($files as $file) {
|
79
|
if ($file != "." && $file != "..") {
|
80
|
$path_ = $path."/".$file;
|
81
|
if (filetype($path_) == "dir") {
|
82
|
$this->deleteDir($path_);
|
83
|
} else {
|
84
|
unlink($path_);
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
reset($files);
|
89
|
rmdir($path);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
}
|
94
|
|
95
|
?>
|