switched jsdom for cheerio. Epic speed improvements!

This commit is contained in:
Victor Nguyen 2013-03-18 22:37:29 +11:00
parent ce9317034d
commit c1b9977bf8
3 changed files with 111 additions and 128 deletions

View file

@ -9,9 +9,9 @@
"dependencies": { "dependencies": {
"express": "3.1.0", "express": "3.1.0",
"jade": "*", "jade": "*",
"jsdom": "0.3.4",
"request": "2.12.0", "request": "2.12.0",
"underscore": "*" "underscore": "*",
"cheerio": "~0.10.8"
}, },
"engines": { "engines": {
"node": "0.8.x", "node": "0.8.x",

View file

@ -6,7 +6,7 @@
exports.index = function(req, res){ exports.index = function(req, res){
var _ = require('underscore'), var _ = require('underscore'),
jsdom = require('jsdom'), cheerio = require('cheerio'),
request = require('request'); request = require('request');
@ -28,18 +28,11 @@ exports.index = function(req, res){
// TODO: render error page here... // TODO: render error page here...
} }
jsdom.env( parsePage(body);
{
html: body,
scripts: ['http://code.jquery.com/jquery.min.js']
},
parsePage
);
} }
function parsePage(error, window) { function parsePage(body) {
var $ = window.jQuery, var $ = cheerio.load(body),
$body = $(window.document.body),
$vehicleLinks = $('#vehicles').children('a'); $vehicleLinks = $('#vehicles').children('a');
$vehicleLinks.each(function(i,link) { $vehicleLinks.each(function(i,link) {

View file

@ -6,7 +6,7 @@
exports.fetch = function(req, res){ exports.fetch = function(req, res){
var _ = require('underscore'), var _ = require('underscore'),
jsdom = require('jsdom'), cheerio = require('cheerio'),
request = require('request'); request = require('request');
var model = req.params.model, var model = req.params.model,
@ -33,14 +33,7 @@ exports.fetch = function(req, res){
var tickUrl = 'http://www.mazda.com.au/brochures/base-framework/img/specs/specs_tick.gif', var tickUrl = 'http://www.mazda.com.au/brochures/base-framework/img/specs/specs_tick.gif',
falseString = '-'; falseString = '-';
jsdom.env( var $ = cheerio.load(body),
{
html: body,
scripts: ['http://code.jquery.com/jquery.min.js']
},
function (error, window) {
var $ = window.jQuery,
$body = $(window.document.body),
$styles = $('.spec-body'); $styles = $('.spec-body');
// For each Body Style (Sedan, Wagon, etc.) // For each Body Style (Sedan, Wagon, etc.)
@ -50,7 +43,7 @@ exports.fetch = function(req, res){
// console.log('FIRST BODY STYLE CATEGORIES YO: ', _.pluck(specs[0].categories, 'name')); // console.log('FIRST BODY STYLE CATEGORIES YO: ', _.pluck(specs[0].categories, 'name'));
// res.send('There are ' + $styles.length + ' body style(s) for the ' + model); // res.send('There are ' + $styles.length + ' body style(s) for the ' + model);
var carName = $body.find('h1').text().replace(' Specifications',''); var carName = $('h1').text().replace(' Specifications','');
res.render( res.render(
'specs', 'specs',
{ {
@ -79,11 +72,11 @@ exports.fetch = function(req, res){
if (i === 0) { if (i === 0) {
return; return;
} }
style.grades.push( $.trim($(grade).text()) ); style.grades.push( $(grade).text() );
}); });
// Assign Body Style name (e.g. 'Sedan') // Assign Body Style name (e.g. 'Sedan')
style.name = $.trim( $bodyStyle.find('h3').text() ); style.name = $bodyStyle.find('h3').text();
style.image = 'http://www.mazda.com.au' + $('#specs-body').find('li').eq(i).find('img').attr('src'); style.image = 'http://www.mazda.com.au' + $('#specs-body').find('li').eq(i).find('img').attr('src');
style.slug = slugify(style.name); style.slug = slugify(style.name);
@ -93,8 +86,7 @@ exports.fetch = function(req, res){
$rows = $cat.find('tbody tr'); $rows = $cat.find('tbody tr');
var category = {}; var category = {};
// category.name = $.trim( $cat.children('h4').text() ); category.name = $('.specs-tabs').first().find('li').eq(i).text();
category.name = $.trim( $('.specs-tabs').first().find('li').eq(i).text() );
category.specs = []; category.specs = [];
category.slug = slugify(category.name); category.slug = slugify(category.name);
@ -109,7 +101,7 @@ exports.fetch = function(req, res){
// strip out any <sup> elems // strip out any <sup> elems
$cell.find('sup').remove(); $cell.find('sup').remove();
var text = $.trim( $(cell).text() ), var text = $(cell).text(),
hasTick = $cell.html().search('/images/specs/tick.gif') !== -1, hasTick = $cell.html().search('/images/specs/tick.gif') !== -1,
hasDash = text === '-'; hasDash = text === '-';
@ -149,7 +141,5 @@ exports.fetch = function(req, res){
} }
} }
); );
}
);
}; };