From d91b4dccf440ced0e2909388a2efb93b7afa1db1 Mon Sep 17 00:00:00 2001 From: Carl Jolly Date: Wed, 16 May 2018 14:42:59 -0500 Subject: [PATCH] updated og:image parsing to handle og:image:url as well --- index.js | 6 ++ test/fixtures/fixtures.js | 4 +- test/fixtures/ogimage.html | 29 +++++++ test/fixtures/ogimageurl.html | 29 +++++++ test/test.js | 144 ++++++++++++++++++++-------------- 5 files changed, 153 insertions(+), 59 deletions(-) create mode 100644 test/fixtures/ogimage.html create mode 100644 test/fixtures/ogimageurl.html diff --git a/index.js b/index.js index 88828c0..197c8b3 100644 --- a/index.js +++ b/index.js @@ -238,6 +238,12 @@ MetaInspector.prototype.getImage = function() this.image = this.getAbsolutePath(img); } } + if (!this.image ) { + img = this.parsedDocument("meta[property='og:image:url']").attr("content"); + if (img){ + this.image = this.getAbsolutePath(img); + } + } return this; } diff --git a/test/fixtures/fixtures.js b/test/fixtures/fixtures.js index 31b74cd..659ce56 100644 --- a/test/fixtures/fixtures.js +++ b/test/fixtures/fixtures.js @@ -6,5 +6,7 @@ fakeweb.registerUri({uri: 'http://www.google.com:80/', file: path.join(__dirname fakeweb.registerUri({uri: 'http://www.simple.com:80/', file: path.join(__dirname, 'simple.com.html')}); fakeweb.registerUri({uri: 'http://www.fastandfurious7-film.com:80/', file: path.join(__dirname, 'fastandfurious7-film.com.html')}); fakeweb.registerUri({uri: 'http://www.techsuplex.com:80/', file: path.join(__dirname, 'techsuplex.com.html')}); +fakeweb.registerUri({uri: 'http://www.ogimageurl.com:80/', file: path.join(__dirname, 'ogimageurl.html')}); +fakeweb.registerUri({uri: 'http://www.ogimage.com:80/', file: path.join(__dirname, 'ogimageurl.html')}); -fakeweb.ignoreUri({uri: 'http://www.google-404.com:80/'}); +fakeweb.ignoreUri({uri: 'http://www.google-404.com:80/'}); \ No newline at end of file diff --git a/test/fixtures/ogimage.html b/test/fixtures/ogimage.html new file mode 100644 index 0000000..72c83f3 --- /dev/null +++ b/test/fixtures/ogimage.html @@ -0,0 +1,29 @@ + + +This is my title! + + + + + + + + + +
+Link Name +is a link to another nifty site +

This is a Header

+

This is a Medium Header

+Send me mail at +support@yourcompany.com. +

This is a new paragraph! This paragraph should be very long so we can grab it as the secondary description. What do you think of that?

+

This is a new paragraph!

+
This is a new sentence without a paragraph break, in bold italics. +I am a relative image +I am another relative image +This is an absolute image +this is ssl absolute image +this is another absolute image without a protocol + + diff --git a/test/fixtures/ogimageurl.html b/test/fixtures/ogimageurl.html new file mode 100644 index 0000000..d3fa885 --- /dev/null +++ b/test/fixtures/ogimageurl.html @@ -0,0 +1,29 @@ + + +This is my title! + + + + + + + + + +
+Link Name +is a link to another nifty site +

This is a Header

+

This is a Medium Header

+Send me mail at +support@yourcompany.com. +

This is a new paragraph! This paragraph should be very long so we can grab it as the secondary description. What do you think of that?

+

This is a new paragraph!

+
This is a new sentence without a paragraph break, in bold italics. +I am a relative image +I am another relative image +This is an absolute image +this is ssl absolute image +this is another absolute image without a protocol + + diff --git a/test/test.js b/test/test.js index b1c7d6c..58b5fd4 100644 --- a/test/test.js +++ b/test/test.js @@ -5,16 +5,16 @@ var MetaInspector = require('../index'), require('./fixtures/fixtures'); -describe('metainspector', function(){ +describe('metainspector', function () { - describe('multiple clients', function(){ + describe('multiple clients', function () { var firstClient = new MetaInspector('http://www.google.com'); var secondClient = new MetaInspector('http://www.google.com'); - it('should not keep the same eventEmitter reference among clients', function(done){ + it('should not keep the same eventEmitter reference among clients', function (done) { var calledOnce = false; - firstClient.on('fetch', function(){ + firstClient.on('fetch', function () { if (!calledOnce) { calledOnce = true; } else { @@ -22,7 +22,7 @@ describe('metainspector', function(){ } }); - secondClient.on('fetch', function(){ + secondClient.on('fetch', function () { should.exists(secondClient.parsedDocument); if (calledOnce) done(); @@ -33,76 +33,76 @@ describe('metainspector', function(){ }); }); - describe('client', function(){ + describe('client', function () { var client = null; - it('should have a url property', function(done){ + it('should have a url property', function (done) { client = new MetaInspector("http://www.google.com"); client.url.should.equal("http://www.google.com/"); done(); }); - it('should add http as the default scheme if no scheme is passed', function(done){ + it('should add http as the default scheme if no scheme is passed', function (done) { client = new MetaInspector("www.google.com"); client.url.should.equal("http://www.google.com/"); done(); }); - it('should have a scheme property', function(done){ + it('should have a scheme property', function (done) { client = new MetaInspector("http://www.google.com"); client.scheme.should.equal("http"); done(); }); - it('should have a host property', function(done){ + it('should have a host property', function (done) { client = new MetaInspector("http://www.google.com"); client.host.should.equal("www.google.com"); done(); }); - it('should not include port number in host', function(done){ + it('should not include port number in host', function (done) { client = new MetaInspector("http://www.google.com:8000"); client.host.should.equal("www.google.com"); done(); }); - it('should have a port property', function(done){ + it('should have a port property', function (done) { client = new MetaInspector("http://www.google.com:8000"); client.port.should.equal(8000); done(); }); - it('port should be undefined if not specified in original url', function(done){ + it('port should be undefined if not specified in original url', function (done) { client = new MetaInspector("http://www.google.com"); should.equal(client.port, undefined); done(); }); - it('should have a rootUrl property', function(done){ + it('should have a rootUrl property', function (done) { client = new MetaInspector("http://www.google.com"); client.rootUrl.should.equal("http://www.google.com"); done(); }); - it('should include port number in rootUrl if specified in original url', function(done){ + it('should include port number in rootUrl if specified in original url', function (done) { client = new MetaInspector("http://www.google.com:8000"); client.rootUrl.should.equal("http://www.google.com:8000"); done(); }); - it('should have a parsedDocument', function(done){ + it('should have a parsedDocument', function (done) { client = new MetaInspector("http://www.google.com"); - client.once("fetch", function(){ + client.once("fetch", function () { should.exists(client.parsedDocument); done(); }); @@ -110,10 +110,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have a title', function(done){ + it('should have a title', function (done) { client = new MetaInspector("http://www.google.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.title.should.equal("Google"); done(); }); @@ -121,21 +121,21 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have keywords', function(done){ + it('should have keywords', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ - client.keywords.should.be.instanceof(Array).and.be.eql([ 'HTML', 'CSS', 'XML', 'JavaScript' ]).and.have.lengthOf(4); + client.once("fetch", function () { + client.keywords.should.be.instanceof(Array).and.be.eql(['HTML', 'CSS', 'XML', 'JavaScript']).and.have.lengthOf(4); done(); }); client.fetch(); }); - it('keywords should be undefined if there is no keywords', function(done){ + it('keywords should be undefined if there is no keywords', function (done) { client = new MetaInspector("http://www.google.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.keywords.should.be.instanceof(Array).and.be.eql([]); done(); }); @@ -143,10 +143,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('author should be undefined if there is no author', function(done){ + it('author should be undefined if there is no author', function (done) { client = new MetaInspector("http://www.google.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { should.not.exist(client.author); done(); }); @@ -154,10 +154,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('charset should be undefined if there is no charset', function(done){ + it('charset should be undefined if there is no charset', function (done) { client = new MetaInspector("http://www.google.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { should.not.exist(client.charset); done(); }); @@ -165,10 +165,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have author', function(done){ + it('should have author', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.author.should.be.equal('Author Name'); done(); }); @@ -176,10 +176,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have charset', function(done){ + it('should have charset', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.charset.should.be.equal('UTF-8'); done(); }); @@ -187,10 +187,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have links returned as an array', function(done){ + it('should have links returned as an array', function (done) { client = new MetaInspector("http://www.google.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.links.length.should.equal(51); done(); }); @@ -198,10 +198,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have a description', function(done){ + it('should have a description', function (done) { client = new MetaInspector("http://www.google.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.description.should.equal("Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for."); done(); }); @@ -209,10 +209,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have a og:image with relative path and return as absolute', function(done){ + it('should have a og:image with relative path and return as absolute', function (done) { client = new MetaInspector("http://www.fastandfurious7-film.com"); - client.once("fetch", function(){ + client.once("fetch", function () { client.image.should.equal("http://www.fastandfurious7-film.com/images/fb.jpg"); done(); }); @@ -220,10 +220,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should have a og:description', function(done){ + it('should have a og:description', function (done) { client = new MetaInspector("http://www.fastandfurious7-film.com"); - client.once("fetch", function(){ + client.once("fetch", function () { client.ogDescription.should.equal("Continuing the global exploits in the unstoppable franchise built on speed, Vin Diesel, Paul Walker and Dwayne Johnson lead the returning cast of Fast & Furious 7."); done(); }); @@ -231,10 +231,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should return undefined if the meta description is not defined when metaDescription used', function(done){ + it('should return undefined if the meta description is not defined when metaDescription used', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { should.not.exist(client.metaDescription); done(); }); @@ -242,10 +242,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should find a secondary description if there is no description meta element', function(done){ + it('should find a secondary description if there is no description meta element', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.description.should.equal("This is a new paragraph! This paragraph should be very long so we can grab it as the secondary description. What do you think of that?"); done(); }); @@ -253,10 +253,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should find a the image based on the og:image tag if defined', function(done){ + it('should find a the image based on the og:image tag if defined', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.image.should.equal("http://placehold.it/350x150"); done(); }); @@ -281,10 +281,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should return an array of rss or atom feeds if defined', function(done){ + it('should return an array of rss or atom feeds if defined', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.feeds.length.should.equal(2); done(); }); @@ -292,10 +292,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should return the open graph title if defined', function(done){ + it('should return the open graph title if defined', function (done) { client = new MetaInspector("http://www.simple.com", {}); - client.once("fetch", function(){ + client.once("fetch", function () { client.ogTitle.should.equal("I am an Open Graph title"); done(); }); @@ -303,10 +303,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should emit errors', function(done){ + it('should emit errors', function (done) { client = new MetaInspector("http://www.google-404.com/", {}); - client.once("error", function(error){ + client.once("error", function (error) { should.exists(error); done(); }); @@ -314,10 +314,10 @@ describe('metainspector', function(){ client.fetch(); }); - it("should return the open graph type, if defined", function(done){ + it("should return the open graph type, if defined", function (done) { client = new MetaInspector("http://www.techsuplex.com", {}); - client.once("fetch", function() { + client.once("fetch", function () { client.ogType.should.exist; client.ogType.should.equal("article"); done(); @@ -326,10 +326,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should return the last updated time, if defined', function(done){ + it('should return the last updated time, if defined', function (done) { client = new MetaInspector("http://www.techsuplex.com", {}); - client.once("fetch", function() { + client.once("fetch", function () { client.ogUpdatedTime.should.exist; client.ogUpdatedTime.should.equal("2013-10-31T09:29:46+00:00"); done(); @@ -338,10 +338,10 @@ describe('metainspector', function(){ client.fetch(); }); - it('should return the open graph locale, if defined', function(done){ + it('should return the open graph locale, if defined', function (done) { client = new MetaInspector("http://www.techsuplex.com", {}); - client.once("fetch", function() { + client.once("fetch", function () { client.ogLocale.should.exist; client.ogLocale.should.equal("en_US"); done(); @@ -349,5 +349,33 @@ describe('metainspector', function(){ client.fetch(); }); + + it('should identify og image using og:image property', function (done) { + client = new MetaInspector("http://www.ogimage.com:80/", {}); + + client.once("fetch", function () { + client.image.should.equal("http://placehold.it/350x150"); + done(); + }); + client.on("error", (error) => { + console.error("errorxxxx:" + JSON.stringify(error)); + }); + + client.fetch(); + }); + + it('should identify og image using og:image:url property', function (done) { + client = new MetaInspector("http://www.ogimageurl.com:80/", {}); + + client.once("fetch", function () { + client.image.should.equal("http://placehold.it/350x150"); + done(); + }); + client.on("error", (error) => { + console.error("errorxxxx:" + JSON.stringify(error)); + }); + + client.fetch(); + }); }); });