Skip to content

Commit 9e5b91e

Browse files
janetleekimadampash
authored andcommitted
feat: refinery29 parser (#71)
1 parent b78c58c commit 9e5b91e

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

Diff for: fixtures/www.refinery29.com/1481661863250.html

+1
Large diffs are not rendered by default.

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

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ export * from './247sports.com';
3737
export * from './uproxx.com';
3838
export * from './www.eonline.com';
3939
export * from './www.miamiherald.com';
40+
export * from './www.refinery29.com';

Diff for: src/extractors/custom/www.refinery29.com/index.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export const WwwRefinery29ComExtractor = {
2+
domain: 'www.refinery29.com',
3+
4+
title: {
5+
selectors: [
6+
'h1.title',
7+
],
8+
},
9+
10+
author: {
11+
selectors: [
12+
'.contributor',
13+
],
14+
},
15+
16+
date_published: {
17+
selectors: [
18+
['meta[name="sailthru.date"]', 'value'],
19+
],
20+
21+
timezone: 'America/New_York',
22+
},
23+
24+
lead_image_url: {
25+
selectors: [
26+
['meta[name="og:image"]', 'value'],
27+
],
28+
},
29+
30+
content: {
31+
selectors: [
32+
['.full-width-opener', '.article-content'],
33+
'.article-content',
34+
'.body',
35+
],
36+
37+
// Is there anything in the content you selected that needs transformed
38+
// before it's consumable content? E.g., unusual lazy loaded images
39+
transforms: {
40+
'div.loading noscript': ($node) => {
41+
const imgHtml = $node.html();
42+
$node.parents('.loading').replaceWith(imgHtml);
43+
},
44+
45+
'.section-image': 'figure',
46+
47+
'.section-image .content-caption': 'figcaption',
48+
49+
'.section-text': 'p',
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+
'.story-share',
57+
],
58+
},
59+
};
+87
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('WwwRefinery29ComExtractor', () => {
11+
describe('initial test case', () => {
12+
let result;
13+
let url;
14+
beforeAll(() => {
15+
url =
16+
'http://www.refinery29.com/2016/12/132377/graphic-tees-statement-shirt-style-tips';
17+
const html =
18+
fs.readFileSync('./fixtures/www.refinery29.com/1481661863250.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.refinery29.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, 'For Holiday Parties This Year, Let\'s Get Behind The Conversation-Starter Tee');
39+
});
40+
41+
it('returns the author', async () => {
42+
// To pass this test, fill out the author selector
43+
// in ./src/extractors/custom/www.refinery29.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, 'Connie Wang');
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.refinery29.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-13T01:00:00.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.refinery29.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://s3.r29static.com//bin/entry/fc5/0,213,2000,1050/x,80/1708221/image.jpg');
69+
});
70+
71+
it('returns the content', async () => {
72+
// To pass this test, fill out the content selector
73+
// in ./src/extractors/custom/www.refinery29.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, 'In a year when so much (too much?) happened that we\'re all still');
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)