Skip to content

Commit dec49ab

Browse files
janetleekimadampash
authored andcommitted
feat: mashable parser (#76)
* feat: mashable parser As usual the date is giving me issues because of formatting discrepancies: AssertionError: '2016-12-13T22:33:06.000Z' == '2016-12-14T03:33:06.000Z' Not sure how we wanna deal with Twitter card embeds that don’t show up? Also, image credits did not show up in preview. * test: fixed date format * transforming .image-credit to figcaption
1 parent cddc1af commit dec49ab

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

fixtures/mashable.com/1481670648585.html

Lines changed: 16 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
@@ -29,6 +29,7 @@ export * from './www.theguardian.com';
2929
export * from './www.sbnation.com';
3030
export * from './www.bloomberg.com';
3131
export * from './www.bustle.com';
32+
export * from './mashable.com';
3233
export * from './www.chicagotribune.com';
3334
export * from './www.vox.com';
3435
export * from './www.cnbc.com';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export const MashableComExtractor = {
2+
domain: 'mashable.com',
3+
4+
title: {
5+
selectors: [
6+
'h1.title',
7+
],
8+
},
9+
10+
author: {
11+
selectors: [
12+
'span.author_name a',
13+
],
14+
},
15+
16+
date_published: {
17+
selectors: [
18+
['meta[name="og:article:published_time"]', 'value'],
19+
],
20+
},
21+
22+
lead_image_url: {
23+
selectors: [
24+
['meta[name="og:image"]', 'value'],
25+
],
26+
},
27+
28+
content: {
29+
selectors: [
30+
'section.article-content.blueprint',
31+
],
32+
33+
// Is there anything in the content you selected that needs transformed
34+
// before it's consumable content? E.g., unusual lazy loaded images
35+
transforms: {
36+
'.image-credit': 'figcaption',
37+
},
38+
39+
// Is there anything that is in the result that shouldn't be?
40+
// The clean selectors will remove anything that matches from
41+
// the result
42+
clean: [
43+
44+
],
45+
},
46+
};
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('MashableComExtractor', () => {
11+
describe('initial test case', () => {
12+
let result;
13+
let url;
14+
beforeAll(() => {
15+
url =
16+
'http://mashable.com/2016/12/13/mysterious-plane-flying-over-new-york/?utm_cid=hp-n-1#sxBI1HiPKsqG';
17+
const html =
18+
fs.readFileSync('./fixtures/mashable.com/1481670648585.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/mashable.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, 'Mysterious plane circling Manhattan sparks concern and intrigue');
39+
});
40+
41+
it('returns the author', async () => {
42+
// To pass this test, fill out the author selector
43+
// in ./src/extractors/custom/mashable.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, 'Nicole Gallucci');
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/mashable.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-13T22:33: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/mashable.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://a.amz.mshcdn.com/media/ZgkyMDE2LzEyLzEzL2UxL2xpbGlzYW1zbWFzaGFibGU1XzcyMC4wMWZkOS5qcGcKcAl0aHVtYgkxMjAweDYzMAplCWpwZw/29e123a7/0e0/lili-sams-mashable-5_720.jpg');
69+
});
70+
71+
it('returns the content', async () => {
72+
// To pass this test, fill out the content selector
73+
// in ./src/extractors/custom/mashable.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, 'A large military-style plane, which looked remarkably like a C-130, circled Manhattan for');
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)