Skip to content

Commit e7e41bd

Browse files
janetleekimadampash
authored andcommitted
feat: the guardian custom extractor (#41)
1 parent 332f859 commit e7e41bd

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

src/extractors/custom/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export * from './www.theverge.com';
2525
export * from './www.cnn.com';
2626
export * from './www.aol.com';
2727
export * from './www.youtube.com';
28+
export * from './www.theguardian.com';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export const WwwTheguardianComExtractor = {
2+
domain: 'www.theguardian.com',
3+
4+
title: {
5+
selectors: [
6+
'.content__headline',
7+
],
8+
},
9+
10+
author: {
11+
selectors: [
12+
'p.byline',
13+
],
14+
},
15+
16+
date_published: {
17+
selectors: [
18+
['meta[name="article:published_time"]', 'value'],
19+
],
20+
},
21+
22+
dek: {
23+
selectors: [
24+
'.content__standfirst',
25+
],
26+
},
27+
28+
lead_image_url: {
29+
selectors: [
30+
['meta[name="og:image"]', 'value'],
31+
],
32+
},
33+
34+
content: {
35+
selectors: [
36+
'.content__article-body',
37+
],
38+
39+
// Is there anything in the content you selected that needs transformed
40+
// before it's consumable content? E.g., unusual lazy loaded images
41+
transforms: {
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+
'.hide-on-mobile',
49+
'.inline-icon',
50+
],
51+
},
52+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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('WwwTheguardianComExtractor', () => {
11+
it('is selected properly', () => {
12+
// This test should be passing by default.
13+
// It sanity checks that the correct parser
14+
// is being selected for URLs from this domain
15+
const url =
16+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
17+
const extractor = getExtractor(url);
18+
assert.equal(extractor.domain, URL.parse(url).hostname);
19+
});
20+
21+
it('returns the title', async () => {
22+
// To pass this test, fill out the title selector
23+
// in ./src/extractors/custom/www.theguardian.com/index.js.
24+
const html =
25+
fs.readFileSync('./fixtures/www.theguardian.com/1480457558008.html');
26+
const articleUrl =
27+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
28+
29+
const { title } =
30+
await Mercury.parse(articleUrl, html, { fallback: false });
31+
32+
// Update these values with the expected values from
33+
// the article.
34+
assert.equal(title, 'Standing Rock protesters hold out against extraordinary police violence');
35+
});
36+
37+
it('returns the author', async () => {
38+
// To pass this test, fill out the author selector
39+
// in ./src/extractors/custom/www.theguardian.com/index.js.
40+
const html =
41+
fs.readFileSync('./fixtures/www.theguardian.com/1480457558008.html');
42+
const articleUrl =
43+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
44+
45+
const { author } =
46+
await Mercury.parse(articleUrl, html, { fallback: false });
47+
48+
// Update these values with the expected values from
49+
// the article.
50+
assert.equal(author, 'Julia Carrie Wong and Sam Levin');
51+
});
52+
53+
it('returns the date_published', async () => {
54+
// To pass this test, fill out the date_published selector
55+
// in ./src/extractors/custom/www.theguardian.com/index.js.
56+
const html =
57+
fs.readFileSync('./fixtures/www.theguardian.com/1480457558008.html');
58+
const articleUrl =
59+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
60+
61+
const { date_published } =
62+
await Mercury.parse(articleUrl, html, { fallback: false });
63+
64+
// Update these values with the expected values from
65+
// the article.
66+
assert.equal(date_published, '2016-11-29T20:26:06.000Z');
67+
});
68+
69+
it('returns the dek', async () => {
70+
// To pass this test, fill out the dek selector
71+
// in ./src/extractors/custom/www.theguardian.com/index.js.
72+
const html =
73+
fs.readFileSync('./fixtures/www.theguardian.com/1480457558008.html');
74+
const articleUrl =
75+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
76+
77+
const { dek } =
78+
await Mercury.parse(articleUrl, html, { fallback: false });
79+
80+
// Update these values with the expected values from
81+
// the article.
82+
assert.equal(dek, 'Apprehension and distrust pervade North Dakota protest site as promises from state that there are no plans to forcibly remove people does little to assuage fears');
83+
});
84+
85+
it('returns the lead_image_url', async () => {
86+
// To pass this test, fill out the lead_image_url selector
87+
// in ./src/extractors/custom/www.theguardian.com/index.js.
88+
const html =
89+
fs.readFileSync('./fixtures/www.theguardian.com/1480457558008.html');
90+
const articleUrl =
91+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
92+
93+
const { lead_image_url } =
94+
await Mercury.parse(articleUrl, html, { fallback: false });
95+
96+
// Update these values with the expected values from
97+
// the article.
98+
assert.equal(lead_image_url, 'https://i.guim.co.uk/img/media/fc64982c5ab7b3ea7d5d38f3f74483c84c60b372/0_225_3500_2100/master/3500.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxNi8wNS8yNS9vdmVybGF5LWxvZ28tMTIwMC05MF9vcHQucG5n&s=034d7b3573a47c5ba3501a9e9e253a8f');
99+
});
100+
101+
it('returns the content', async () => {
102+
// To pass this test, fill out the content selector
103+
// in ./src/extractors/custom/www.theguardian.com/index.js.
104+
// You may also want to make use of the clean and transform
105+
// options.
106+
const html =
107+
fs.readFileSync('./fixtures/www.theguardian.com/1480457558008.html');
108+
const url =
109+
'https://www.theguardian.com/us-news/2016/nov/29/standing-rock-protest-north-dakota-shutdown-evacuation';
110+
111+
const { content } =
112+
await Mercury.parse(url, html, { fallback: false });
113+
114+
const $ = cheerio.load(content || '');
115+
116+
const first13 = excerptContent($('*').first().text(), 13);
117+
118+
// Update these values with the expected values from
119+
// the article.
120+
assert.equal(first13, 'Police violence against Standing Rock protesters in North Dakota has risen to extraordinary');
121+
});
122+
});

0 commit comments

Comments
 (0)