-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoming_bus.php
More file actions
executable file
·105 lines (95 loc) · 2.33 KB
/
coming_bus.php
File metadata and controls
executable file
·105 lines (95 loc) · 2.33 KB
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
#!/usr/bin/php
<?php
require('lib/simple_html_dom.php');
/**
* ComingBus
*
*/
class ComingBus
{
//Constants
const TFL_BUS_COUNTDOWN_BASEURL = "http://m.countdown.tfl.gov.uk/arrivals/";
/**
* getBuses
*
* @param array $buses
* @param int $stopcode
* @static
* @access public
* @return array
*/
static function getBuses($buses, $stopcode) {
$html = file_get_html(self::TFL_BUS_COUNTDOWN_BASEURL.$stopcode);
$ticker = $html->find('td.resRoute');
$out = array();
foreach ($ticker as $item){
$bus = trim($item->plaintext);
if (in_array($bus, $buses)) {
$out[] = array('bus'=>$bus, 'time'=>trim($item->next_sibling()->next_sibling()->plaintext));
}
}
return $out;
}
}
//Default params
$stopcode = '75882';
$buses = array(154);
//Parse information in the configuration file
$conf_file = $_SERVER['HOME'].'/.bus';
if (file_exists($conf_file)) {
$info = json_decode(file_get_contents($conf_file));
if (isset($info->stop)) {
$stopcode = intval($info->stop);
}
if (isset($info->buses) && is_array($info->buses)) {
$buses = $info->buses;
}
}
//This function shows a help message
function help(){
echo "bus: usage\n";
echo "usage: bus [-s stop_number] [-b bus1[:bus2]]\n";
echo "~/.bus format: \n";
echo '{"buses":[123],"stop":12345}';
die;
}
//TODO function to parse parameters
$params = array_slice($argv,1);
for ($i=0;$i<count($params);$i++){
//Stop parameter
if ($params[$i] == "-s" ) {
if (!isset($params[$i+1])) {
help();
exit(0);
}else{
if ( ($stopcode = intval($params[$i+1])) == 0 ) {
help();
exit(0);
}
}
}
//Bus number
if ($params[$i] == "-b" ) {
if (!isset($params[$i+1])) {
help();
exit(0);
}else{
$buses = explode(':',$params[$i+1]);
}
}
//Help
if ($params[$i] == "-h" ) {
help();
exit(1);
}
}
try{
$data = ComingBus::getBuses($buses, $stopcode);
echo "Stop $stopcode\n";
foreach ($data as $bus_coming) {
echo "{$bus_coming['bus']}\t{$bus_coming['time']}\n";
}
}catch(Exception $e){
echo $e;
}
exit(1);