forked from pietercolpaert/IWay-TDT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forecast.class.php
executable file
·107 lines (86 loc) · 3.25 KB
/
Forecast.class.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
<?php
/* Copyright (C) 2011 by iRail vzw/asbl
*
* Author: Quentin Kaiser <[email protected]>
* License: AGPLv3
*
* This method of IWay will get forecast about trafic jams and travel times in belgium
*/
include_once 'simple_html_dom.php';
class IWayForecast extends AResource{
private $type;
public static function getParameters(){
return array(
"type" => "The type of forecast that you want to retrieve. Available : traveltime, traficjam."
);
}
public static function getRequiredParameters(){
return array("type");
}
public function setParameter($key,$val){
if($key == "type"){
$this->type = $val;
}
}
private function getData($type){
$result = new stdClass();
$result->item = array();
if(!strcmp($type, "traveltime")){
$url = "http://www.rtbf.be/services/mobilinfo/previsions-trafic";
$data = utf8_encode(TDT::HttpRequest($url)->data);
$html = str_get_html($data);
$times = $html->find('div[class=head] span');
$lengths = $html->find('span[class=indicationPannel]');
for($i=0; $i<count($times); $i++){
$result->item[$i] = new stdClass();
preg_match("/(\d\d)-(\d\d)-(\d\d\d\d)?/",$times[$i]->innertext,$match);
$result->item[$i]->time = mktime(0, 0, 0, $match[2], $match[1], $match[3]);
preg_match("/(\w+)<br \/>(\d+)-(\d+) km?/", $lengths[$i]->innertext, $match);
$result->item[$i]->from = $match[2];
$result->item[$i]->to = $match[3];
}
}
else if(!strcmp($type, "traficjam")){
$url = "http://www.rtbf.be/services/mobilinfo/temps-parcours";
$data = utf8_encode(TDT::HttpRequest($url)->data);
$html = str_get_html($data);
$forecasts = $html->find('div[id=mobilTabs-2] table tr');
$i=0;
foreach($forecasts as $tr){
$element->item[$i++] = new stdClass();
$tds = $tr->find('td');
for($i=0; $i<count($tds); $i++){
if($i==0)
$result->item[$i]->from = $tds[$i]->innertext;
if($i==1)
$result->item[$i]->to = $tds[$i]->innertext;
if($i==2){
preg_match("/(\d+) mins (\+(\d+))?/", $tds[$i], $match);
$result->item[$i]->current_time = $match[1];
}
if($i==3){
preg_match("/(\d+) mins?/", $tds[$i], $match);
$result->item[$i]->normal_time = $match[1];
}
}
}
}
return $result;
}
public function call(){
$c = Cache::getInstance();
$element = $c->get("forecast".$this->type);
if(is_null($element)){
$element = $this->getData();
$c->set("forecast".$this->type, $element, 300);
}
return $element;
}
public static function getAllowedPrintMethods(){
return array("json","xml", "jsonp", "php", "html", "kml", "map");
}
public static function getDoc(){
return "Forecast return data about travel times and trafic jam";
}
}
?>