1 |
56cb34de
|
horkym
|
<?php
|
2 |
|
|
|
3 |
|
|
require_once "traffic.php";
|
4 |
|
|
require_once "location.php";
|
5 |
|
|
|
6 |
|
|
class Parser {
|
7 |
|
|
|
8 |
|
|
private $name;
|
9 |
|
|
private $path;
|
10 |
|
|
|
11 |
|
|
private $traffic;
|
12 |
|
|
private $locations;
|
13 |
|
|
|
14 |
|
|
public function __construct() {
|
15 |
|
|
$this->name = "DOPR_D_";
|
16 |
|
|
$this->path = "http://doprava.plzensky-kraj.cz/opendata/doprava/den/".$this->name;
|
17 |
|
|
$this->traffic = NULL;
|
18 |
|
|
$this->locations = NULL;
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
public function doWork($date) {
|
22 |
|
|
$zipUrl = $this->path.$date.".zip";
|
23 |
|
|
$dir = "../../download/$date/";
|
24 |
|
|
$downloaded = $dir."downloaded.zip";
|
25 |
|
|
|
26 |
|
|
$result = $this->download($date, $zipUrl, $dir, $downloaded);
|
27 |
|
|
if ($result == -1 || $result == 0 || $result == 1) {
|
28 |
|
|
|
29 |
|
|
$ok = -1;
|
30 |
|
|
if ($result == 0 && $this->extract($dir, $downloaded) == 0) {
|
31 |
|
|
unlink($downloaded);
|
32 |
|
|
$ok = 0;
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
if ($ok == 0 || $result == 1) {
|
36 |
|
|
$this->traffic = $this->parse($dir.$this->name.$date.".csv", TRUE);
|
37 |
|
|
$this->locations = $this->parse($dir."Locations.csv", FALSE);
|
38 |
|
|
return;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
$this->deleteDir($dir);
|
42 |
|
|
|
43 |
|
|
}
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
private function parse($fileName, $traffic) {
|
47 |
|
|
$counter = 0; // TODO
|
48 |
|
|
|
49 |
|
|
$array = array();
|
50 |
|
|
if (($file = fopen($fileName, "r"))) {
|
51 |
|
|
while (($row = fgetcsv($file, 1000, "|")) && $counter++ < 10) {
|
52 |
|
|
if ($traffic) {
|
53 |
|
|
$array[] = new Traffic($row);
|
54 |
|
|
} else {
|
55 |
|
|
$array[] = new Location($row);
|
56 |
|
|
}
|
57 |
|
|
}
|
58 |
|
|
fclose($file);
|
59 |
|
|
}
|
60 |
|
|
return $array;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
private function download($date, $zipUrl, $dir, $downloaded) {
|
64 |
|
|
if (strpos(get_headers($zipUrl, 1)[0], "404") === FALSE) {
|
65 |
|
|
if (!file_exists($dir)) {
|
66 |
|
|
if (mkdir($dir)) {
|
67 |
|
|
if (copy($zipUrl, $downloaded)) {
|
68 |
|
|
// Stazeni probehlo v poradku.
|
69 |
|
|
return 0;
|
70 |
|
|
} else {
|
71 |
|
|
// Nepovedlo se stazeni zip souboru.
|
72 |
|
|
return -1;
|
73 |
|
|
}
|
74 |
|
|
} else {
|
75 |
|
|
// Nepodarilo se vytvorit slozku pro data.
|
76 |
|
|
return -2;
|
77 |
|
|
}
|
78 |
|
|
} else {
|
79 |
|
|
// Data k vybranemu dni jiz byla stazena.
|
80 |
|
|
return 1;
|
81 |
|
|
}
|
82 |
|
|
} else {
|
83 |
|
|
// Pro dany datum neexistuji data.
|
84 |
|
|
return -3;
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
private function extract($dir, $downloaded) {
|
89 |
|
|
$zip = new ZipArchive();
|
90 |
|
|
if ($zip->open($downloaded, ZIPARCHIVE::CREATE) === TRUE) {
|
91 |
|
|
$zip->extractTo($dir);
|
92 |
|
|
$zip->close();
|
93 |
|
|
// Extrahovani v poradku dokonceno.
|
94 |
|
|
return 0;
|
95 |
|
|
} else {
|
96 |
|
|
// Nepovedlo se extrahovani obsahu zipu.
|
97 |
|
|
return -1;
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
private function deleteDir($path) {
|
102 |
|
|
if (is_dir($path)) {
|
103 |
|
|
$files = scandir($path);
|
104 |
|
|
foreach ($files as $file) {
|
105 |
|
|
if ($file != "." && $file != "..") {
|
106 |
|
|
$path_ = $path."/".$file;
|
107 |
|
|
if (filetype($path_) == "dir") {
|
108 |
|
|
$this->deleteDir($path_);
|
109 |
|
|
} else {
|
110 |
|
|
unlink($path_);
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
reset($files);
|
115 |
|
|
rmdir($path);
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
public function getTraffic() {
|
120 |
|
|
return $this->traffic;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
public function getLocations() {
|
124 |
|
|
return $this->locations;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
?>
|