diff --git a/index.js b/index.js index 64800eb..c6b9104 100644 --- a/index.js +++ b/index.js @@ -33,39 +33,20 @@ function driver(options, fn) { return function phantom_driver(ctx, done) { debug('going to %s', ctx.url); - nightmare - .on('error', error) - .on('timeout', function(timeout) { - return done(new Error(timeout)); - }) - .on('resourceReceived', function(resource) { - if (normalize(resource.url) == normalize(ctx.url)) { - debug('got response from %s: %s', resource.url, resource.status); - ctx.status = resource.status; - }; - }) - .on('urlChanged', function(url) { - debug('redirect: %s', url); - ctx.url = url; - }) - - wrapfn(fn, select)(ctx, nightmare); - - function select(err, ret) { - if (err) return done(err); - - nightmare + fn(ctx, nightmare) .evaluate(function() { - return document.documentElement.outerHTML; - }, function(body) { - ctx.body = body; + return document.documentElement.outerHTML; + }) + .end() + .then(function (body) { + debug('got response from %s, content length: %s', ctx.url, (body || '').length); + ctx.body = body; + done(null, ctx); }) - .run(function(err) { - if (err) return done(err); - debug('%s - %s', ctx.url, ctx.status); - done(null, ctx); + .catch(function (err) { + debug('nightmare error', err); + if (err) return done(err); }); - }; } } diff --git a/package.json b/package.json index 111c6f6..d02d34a 100644 --- a/package.json +++ b/package.json @@ -10,15 +10,14 @@ }, "dependencies": { "debug": "^2.1.1", - "nightmare": "^1.7.0", + "nightmare": "^2.5.2", "normalizeurl": "^0.1.3", "wrap-fn": "^0.1.4" }, "devDependencies": { "cheerio": "^0.19.0", "mocha": "*", - "nightmare-swiftly": "^0.2.4", "x-ray-crawler": "^2.0.1" }, "main": "index" -} \ No newline at end of file +} diff --git a/test.js b/test.js deleted file mode 100644 index 2b079c9..0000000 --- a/test.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Module Dependencies - */ - -var crawler = require('x-ray-crawler'); -var driver = require('./'); - -/** - * Setup the driver - */ - -function *phantom(ctx, nightmare) { - nightmare.goto(ctx.url) - return nightmare; -} - -/** - * Crawl google - */ - -crawler('http://google.com') - .driver(driver(phantom)) - .on('response', function($, ctx) { - console.log('%s - %s', ctx.status, $('title').text().trim()); - }) - .crawl(function(err) { - if (err) throw err; - }) diff --git a/test/phantom.js b/test/phantom.js index 6eed8ef..2cd366c 100644 --- a/test/phantom.js +++ b/test/phantom.js @@ -16,8 +16,7 @@ var fs = require('fs'); describe('phantom driver', function() { it('should have sensible defaults', function(done) { - var crawler = Crawler() - .driver(phantom()) + var crawler = Crawler(phantom()); crawler('http://google.com', function(err, ctx) { if (err) return done(err); @@ -28,9 +27,8 @@ describe('phantom driver', function() { }) }); - it('should work with client-side pages', function(done) { - var crawler = Crawler() - .driver(phantom()); + it.skip('should work with client-side pages', function(done) { + var crawler = Crawler(phantom()); crawler('https://exchange.coinbase.com/trade', function(err, ctx) { if (err) return done(err); @@ -39,28 +37,27 @@ describe('phantom driver', function() { assert.equal(false, isNaN(+price)); done(); }) - }) + }); it('should support custom functions', function(done) { - var crawler = Crawler() - .driver(phantom(runner)); + var crawler = Crawler(phantom(runner)); - crawler('http://mat.io', function(err, ctx) { + crawler('http://www.npmjs.com', function(err, ctx) { if (err) return done(err); var $ = cheerio.load(ctx.body); var title = $('title').text(); - assert.equal('Lapwing Labs', title); + assert.equal('npm Documentation', title); done(); - }) + }); function runner(ctx, nightmare) { return nightmare - .goto(ctx.url) - .click('.Header-logo-item+ .Header-list-item a') - .wait() + .goto(ctx.url) + .click('#nav-docs-link') + .wait() } - }) -}) + }); +}); /** * Read