Skip to content

Commit 3cf2d0d

Browse files
janetleekimkev5873
authored andcommitted
feat: msnbc parser (#100)
* feat: msnbc parser
1 parent f9ab9eb commit 3cf2d0d

4 files changed

Lines changed: 148 additions & 0 deletions

File tree

fixtures/www.msnbc.com/1482261084088.html

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/extractors/custom/index.js

Lines changed: 1 addition & 0 deletions
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 './www.msnbc.com';
4041
export * from './www.thepoliticalinsider.com';
4142
export * from './www.mentalfloss.com';
4243
export * from './abcnews.go.com';
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export const WwwMsnbcComExtractor = {
2+
domain: 'www.msnbc.com',
3+
4+
title: {
5+
selectors: [
6+
'h1',
7+
'h1.is-title-pane',
8+
],
9+
},
10+
11+
author: {
12+
selectors: [
13+
'.author',
14+
],
15+
},
16+
17+
date_published: {
18+
selectors: [
19+
['meta[name="DC.date.issued"]', 'value'],
20+
],
21+
},
22+
23+
dek: {
24+
selectors: [
25+
['meta[name="description"]', 'value'],
26+
],
27+
},
28+
29+
lead_image_url: {
30+
selectors: [
31+
['meta[name="og:image"]', 'value'],
32+
],
33+
},
34+
35+
content: {
36+
selectors: [
37+
'.pane-node-body',
38+
],
39+
40+
// Is there anything in the content you selected that needs transformed
41+
// before it's consumable content? E.g., unusual lazy loaded images
42+
transforms: {
43+
'.pane-node-body': ($node, $) => {
44+
const [selector, attr] = WwwMsnbcComExtractor.lead_image_url.selectors[0];
45+
const src = $(selector).attr(attr);
46+
if (src) {
47+
$node.prepend(`<img src="${src}" />`);
48+
}
49+
},
50+
},
51+
52+
// Is there anything that is in the result that shouldn't be?
53+
// The clean selectors will remove anything that matches from
54+
// the result
55+
clean: [
56+
57+
],
58+
},
59+
};
Lines changed: 87 additions & 0 deletions
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('WwwMsnbcComExtractor', () => {
11+
describe('initial test case', () => {
12+
let result;
13+
let url;
14+
beforeAll(() => {
15+
url =
16+
'http://www.msnbc.com/rachel-maddow-show/bizarre-election-ends-bizarre-electoral-college-tally';
17+
const html =
18+
fs.readFileSync('./fixtures/www.msnbc.com/1482261084088.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/www.msnbc.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, 'A bizarre election ends with a bizarre Electoral College tally');
39+
});
40+
41+
it('returns the author', async () => {
42+
// To pass this test, fill out the author selector
43+
// in ./src/extractors/custom/www.msnbc.com/index.js.
44+
const { author } = await result;
45+
46+
// Update these values with the expected values from
47+
// the article.
48+
assert.equal(author, 'Steve Benen');
49+
});
50+
51+
it('returns the date_published', async () => {
52+
// To pass this test, fill out the date_published selector
53+
// in ./src/extractors/custom/www.msnbc.com/index.js.
54+
const { date_published } = await result;
55+
56+
// Update these values with the expected values from
57+
// the article.
58+
assert.equal(date_published, '2016-12-20T13:00:06.000Z');
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/www.msnbc.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://www.msnbc.com/sites/msnbc/files/styles/ratio--1_91-1--1200x630/public/trump.jpeg-99fbe.jpg?itok=2azRDj-y');
69+
});
70+
71+
it('returns the content', async () => {
72+
// To pass this test, fill out the content selector
73+
// in ./src/extractors/custom/www.msnbc.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, 'On Election Day, Donald Trump earned 306 electoral votes to Hillary Clinton’s 232,');
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)