Skip to content

Instantly share code, notes, and snippets.

@smarr
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarr/acbe7c73c3e75ca1fea6 to your computer and use it in GitHub Desktop.
Save smarr/acbe7c73c3e75ca1fea6 to your computer and use it in GitHub Desktop.
Scrap Lille's Velo availability
<?php
// scrapping the V'Lille Bicycle availability
$index = file_get_contents('http://www.vlille.mobi/index.php?id=158'); //('index158.html');
$lines = explode("\n", $index);
class Item {
public $name;
public $velos;
public $places;
}
function address($line, &$data, &$item) {
if (strpos($line,'class="address"') !== false) {
return 'station';
}
return 'address';
}
function station($line, &$data, &$item) {
if (1 === preg_match("/<td.*class=\"station\".*>(.*)<\/td>/", $line, $matches)) {
$new = new Item();
$new->name = trim($matches[1]);
$data[$new->name] = $new;
$item = $new;
return 'velos';
}
return 'station';
}
function velos($line, &$data, &$item) {
if (1 === preg_match("/<td.*class=\"velo\".*>(\d*)<\/span.*/", $line, $matches)) {
$item->velos = $matches[1];
return 'places';
}
return 'velos';
}
function places($line, &$data, &$item) {
if (1 === preg_match("/<td.*class=\"place\".*>(\d*)<\/span.*/", $line, $matches)) {
$item->places = $matches[1];
return 'address';
}
return 'places';
}
$state = 'address'; // 'address', 'places', ...
$data = array();
$item = NULL;
foreach ($lines as $line) {
$state = $state($line, $data, $item);
}
$stations = array(
// here go the station id's/names
// examples:
"82 Gare Lille Europe",
"24 Flandres Euralille");
?>
<html>
<body>
<table>
<tr>
<th></th><th>Available Bikes</th><th>Free Spots</th>
</tr>
<?php
foreach ($stations as $station) {
$item = $data[$station];
echo "<tr><td>{$item->name}</td><td>{$item->velos}</td><td>{$item->places}</td></tr>\n";
}
?>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment