Skip to content

Instantly share code, notes, and snippets.

@TylerBrock
Last active March 29, 2020 04:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerBrock/106cad5277151913f197daa2994e5991 to your computer and use it in GitHub Desktop.
Save TylerBrock/106cad5277151913f197daa2994e5991 to your computer and use it in GitHub Desktop.
Get the turnstile data from NYC MTA website
const fs = require('fs');
const axios = require('axios');
const cheerio = require('cheerio');
const downloadDir = './downloads';
const siteUrl = 'http://web.mta.info/developers/turnstile.html';
async function fetchData() {
const result = await axios.get(siteUrl);
return cheerio.load(result.data);
}
function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms));
}
async function main() {
const $ = await fetchData();
const urls = $('#contentbox > .container > .last a').map((index, element) => {
return $(element).attr('href');
}).get();
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
}
console.log(`found ${urls.length} files to download`);
for (const url of urls) {
console.log(`Downloading ${url}`);
const response = await axios({
method: 'get',
url: `http://web.mta.info/developers/${url}`,
responseType: 'stream',
});
const filename = url.split('/').pop();
const stream = fs.createWriteStream(`${downloadDir}/${filename}`);
response.data.pipe(stream);
sleep(1000);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment