-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_data.php
74 lines (61 loc) · 2.08 KB
/
import_data.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
$DB_FILE = 'buskatoon.sqlite3';
$ROUTES_URL = 'http://opendata-saskatoon.cloudapp.net:8080/v1/SaskatoonOpenDataCatalogueBeta/TransitRoutes4/';
$TRIPS_URL = 'http://opendata-saskatoon.cloudapp.net:8080/v1/SaskatoonOpenDataCatalogueBeta/TransitTrips4/';
if (file_exists($DB_FILE)) {
unlink($DB_FILE);
}
$db = new PDO("sqlite:$DB_FILE");
$db->query('PRAGMA synchronous = OFF;');
$db->exec('
CREATE TABLE routes (
id INTEGER PRIMARY KEY,
short_name TEXT,
long_name TEXT,
color TEXT
);
');
$db->exec('
CREATE TABLE trips (
id INTEGER PRIMARY KEY,
route_id INTEGER,
headsign TEXT
);
');
$sql = 'INSERT INTO routes (id, short_name, long_name, color) VALUES (:id, :short_name, :long_name, :color);';
$stmt = $db->prepare($sql);
echo "Retrieving $ROUTES_URL\n";
$routes = simplexml_load_file($ROUTES_URL);
foreach ($routes->entry as $route) {
$route = $route->content->children('m', true)->properties->children('d', true);
// Add missing leading 0 that the XML feed seems to chop off
$short_name = $route->route_short_name;
if (strlen($short_name) == 1) $short_name = "0$short_name";
$stmt->execute([
':id' => $route->route_id,
':short_name' => $short_name,
':long_name' => $route->route_long_name,
':color' => $route->route_color
]);
}
$sql = 'INSERT INTO trips (id, route_id, headsign) VALUES (:id, :route_id, :headsign);';
$stmt = $db->prepare($sql);
$trips_url = $TRIPS_URL;
do {
echo "Retrieving $trips_url\n";
$trips = simplexml_load_file($trips_url);
foreach($trips->getDocNamespaces() as $prefix => $namespace) {
if (empty($prefix)) $prefix = "global";
$trips->registerXPathNamespace($prefix, $namespace);
}
foreach ($trips->entry as $trip) {
$trip = $trip->content->children('m', true)->properties->children('d', true);
$stmt->execute([
':id' => $trip->trip_id,
':route_id' => $trip->route_id,
':headsign' => $trip->trip_headsign
]);
}
$next_link = $trips->xpath("global:link[@rel='next']");
$trips_url = empty($next_link) ? false : (string)$next_link[0]['href'];
} while ($trips_url);