I recently bought a PSP and wanted to know the best ISO files and wrote a scraper to retrieve games ISOs titles that received a high rating and send them to a csv. Any recommendations as to readability, and/or better code optimizations?
var request = require('request');
var cheerio = require('cheerio');
var json2csv = require('json2csv');
var fs = require('fs');
var url = 'http://goo.gl/cc4HRc';
var pspISOs = [];
request(url, function (error, response, html) {
if (!error && response.statusCode === 200) {
var $ = cheerio.load(html);
$('.gamelist').each(function () {
var titles = $(this).text();
var ratings = +parseFloat($(this)
.parents('tr').find('td')
.last().text()).toFixed(2);
pspISOs.push({ title: titles, rating: ratings });
});
var topISOs = pspISOs.filter(function (iso) {
return iso.rating >= 4.81;
});
var columns = Object.keys(topISOs[0]);
json2csv({ data: topISOs, fields: columns }, function (err, csv) {
if (err) console.log(err);
fs.writeFile('psp_iso.csv', csv, function (err) {
if (err) throw err;
console.log('Saved.');
});
});
}
});