Skip to content

Commit 3c5fa28

Browse files
janetleekimkev5873
authored andcommitted
feat: cbs sports parser (#98)
* feat: cbs sports parser
1 parent 3cf2d0d commit 3c5fa28

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

fixtures/www.cbssports.com/1482254907948.html

+3
Large diffs are not rendered by default.

src/extractors/custom/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export * from './www.reuters.com';
3737
export * from './mashable.com';
3838
export * from './www.chicagotribune.com';
3939
export * from './www.vox.com';
40+
export * from './www.cbssports.com';
4041
export * from './www.msnbc.com';
4142
export * from './www.thepoliticalinsider.com';
4243
export * from './www.mentalfloss.com';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export const WwwCbssportsComExtractor = {
2+
domain: 'www.cbssports.com',
3+
4+
title: {
5+
selectors: [
6+
'.article-headline',
7+
],
8+
},
9+
10+
author: {
11+
selectors: [
12+
'.author-name',
13+
],
14+
},
15+
16+
date_published: {
17+
selectors: [
18+
['.date-original-reading-time time', 'datetime'],
19+
],
20+
timezone: 'UTC',
21+
},
22+
23+
dek: {
24+
selectors: [
25+
'.article-subline',
26+
],
27+
},
28+
29+
lead_image_url: {
30+
selectors: [
31+
['meta[name="og:image"]', 'value'],
32+
],
33+
},
34+
35+
content: {
36+
selectors: [
37+
'.article',
38+
],
39+
40+
// Is there anything in the content you selected that needs transformed
41+
// before it's consumable content? E.g., unusual lazy loaded images
42+
transforms: {
43+
},
44+
45+
// Is there anything that is in the result that shouldn't be?
46+
// The clean selectors will remove anything that matches from
47+
// the result
48+
clean: [
49+
50+
],
51+
},
52+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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('WwwCbssportsComExtractor', () => {
11+
describe('initial test case', () => {
12+
let result;
13+
let url;
14+
beforeAll(() => {
15+
url =
16+
'http://www.cbssports.com/mlb/news/why-despite-the-complaints-of-many-mlb-players-are-actually-not-overpaid/';
17+
const html =
18+
fs.readFileSync('./fixtures/www.cbssports.com/1482254907948.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.cbssports.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, 'Why, despite the complaints of many, MLB players are actually not overpaid');
39+
});
40+
41+
it('returns the author', async () => {
42+
// To pass this test, fill out the author selector
43+
// in ./src/extractors/custom/www.cbssports.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, 'Matt Snyder');
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.cbssports.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-19T19:19:00.000Z');
59+
});
60+
61+
it('returns the dek', async () => {
62+
// To pass this test, fill out the dek selector
63+
// in ./src/extractors/custom/www.cbssports.com/index.js.
64+
const { dek } = await result;
65+
66+
// Update these values with the expected values from
67+
// the article.
68+
assert.equal(dek, 'Fan backlash against league-wide salaries is wholly misguided');
69+
});
70+
71+
it('returns the lead_image_url', async () => {
72+
// To pass this test, fill out the lead_image_url selector
73+
// in ./src/extractors/custom/www.cbssports.com/index.js.
74+
const { lead_image_url } = await result;
75+
76+
// Update these values with the expected values from
77+
// the article.
78+
assert.equal(lead_image_url, 'http://sportshub.cbsistatic.com/i/r/2016/12/19/4afa0e8e-b3b8-4c44-aca2-688cd11e9b39/thumbnail/770x433/f8a6d661a00ba87cb98847bdef9dfbad/yoenis-cespedes-121916.jpg');
79+
});
80+
81+
it('returns the content', async () => {
82+
// To pass this test, fill out the content selector
83+
// in ./src/extractors/custom/www.cbssports.com/index.js.
84+
// You may also want to make use of the clean and transform
85+
// options.
86+
const { content } = await result;
87+
88+
const $ = cheerio.load(content || '');
89+
90+
const first13 = excerptContent($('*').first().text(), 13);
91+
92+
// Update these values with the expected values from
93+
// the article.
94+
assert.equal(first13, 'Once the World Series ends, we know we\'re going to be treated to');
95+
});
96+
});
97+
});

0 commit comments

Comments
 (0)