-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-regional-rail-trainview.php
executable file
·169 lines (118 loc) · 3.09 KB
/
get-regional-rail-trainview.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env php
<?php
/**
*
* Fetch our Regional Trail train info.
*
* A full list of API endpoints can be found at http://www3.septa.org/hackathon/
*
*/
date_default_timezone_set("EST5EDT");
/**
* Fetch our JSON from the API.
*
* @param string $url The URL to fetch from
*
* @return string the JSON that is retrieved.
*/
function get_json($url) {
$ch = curl_init();
//
// Connection timeout
//
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
//
// How long, once connected, to get the data.
//
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno) {
throw new Exception("Curl Error on URL '$url': ($curl_errno): $curl_error");
}
curl_close($ch);
return($response);
} // End of get_json()
/**
* Return the current timestamp.
*/
function get_timestamp() {
$retval = date("Y-m-d H:i:s");
return($retval);
} // End of get_timestamp()
/**
* This adds a timestamp into a row, but we specifically need the timestamp
* to be first, so that Splunk will see it when the row is imported.
*
* (Yes, this is duplicated across 2 different PHP scripts. Technical debt. )
*
* @param array $data The current piece of train data we're working with.
*
* @param string $timestamp The timestamp assocaited with this train data.
*
* @return string A JSON-encoded string with the timestamp in the beginning.
*/
function add_timestamp($data, $timestamp) {
$row = array();
$row["_timestamp"] = $timestamp;
foreach ($data as $key => $value) {
$row[$key] = $value;
}
$retval = json_encode($row);
return($retval);
} // End of add_timestamp()
/**
* Split our JSON into one line per array element (train).
*
* @param string $json The JSON we got from SEPTA
*
* @param string $timestamp The current timestamp
*
* @return string A string with one JSON row per train, with timestamp data added.
*
*/
function parse_line($json, $timestamp) {
$retval = "";
$data = json_decode($json, true);
//$data = "bad data"; // Debugging
if (is_array($data)) {
if (!isset($data["error"])) {
foreach ($data as $key => $value) {
$row = add_timestamp($value, $timestamp);
$retval .= $row . "\n";
}
} else {
//
// We got some kind error, so insert our timestamp and that's it
//
$data["_timestamp"] = $timestamp;
$row = json_encode($data);
$retval .= $row . "\n";
}
} else {
print "ERROR: I don't know what to do with this data: $data\n";
}
return($retval);
} // End of parse_lines()
/**
* Our main entry point.
*/
function main() {
$url = "http://www3.septa.org/hackathon/TrainView/";
//$url = "http://www.google.com/notaurl"; // Debugging - 404
//$url = "http://foobar.localdomain/"; // Debugging - Bad DNS
//$url = "http://10.255.255.254"; // Debugging - Timeout
try {
$json = get_json($url);
$timestamp = get_timestamp();
$lines = parse_line($json, $timestamp);
} catch (Exception $e) {
print "ERROR: " .$e->getMessage() . "\n";
exit(1);
}
print $lines;
} // End of main()
main();