Skip to content

Commit 9a961aa

Browse files
jbraytonjohnholdun
andauthored
feat: Add a custom extractor for www.ndtv.com. (#554)
* feat:Add a custom extractor for ma.ttias.be. When parsing content for cron.weekly issues, such as the one at https://ma.ttias.be/cronweekly/issue-130/, Mercury Parser would remove headings and ordered lists that were part of the content. This resolves that as follows: * Remove "id" attributes from "h1" and "h2" elements. Those attributes would result in the elements having a low weight. * Since Mercury Parser demotes "h1" elements to "h2", demote "h2" elements to "h3". * Add class="entry-content-asset" to "ul" elements to avoid them being removed. * removed redundant comment. * feat: Add a custom extractor for engadget.com. * feat: Add a custom extractor for www.ndtv.com. * Works, but I need to figure how to make pagination work correctly. * fixed pagination - would only retrieve first or second page because we would send contentOnly: true on subsequent pages (page 2). removed failover: true from preview. * rolled back { fallback: false } option removal * Clarified comments. * rolling back yarn.lock changes Co-authored-by: John Holdun <[email protected]>
1 parent 143631b commit 9a961aa

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

fixtures/www.ndtv.com/1587821636077.html

Lines changed: 17 additions & 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
@@ -140,3 +140,4 @@ export * from './www.abendblatt.de';
140140
export * from './www.gruene.de';
141141
export * from './www.engadget.com';
142142
export * from './arstechnica.com';
143+
export * from './www.ndtv.com';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export const WwwNdtvComExtractor = {
2+
domain: 'www.ndtv.com',
3+
4+
title: {
5+
selectors: [['meta[name="og:title"]', 'value'], 'h1.entry-title'],
6+
},
7+
8+
author: {
9+
selectors: ['span[itemprop="author"] span[itemprop="name"]'],
10+
},
11+
12+
date_published: {
13+
selectors: [['span[itemprop="dateModified"]', 'content']],
14+
},
15+
16+
dek: {
17+
selectors: ['h2'],
18+
},
19+
20+
lead_image_url: {
21+
selectors: [['meta[name="og:image"]', 'value']],
22+
},
23+
24+
content: {
25+
selectors: ['div[itemprop="articleBody"]'],
26+
27+
// Is there anything in the content you selected that needs transformed
28+
// before it's consumable content? E.g., unusual lazy loaded images
29+
transforms: {
30+
// This site puts a dateline in a 'b' above the first paragraph, and then somehow
31+
// blends it into the first paragraph with CSS. This transform moves the dateline
32+
// to the first paragraph.
33+
'.place_cont': $node => {
34+
if (!$node.parents('p').length) {
35+
const nextSibling = $node.next('p');
36+
if (nextSibling) {
37+
$node.remove();
38+
nextSibling.prepend($node);
39+
}
40+
}
41+
},
42+
},
43+
44+
// Is there anything that is in the result that shouldn't be?
45+
// The clean selectors will remove anything that matches from
46+
// the result
47+
clean: [
48+
'.highlghts_Wdgt',
49+
'.ins_instory_dv_caption',
50+
'input',
51+
'._world-wrapper .mt20',
52+
],
53+
},
54+
};
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import assert from 'assert';
2+
import URL from 'url';
3+
import cheerio from 'cheerio';
4+
5+
import Mercury from 'mercury';
6+
import getExtractor from 'extractors/get-extractor';
7+
import { excerptContent } from 'utils/text';
8+
9+
const fs = require('fs');
10+
11+
describe('WwwNdtvComExtractor', () => {
12+
describe('initial test case', () => {
13+
let result;
14+
let url;
15+
beforeAll(() => {
16+
url =
17+
'https://www.ndtv.com/india-news/coronavirus-us-president-donald-trump-says-there-may-be-retaliation-if-india-doesnt-clear-export-of-2207327';
18+
const html = fs.readFileSync(
19+
'./fixtures/www.ndtv.com/1587821636077.html'
20+
);
21+
result = Mercury.parse(url, { html, fallback: false });
22+
});
23+
24+
it('is selected properly', () => {
25+
// This test should be passing by default.
26+
// It sanity checks that the correct parser
27+
// is being selected for URLs from this domain
28+
const extractor = getExtractor(url);
29+
assert.equal(extractor.domain, URL.parse(url).hostname);
30+
});
31+
32+
it('returns the title', async () => {
33+
// To pass this test, fill out the title selector
34+
// in ./src/extractors/custom/www.ndtv.com/index.js.
35+
const { title } = await result;
36+
37+
// Update these values with the expected values from
38+
// the article.
39+
assert.equal(
40+
title,
41+
`COVID-19: Trump Talks "Retaliation" If India Rejects Export Of Key Drug`
42+
);
43+
});
44+
45+
it('returns the author', async () => {
46+
// To pass this test, fill out the author selector
47+
// in ./src/extractors/custom/www.ndtv.com/index.js.
48+
const { author } = await result;
49+
50+
// Update these values with the expected values from
51+
// the article.
52+
assert.equal(author, 'Swati Bhasin');
53+
});
54+
55+
it('returns the date_published', async () => {
56+
// To pass this test, fill out the date_published selector
57+
// in ./src/extractors/custom/www.ndtv.com/index.js.
58+
const { date_published } = await result;
59+
60+
// Update these values with the expected values from
61+
// the article.
62+
assert.equal(date_published, '2020-04-07T10:19:34.000Z');
63+
});
64+
65+
it('returns the dek', async () => {
66+
// To pass this test, fill out the dek selector
67+
// in ./src/extractors/custom/www.ndtv.com/index.js.
68+
const { dek } = await result;
69+
70+
// Update these values with the expected values from
71+
// the article.
72+
assert.equal(
73+
dek,
74+
'Amid rising pressure, the government is likely to take a decision on the matter today and clear the move after calculating sufficient stocks for the country, sources have told NDTV.'
75+
);
76+
});
77+
78+
it('returns the lead_image_url', async () => {
79+
// To pass this test, fill out the lead_image_url selector
80+
// in ./src/extractors/custom/www.ndtv.com/index.js.
81+
const { lead_image_url } = await result;
82+
83+
// Update these values with the expected values from
84+
// the article.
85+
assert.equal(
86+
lead_image_url,
87+
`https://c.ndtvimg.com/2020-04/u9vkhue_donald-trump-white-house-afp_625x300_04_April_20.jpg`
88+
);
89+
});
90+
91+
it('returns the content', async () => {
92+
// To pass this test, fill out the content selector
93+
// in ./src/extractors/custom/www.ndtv.com/index.js.
94+
// You may also want to make use of the clean and transform
95+
// options.
96+
const { content } = await result;
97+
98+
const $ = cheerio.load(content || '');
99+
100+
const first13 = excerptContent(
101+
$('*')
102+
.first()
103+
.text(),
104+
13
105+
);
106+
107+
// Update these values with the expected values from
108+
// the article.
109+
assert.equal(
110+
first13,
111+
'Washington/ New Delhi: US President Donald Trump has said "there may be retaliation"'
112+
);
113+
114+
// Confirm that the dateline is moved.
115+
const dateline = $('.place_cont');
116+
assert.equal(dateline.length, 1);
117+
assert.equal(dateline.get(0).parent.tagName, 'p');
118+
});
119+
});
120+
});

0 commit comments

Comments
 (0)