Skip to content

Commit 93d2baf

Browse files
janetleekimdviramontes
authored andcommitted
feat: news.natgeo parser (#88)
* feat: natgeo parser For some reason, the local copy of the article didn’t grab the author name in it, so I couldn’t figure out how to parse it. The generic parser took a name of an author of a paper mentioned in the article, and thought that was the author name, which was funny. I cleaned a large block quote that didn’t make sense as it was shown in the preview, although I noticed that the Mercury chrome extension didn’t even display it. * fix: add date_published transform * fix: date_published assertion * disable: author assertion, generlize author selector * rm: author assertion * fix: image lead * fix: guard agaist missing img url * fix: generalize dek and title selectors
1 parent 2279c2d commit 93d2baf

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

Diff for: fixtures/news.nationalgeographic.com/1481919545107.html

+2
Large diffs are not rendered by default.

Diff for: src/extractors/custom/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export * from './www.reuters.com';
3737
export * from './mashable.com';
3838
export * from './www.chicagotribune.com';
3939
export * from './www.vox.com';
40+
export * from './news.nationalgeographic.com';
4041
export * from './www.nationalgeographic.com';
4142
export * from './www.latimes.com';
4243
export * from './pagesix.com';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export const NewsNationalgeographicComExtractor = {
2+
domain: 'news.nationalgeographic.com',
3+
4+
title: {
5+
selectors: [
6+
'h1',
7+
'h1.main-title',
8+
],
9+
},
10+
11+
author: {
12+
selectors: [
13+
'.byline-component__contributors b span',
14+
],
15+
},
16+
17+
date_published: {
18+
selectors: [
19+
['meta[name="article:published_time"]', 'value'],
20+
],
21+
format: 'ddd MMM DD HH:mm:ss zz YYYY',
22+
timezone: 'EST',
23+
},
24+
25+
dek: {
26+
selectors: [
27+
'.article__deck',
28+
],
29+
},
30+
31+
lead_image_url: {
32+
selectors: [
33+
['meta[name="og:image"]', 'value'],
34+
],
35+
},
36+
37+
content: {
38+
selectors: [
39+
['.parsys.content', '.__image-lead__'],
40+
'.content',
41+
],
42+
43+
// Is there anything in the content you selected that needs transformed
44+
// before it's consumable content? E.g., unusual lazy loaded images
45+
transforms: {
46+
'.parsys.content': ($node, $) => {
47+
const $imgSrc = $node.find('.image.parbase.section')
48+
.find('.picturefill')
49+
.first()
50+
.data('platform-src');
51+
if ($imgSrc) {
52+
$node.prepend($(`<img class="__image-lead__" src="${$imgSrc}"/>`));
53+
}
54+
},
55+
},
56+
57+
// Is there anything that is in the result that shouldn't be?
58+
// The clean selectors will remove anything that matches from
59+
// the result
60+
clean: [
61+
'.pull-quote.pull-quote--large',
62+
],
63+
},
64+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import assert from 'assert';
2+
import fs from 'fs';
3+
import URL from 'url';
4+
import cheerio from 'cheerio';
5+
6+
import Mercury from 'mercury';
7+
import getExtractor from 'extractors/get-extractor';
8+
import { excerptContent } from 'utils/text';
9+
10+
describe('NewsNationalgeographicComExtractor', () => {
11+
describe('initial test case', () => {
12+
let result;
13+
let url;
14+
beforeAll(() => {
15+
url =
16+
'http://news.nationalgeographic.com/energy/2015/08/150803-gender-bias-affects-office-heating-cooling-temperatures/';
17+
const html =
18+
fs.readFileSync('./fixtures/news.nationalgeographic.com/1481919545107.html');
19+
result =
20+
Mercury.parse(url, html, { fallback: false });
21+
});
22+
23+
it('is selected properly', () => {
24+
// This test should be passing by default.
25+
// It sanity checks that the correct parser
26+
// is being selected for URLs from this domain
27+
const extractor = getExtractor(url);
28+
assert.equal(extractor.domain, URL.parse(url).hostname);
29+
});
30+
31+
it('returns the title', async () => {
32+
// To pass this test, fill out the title selector
33+
// in ./src/extractors/custom/news.nationalgeographic.com/index.js.
34+
const { title } = await result;
35+
36+
// Update these values with the expected values from
37+
// the article.
38+
assert.equal(title, 'Here’s Why Your Office May Be Too Hot or Cold: Gender Bias');
39+
});
40+
41+
it('returns the date_published', async () => {
42+
// To pass this test, fill out the date_published selector
43+
// in ./src/extractors/custom/news.nationalgeographic.com/index.js.
44+
const { date_published } = await result;
45+
46+
// Update these values with the expected values from
47+
// the article.
48+
assert.equal(date_published, '2015-08-03T17:45:00.000Z');
49+
});
50+
51+
it('returns the dek', async () => {
52+
// To pass this test, fill out the dek selector
53+
// in ./src/extractors/custom/news.nationalgeographic.com/index.js.
54+
const { dek } = await result;
55+
56+
// Update these values with the expected values from
57+
// the article.
58+
assert.equal(dek, 'Do you argue about the temperature in your office or home? Find out what often decides it, and tell us your preference.');
59+
});
60+
61+
it('returns the lead_image_url', async () => {
62+
// To pass this test, fill out the lead_image_url selector
63+
// in ./src/extractors/custom/news.nationalgeographic.com/index.js.
64+
const { lead_image_url } = await result;
65+
66+
// Update these values with the expected values from
67+
// the article.
68+
assert.equal(lead_image_url, 'http://news.nationalgeographic.com/content/dam/news/2015/08/03/temperaturegenderbias/02tempgenderbias.ngsversion.1438795800319.jpg');
69+
});
70+
71+
it('returns the content', async () => {
72+
// To pass this test, fill out the content selector
73+
// in ./src/extractors/custom/news.nationalgeographic.com/index.js.
74+
// You may also want to make use of the clean and transform
75+
// options.
76+
const { content } = await result;
77+
78+
const $ = cheerio.load(content || '');
79+
80+
const first13 = excerptContent($('*').first().text(), 13);
81+
82+
// Update these values with the expected values from
83+
// the article.
84+
assert.equal(first13, 'Many couples fight about it at home. No, it’s not money, sex, or');
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)