Skip to content

Instantly share code, notes, and snippets.

@remy
Created November 28, 2016 12:11
Show Gist options
  • Save remy/8aef7b241b31ea97acb53a23a9adc862 to your computer and use it in GitHub Desktop.
Save remy/8aef7b241b31ea97acb53a23a9adc862 to your computer and use it in GitHub Desktop.
A small demo of how to create a "next bus" service based on scraping a specific bus service page, and displaying in a LaMetric compatible format.
// follow modules loaded via `npm i --save request cheerio`
const request = require('request');
const cheerio = require('cheerio');
const http = require('http');
http.createServer((req, res) => {
getDueTimes().then(data => {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify(data));
}).catch(error => {
console.error(error);
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ error }));
});
}).listen(3000);
function getDueTimes() {
// we're returning a promise so we can later reuse in
// a web server
return new Promise((resolve, reject) => {
// make a GET request to get a page of the next due buses
request({
url: 'http://m.buses.co.uk/brightonbuses/operatorpages/mobilesite/stop.aspx?stopid=6979&device=&s=50&d=&stopcode=&source=siri'
}, (error, res, body) => {
// if there was something wrong with the request, print it
// out and exit the function
if (error) {
return reject(error);
}
// …otherwise, load the HTML into Cheerio, which give
// us jQuery-like access
const $ = cheerio.load(body);
// now query the DOM for the times (bespoke to your markup)
// and return just the text for each of those nodes, note
// that to get a _real_ array back, I have to use `.get()`
// at the end.
const times = $('.colDepartureTime').map((i, el) => {
return $(el).text();
}).get();
// resolve the promise: specifically, return the times
const icon = "i996"; // our bus icon
const data = { frames: times.map(text => ({ text, icon })) };
resolve(data);
});
});
}
{
"name": "bus",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^0.22.0",
"request": "^2.79.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment