forked from tyz910/sf-crime-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grabber.php
50 lines (40 loc) · 1.1 KB
/
grabber.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
49
50
<?php
$file = new SplFileObject(__DIR__ . '/../data/train.csv');
$data = [];
$prevYear = $prevWeek = '';
$num = 0;
while ($row = $file->fgetcsv()) {
if ($num && $row[0]) {
$date = new \DateTime($row[0]);
$year = $date->format('Y');
$week = $date->format('W');
if ($week != $prevWeek) {
save($data, $prevYear, $prevWeek);
$data = [];
}
$data[$row[1]][] = [
'Date' => $row[0],
'Hour' => $date->format('H'),
'DayOfWeek' => $row[3],
'Descript' => $row[2],
'PdDistrict' => $row[4],
'Resolution' => $row[5],
'Address' => $row[6],
'X' => $row[7],
'Y' => $row[8],
];
$prevYear = $year;
$prevWeek = $week;
}
$num++;
}
save($data, $year, $week);
function save($data, $year, $week)
{
echo "$year - $week\n";
$dir = __DIR__ . "/dates/{$year}";
if (!is_dir($dir)) {
mkdir($dir);
}
file_put_contents($dir . "/{$week}.json", json_encode($data));
}