mirror of
https://github.com/postlight/mercury-parser
synced 2024-11-17 03:25:31 +00:00
Merge pull request #13 from postlight/feat-apartmenttherapy-parser
feat: Add custom extrator for Apartment Therapy
This commit is contained in:
commit
94321111e9
61
dist/mercury.js
vendored
61
dist/mercury.js
vendored
@ -1034,6 +1034,64 @@ var BroadwayWorldExtractor = {
|
||||
}
|
||||
};
|
||||
|
||||
// Rename CustomExtractor
|
||||
// to fit your publication
|
||||
// (e.g., NYTimesExtractor)
|
||||
var ApartmentTherapyExtractor = {
|
||||
domain: 'www.apartmenttherapy.com',
|
||||
title: {
|
||||
selectors: ['h1.headline']
|
||||
},
|
||||
|
||||
author: {
|
||||
selectors: ['.PostByline__name']
|
||||
},
|
||||
|
||||
content: {
|
||||
selectors: ['div.post__content'],
|
||||
|
||||
// Is there anything in the content you selected that needs transformed
|
||||
// before it's consumable content? E.g., unusual lazy loaded images
|
||||
transforms: {
|
||||
'div[data-render-react-id="images/LazyPicture"]': function divDataRenderReactIdImagesLazyPicture($node, $) {
|
||||
var data = JSON.parse($node.attr('data-props'));
|
||||
var src = data.sources[0].src;
|
||||
var $img = $('<img />').attr('src', src);
|
||||
$node.replaceWith($img);
|
||||
}
|
||||
},
|
||||
|
||||
// Is there anything that is in the result that shouldn't be?
|
||||
// The clean selectors will remove anything that matches from
|
||||
// the result
|
||||
clean: []
|
||||
},
|
||||
|
||||
date_published: {
|
||||
selectors: [['.PostByline__timestamp[datetime]', 'datetime']]
|
||||
},
|
||||
|
||||
lead_image_url: {
|
||||
selectors: [['meta[name="og:image"]', 'value']]
|
||||
},
|
||||
|
||||
dek: {
|
||||
selectors: [['meta[name=description]', 'value']]
|
||||
},
|
||||
|
||||
next_page_url: {
|
||||
selectors: [
|
||||
// enter selectors
|
||||
]
|
||||
},
|
||||
|
||||
excerpt: {
|
||||
selectors: [
|
||||
// enter selectors
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
var Extractors = {
|
||||
'nymag.com': NYMagExtractor,
|
||||
'blogspot.com': BloggerExtractor,
|
||||
@ -1050,7 +1108,8 @@ var Extractors = {
|
||||
'www.littlethings.com': LittleThingsExtractor,
|
||||
'www.politico.com': PoliticoExtractor,
|
||||
'deadspin.com': DeadspinExtractor,
|
||||
'www.broadwayworld.com': BroadwayWorldExtractor
|
||||
'www.broadwayworld.com': BroadwayWorldExtractor,
|
||||
'www.apartmenttherapy.com': ApartmentTherapyExtractor
|
||||
};
|
||||
|
||||
// Spacer images to be removed
|
||||
|
2
dist/mercury.js.map
vendored
2
dist/mercury.js.map
vendored
File diff suppressed because one or more lines are too long
3
fixtures/www.apartmenttherapy.com/1476396697639.html
Normal file
3
fixtures/www.apartmenttherapy.com/1476396697639.html
Normal file
File diff suppressed because one or more lines are too long
@ -14,6 +14,7 @@ import { LittleThingsExtractor } from './custom/www.littlethings.com';
|
||||
import { PoliticoExtractor } from './custom/www.politico.com';
|
||||
import { DeadspinExtractor } from './custom/deadspin.com';
|
||||
import { BroadwayWorldExtractor } from './custom/www.broadwayworld.com';
|
||||
import { ApartmentTherapyExtractor } from './custom/www.apartmenttherapy.com';
|
||||
|
||||
const Extractors = {
|
||||
'nymag.com': NYMagExtractor,
|
||||
@ -32,6 +33,7 @@ const Extractors = {
|
||||
'www.politico.com': PoliticoExtractor,
|
||||
'deadspin.com': DeadspinExtractor,
|
||||
'www.broadwayworld.com': BroadwayWorldExtractor,
|
||||
'www.apartmenttherapy.com': ApartmentTherapyExtractor,
|
||||
};
|
||||
|
||||
export default Extractors;
|
||||
|
71
src/extractors/custom/www.apartmenttherapy.com/index.js
Normal file
71
src/extractors/custom/www.apartmenttherapy.com/index.js
Normal file
@ -0,0 +1,71 @@
|
||||
// Rename CustomExtractor
|
||||
// to fit your publication
|
||||
// (e.g., NYTimesExtractor)
|
||||
export const ApartmentTherapyExtractor = {
|
||||
domain: 'www.apartmenttherapy.com',
|
||||
title: {
|
||||
selectors: [
|
||||
'h1.headline',
|
||||
],
|
||||
},
|
||||
|
||||
author: {
|
||||
selectors: [
|
||||
'.PostByline__name',
|
||||
],
|
||||
},
|
||||
|
||||
content: {
|
||||
selectors: [
|
||||
'div.post__content',
|
||||
],
|
||||
|
||||
// Is there anything in the content you selected that needs transformed
|
||||
// before it's consumable content? E.g., unusual lazy loaded images
|
||||
transforms: {
|
||||
'div[data-render-react-id="images/LazyPicture"]': ($node, $) => {
|
||||
const data = JSON.parse($node.attr('data-props'));
|
||||
const { src } = data.sources[0];
|
||||
const $img = $('<img />').attr('src', src);
|
||||
$node.replaceWith($img);
|
||||
},
|
||||
},
|
||||
|
||||
// Is there anything that is in the result that shouldn't be?
|
||||
// The clean selectors will remove anything that matches from
|
||||
// the result
|
||||
clean: [
|
||||
|
||||
],
|
||||
},
|
||||
|
||||
date_published: {
|
||||
selectors: [
|
||||
['.PostByline__timestamp[datetime]', 'datetime'],
|
||||
],
|
||||
},
|
||||
|
||||
lead_image_url: {
|
||||
selectors: [
|
||||
['meta[name="og:image"]', 'value'],
|
||||
],
|
||||
},
|
||||
|
||||
dek: {
|
||||
selectors: [
|
||||
['meta[name=description]', 'value'],
|
||||
],
|
||||
},
|
||||
|
||||
next_page_url: {
|
||||
selectors: [
|
||||
// enter selectors
|
||||
],
|
||||
},
|
||||
|
||||
excerpt: {
|
||||
selectors: [
|
||||
// enter selectors
|
||||
],
|
||||
},
|
||||
};
|
130
src/extractors/custom/www.apartmenttherapy.com/index.test.js
Normal file
130
src/extractors/custom/www.apartmenttherapy.com/index.test.js
Normal file
@ -0,0 +1,130 @@
|
||||
import assert from 'assert';
|
||||
import fs from 'fs';
|
||||
import URL from 'url';
|
||||
import cheerio from 'cheerio';
|
||||
|
||||
import Mercury from 'mercury';
|
||||
import getExtractor from 'extractors/get-extractor';
|
||||
import { excerptContent } from 'utils/text';
|
||||
|
||||
// Rename CustomExtractor
|
||||
describe('CustomExtractor', () => {
|
||||
it('is selected properly', () => {
|
||||
// To pass this test, rename your extractor in
|
||||
// ./src/extractors/custom/www.apartmenttherapy.com/index.js
|
||||
// (e.g., CustomExtractor => NYTimesExtractor)
|
||||
// then add your new extractor to
|
||||
// src/extractors/all.js
|
||||
const url =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
const extractor = getExtractor(url);
|
||||
assert.equal(extractor.domain, URL.parse(url).hostname);
|
||||
});
|
||||
|
||||
it('returns the title', async () => {
|
||||
// To pass this test, fill out the title selector
|
||||
// in ./src/extractors/custom/www.apartmenttherapy.com/index.js.
|
||||
const html =
|
||||
fs.readFileSync('./fixtures/www.apartmenttherapy.com/1476396697639.html');
|
||||
const articleUrl =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
|
||||
const { title } =
|
||||
await Mercury.parse(articleUrl, html, { fallback: false });
|
||||
|
||||
// Update these values with the expected values from
|
||||
// the article.
|
||||
assert.equal(title, 'A Light Filled LA Loft');
|
||||
});
|
||||
|
||||
|
||||
it('returns the author', async () => {
|
||||
// To pass this test, fill out the author selector
|
||||
// in ./src/extractors/custom/www.apartmenttherapy.com/index.js.
|
||||
const html =
|
||||
fs.readFileSync('./fixtures/www.apartmenttherapy.com/1476396697639.html');
|
||||
const articleUrl =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
|
||||
const { author } =
|
||||
await Mercury.parse(articleUrl, html, { fallback: false });
|
||||
|
||||
// Update these values with the expected values from
|
||||
// the article.
|
||||
assert.equal(author, 'Apartment Therapy Submissions');
|
||||
});
|
||||
|
||||
|
||||
it('returns the date_published', async () => {
|
||||
// To pass this test, fill out the date_published selector
|
||||
// in ./src/extractors/custom/www.apartmenttherapy.com/index.js.
|
||||
const html =
|
||||
fs.readFileSync('./fixtures/www.apartmenttherapy.com/1476396697639.html');
|
||||
const articleUrl =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
|
||||
const { date_published } =
|
||||
await Mercury.parse(articleUrl, html, { fallback: false });
|
||||
|
||||
// Update these values with the expected values from
|
||||
// the article.
|
||||
assert.equal(date_published, '2016-10-13T21:00:00.000Z');
|
||||
});
|
||||
|
||||
|
||||
it('returns the dek', async () => {
|
||||
// To pass this test, fill out the dek selector
|
||||
// in ./src/extractors/custom/www.apartmenttherapy.com/index.js.
|
||||
const html =
|
||||
fs.readFileSync('./fixtures/www.apartmenttherapy.com/1476396697639.html');
|
||||
const articleUrl =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
|
||||
const { dek } =
|
||||
await Mercury.parse(articleUrl, html, { fallback: false });
|
||||
|
||||
// Update these values with the expected values from
|
||||
// the article.
|
||||
assert.equal(dek, "Name: Ashley Location: Downtown — Los Angeles, California Welcome to our sunny and spacious downtown home located in the in the heart of Downtown LA's Historic Core. Inside you'll find a 1,300 square foot bi-level ground unit with loft (only three of its kind!) that offers an unparalleled, refined industrial, modern aesthetic.");
|
||||
});
|
||||
|
||||
|
||||
it('returns the lead_image_url', async () => {
|
||||
// To pass this test, fill out the lead_image_url selector
|
||||
// in ./src/extractors/custom/www.apartmenttherapy.com/index.js.
|
||||
const html =
|
||||
fs.readFileSync('./fixtures/www.apartmenttherapy.com/1476396697639.html');
|
||||
const articleUrl =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
|
||||
const { lead_image_url } =
|
||||
await Mercury.parse(articleUrl, html, { fallback: false });
|
||||
|
||||
// Update these values with the expected values from
|
||||
// the article.
|
||||
assert.equal(lead_image_url, 'http://atmedia.imgix.net/9332fdca908b1fcc5c9a6891b458820718239950?w=1500&fit=max');
|
||||
});
|
||||
|
||||
|
||||
it('returns the content', async () => {
|
||||
// To pass this test, fill out the content selector
|
||||
// in ./src/extractors/custom/www.apartmenttherapy.com/index.js.
|
||||
// You may also want to make use of the clean and transform
|
||||
// options.
|
||||
const html =
|
||||
fs.readFileSync('./fixtures/www.apartmenttherapy.com/1476396697639.html');
|
||||
const url =
|
||||
'http://www.apartmenttherapy.com/a-light-filled-la-loft-236564';
|
||||
|
||||
const { content } =
|
||||
await Mercury.parse(url, html, { fallback: false });
|
||||
|
||||
const $ = cheerio.load(content || '');
|
||||
|
||||
const first13 = excerptContent($('*').first().text(), 13);
|
||||
|
||||
// Update these values with the expected values from
|
||||
// the article.
|
||||
assert.equal(first13, 'Name: Ashley Location: Downtown — Los Angeles, California Welcome to our sunny and');
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user