Skip to content

Instantly share code, notes, and snippets.

@bfritscher
Created July 11, 2014 19:58
Show Gist options
  • Save bfritscher/1e65892b5955febff83d to your computer and use it in GitHub Desktop.
Save bfritscher/1e65892b5955febff83d to your computer and use it in GitHub Desktop.
<?php
Class PubliBike {
const ONLINE = 1;
const OFFLINE = 3;
const CONSTRUCTION = 0;
const CACHE_FILE = "/home/www/804dba0db44da871b3b97d6441c7ff89/web/getvelo/cache";
public function getNetworks(){
$networks = $this->cache_fetch();
if(!$networks){
$networks = $this->fetchNetworkList();
}
return $networks;
}
private function cache_fetch(){
if (file_exists(self::CACHE_FILE) and time() - filemtime(self::CACHE_FILE) < 60){
return unserialize(file_get_contents(self::CACHE_FILE));
}else{
return false;
}
}
private function cache_store($data){
file_put_contents(self::CACHE_FILE, serialize($data));
}
private function fetchNetworkList(){
$networks = array();
$networks_map = array();
$url = "https://www.publibike.ch/myinterfaces/terminals.fr.json?filterid=0&stationArrow=show%20all%20stations";
// use key 'http' even if you send the request to https://...
$options = array('http' => array('method' => 'GET'));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$root = json_decode($result);
foreach($root->groups as $group){
if( $group->id != 35 ){ //remove SwissPass
$network = array(
'id' => $group->id,
'name' => $group->name,
'timestamp' => date('Y-m-d H:i:s'),
'stations' => array());
$networks_map[$group->id] = $network;
}
}
foreach($root->terminals as $terminal){
if($terminal->status != self::OFFLINE){
$bikes = $terminal->bikes[0]->available;
$racks = $terminal->bikeholders[0]->holdersfree;
$station = array(
'id' => $terminal->terminalid,
'name' => $terminal->name,
'city' => $terminal->city,
'lat' => $terminal->lat,
'long' => $terminal->lng,
'status' => $terminal->status == 1 ? 0 : 1,
'bikes' => $bikes ? $bikes : 0, //type, available
'racks' => $racks ? $racks : 0, //type, holders, holdersfree
'r' => str_repeat('4', $bikes) . str_repeat('0', $racks),
'r2' => ''
);
foreach($terminal->groupids as $id){
if( $id != 35 ) { //remove SwissPass
$networks_map[$id]['stations'][] = $station;
}
}
}
}
$networks = array();
foreach($networks_map as $n){
$networks[] = $n;
}
$this->cache_store($networks);
return $networks;
}
}
function findById($array, $id){
for($i=0; $i < count($array); $i++){
if($array[$i]['id'] == $id){
return $array[$i];
}
}
return array('name' => 'NOT FOUND', 'stations' => array());
}
$velopass = new PubliBike();
if(isset($_GET['action']) && $_GET['action'] == 'tile'){
if(isset($_GET['network']) && isset($_GET['station'])){
$network = findById($velopass->getNetworks(), $_GET['network']);
$station = findById($network['stations'], $_GET['station']);
drawTile($station);
}
}else{
print json_encode($velopass->getNetworks());
}
function drawTile($station){
//TileSquareText01 if len <10?
//translate
//singular, plurals
header('Content-type: text/xml');
$tile = <<<EOT
<?xml version="1.0" encoding="utf-8" ?>
<tile>
<visual>
<binding template="TileWideText01" branding="logo">
<text id="1">{$station['name']}</text>
<text id="2">{$station['city']}</text>
<text id="3">▲ {$station['bikes']} vélos</text>
<text id="4">▼ {$station['racks']} bornes</text>
</binding>
<binding template="TileSquareText03" branding="logo">
<text id="1">{$station['name']}</text>
<text id="2">{$station['city']}</text>
<text id="3">▲ {$station['bikes']} vélos</text>
<text id="4">▼ {$station['racks']} bornes</text>
</binding>
</visual>
</tile>
EOT;
print $tile;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment