Skip to content

Commit cd245f7

Browse files
janetleekimzacharygolba
authored andcommitted
feat: popsugar parser (#93)
I think this one is good to go!
1 parent a8ab713 commit cd245f7

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

Diff for: fixtures/www.popsugar.com/1482182390796.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
@@ -30,6 +30,7 @@ export * from './www.sbnation.com';
3030
export * from './www.bloomberg.com';
3131
export * from './www.bustle.com';
3232
export * from './www.vox.com';
33+
export * from './www.popsugar.com';
3334
export * from './observer.com';
3435
export * from './people.com';
3536
export * from './www.usmagazine.com';

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

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export const WwwPopsugarComExtractor = {
2+
domain: 'www.popsugar.com',
3+
4+
title: {
5+
selectors: [
6+
'h2.post-title',
7+
'title-text',
8+
],
9+
},
10+
11+
author: {
12+
selectors: [
13+
['meta[name="article:author"]', 'value'],
14+
],
15+
},
16+
17+
date_published: {
18+
selectors: [
19+
['meta[name="article:published_time"]', 'value'],
20+
],
21+
},
22+
23+
lead_image_url: {
24+
selectors: [
25+
['meta[name="og:image"]', 'value'],
26+
],
27+
},
28+
29+
content: {
30+
selectors: [
31+
'#content',
32+
],
33+
34+
// Is there anything in the content you selected that needs transformed
35+
// before it's consumable content? E.g., unusual lazy loaded images
36+
transforms: {
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+
'.share-copy-title',
44+
'.post-tags',
45+
'.reactions',
46+
],
47+
},
48+
};

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

+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('WwwPopsugarComExtractor', () => {
11+
describe('initial test case', () => {
12+
let result;
13+
let url;
14+
beforeAll(() => {
15+
url =
16+
'http://www.popsugar.com/home/Millennial-Decorating-Mistakes-42696116#photo-42696116';
17+
const html =
18+
fs.readFileSync('./fixtures/www.popsugar.com/1482182390796.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.popsugar.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, 'The 5 Biggest Decorating Mistakes Millennials Make — and How to Solve Them');
39+
});
40+
41+
it('returns the author', async () => {
42+
// To pass this test, fill out the author selector
43+
// in ./src/extractors/custom/www.popsugar.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, 'Maggie Winterfeldt');
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.popsugar.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-19T20:55: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.popsugar.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, 'https://media1.popsugar-assets.com/files/thumbor/ACcTJRJJH9B4HWDrRgOZF9Gk3ZU/fit-in/1200x630/filters:format_auto-!!-:strip_icc-!!-:fill-!white!-/2016/11/11/857/n/1922794/9242685858261d30174e09.95998840_edit_img_facebook_post_image_file_42696116_1478888970_14788922890196.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.popsugar.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, 'No generation is perfect, and when it comes to millennials, one area ripe');
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)