-
Notifications
You must be signed in to change notification settings - Fork 0
/
blatube.php.save
172 lines (160 loc) · 4.77 KB
/
blatube.php.save
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
168
169
170
171
172
me<?php
/* Blatube - Script to retrive data about London Underground Tube statuses.*/
//Orginal Author: Phil Burton <[email protected]>
// Version History
// 0.0.1 Created blatube.php
// 0.0.2 Spelt Northern correctly, Added summary.
// 0.0.3 Added Bakerloo.
// 0.0.4 Spelling mistake Be != Ba
// 0.1.0 Fixed Summary. Feature complete.
$version = "BlaTube Version: 0.1.0";
// get the input
$stdin = read_stdin();
$stdin = explode(" ", strtolower($stdin));
$out = "";
$token = "";
for ($i=0 ; $i<sizeof($stdin); $i++)
{
switch ($stdin[$i])
{
// List the Underground Lines
case "-l":
echo "Bakerloo (Ba) - Central (Ce) - Circle (Ci) - District (Di) - Hammersmith and City (Ha) - Jubilee (Ju) - Metropolitan (Me) - Northern (No) - Piccadilly (Pi) - Victoria (Vi) - Waterloo and City (Wa) - Overground (Ov) - DLR\n";
break;
// Version information
case "-v":
echo $version . "\n";
break;
// Help
case "-h":
echo "-l lists tube lines. -v shows version information -<TubeLine> will display information for that line. -s will display summary of lines (-s is run by default if no other options are chosen. -h Displays this help message." . "\n";
break;
// Specfic Line information
case "bakerloo":
case "ba":
$array = populateTubes()[0];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "central":
case "ce":
$array = populateTubes()[1];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "circle":
case "ci":
$array = populateTubes()[2];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "district":
case "di":
$array = populateTubes()[3];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "hammersmith":
case "ha":
$array = populateTubes()[4];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "jubilee":
case "ju":
$array = populateTubes()[5];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "metropolitan":
case "me":
$array = populateTubes()[6];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "northern":
case "no":
$array = populateTubes()[7];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "piccadilly":
case "pi":
$array = populateTubes()[8];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "victoria":
case "vi":
$array = populateTubes()[9];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "waterloo":
case "wa":
$array = populateTubes()[10];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "overground":
case "ov":
$array = populateTubes()[11];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
case "dlr":
$array = populateTubes()[12];
echo $array[0] . ": " . $array[1] . " - " . $array[2] . " " . $array[3] . "\n";
break;
// Summary of Line info
case "-s":
default:
$array = populateTubes();
$out = "";
foreach($array as $element)
{
if($element[3] != "")
{
$out .= $element[0] . ": " . $element[1] . " - " . $element[2] . " " . $element[3] . "\n";
}
}
if($out == "")
{
$out = "Good service on all lines\n";
}
echo $out;
break;
}
}
function populateTubes()
{
$stuff = file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/LineStatus");
$stuff = explode("<LineStatus",$stuff);
// Create array to hold all tube information
$tubes = array();
$j = 0;
foreach($stuff as $line)
{
if($j>0)
{
$line = explode("\"",$line);
$details = $line[3];
$name = $line[7];
if($line[13] != "")
{
$desc = "- " . $line[13];
}
if($line[15] == "true")
{
$active = "Running";
}
else
{
$active = "Not Running";
}
// Create array for specfic Line
$tube = array($name, $desc, $active, $details);
// Add Line info to tubes array.
$tubes[$j-1] = $tube;
}
$j++;
}
return $tubes;
}
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
?>