Skip to content

Commit

Permalink
feat: aol custom extractor (#42)
Browse files Browse the repository at this point in the history
* feat: aol custom parser

* removed work from other commits. merged with latest master
  • Loading branch information
janetleekim authored and adampash committed Dec 2, 2016
1 parent 4cc3b68 commit b415d1d
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fixtures/www.aol.com/1480618816916.html

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions fixtures/www.theguardian.com/1480457558008.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/extractors/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './newrepublic.com';
export * from './money.cnn.com';
export * from './www.theverge.com';
export * from './www.cnn.com';
export * from './www.aol.com';
53 changes: 53 additions & 0 deletions src/extractors/custom/www.aol.com/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export const WwwAolComExtractor = {
domain: 'www.aol.com',

title: {
selectors: [
'h1.p-article__title',
],
},

author: {
selectors: [
['meta[name="author"]', 'value'],
],
},

date_published: {
selectors: [
'.p-article__byline__date',
],

timezone: 'America/New_York',
},

dek: {
selectors: [
// enter selectors
],
},

lead_image_url: {
selectors: [
['meta[name="og:image"]', 'value'],
],
},

content: {
selectors: [
'.article-content',
],

// Is there anything in the content you selected that needs transformed
// before it's consumable content? E.g., unusual lazy loaded images
transforms: {
},

// Is there anything that is in the result that shouldn't be?
// The clean selectors will remove anything that matches from
// the result
clean: [

],
},
};
106 changes: 106 additions & 0 deletions src/extractors/custom/www.aol.com/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import assert from 'assert';
import fs from 'fs';
import URL from 'url';
import cheerio from 'cheerio';

import Mercury from 'mercury';
import getExtractor from 'extractors/get-extractor';
import { excerptContent } from 'utils/text';

describe('WwwAolComExtractor', () => {
it('is selected properly', () => {
// This test should be passing by default.
// It sanity checks that the correct parser
// is being selected for URLs from this domain
const url =
'http://www.aol.com/article/news/2016/12/01/son-of-slain-police-officer-given-teddy-bears-made-from-dads-un/21618553/';
const extractor = getExtractor(url);
assert.equal(extractor.domain, URL.parse(url).hostname);
});

it('returns the title', async () => {
// To pass this test, fill out the title selector
// in ./src/extractors/custom/www.aol.com/index.js.
const html =
fs.readFileSync('./fixtures/www.aol.com/1480618816916.html');
const articleUrl =
'http://www.aol.com/article/news/2016/12/01/son-of-slain-police-officer-given-teddy-bears-made-from-dads-un/21618553/';

const { title } =
await Mercury.parse(articleUrl, html, { fallback: false });

// Update these values with the expected values from
// the article.
assert.equal(title, 'Son of slain police officer given teddy bears made from dad\'s uniform');
});

it('returns the author', async () => {
// To pass this test, fill out the author selector
// in ./src/extractors/custom/www.aol.com/index.js.
const html =
fs.readFileSync('./fixtures/www.aol.com/1480618816916.html');
const articleUrl =
'http://www.aol.com/article/news/2016/12/01/son-of-slain-police-officer-given-teddy-bears-made-from-dads-un/21618553/';

const { author } =
await Mercury.parse(articleUrl, html, { fallback: false });

// Update these values with the expected values from
// the article.
assert.equal(author, 'AOL Staff');
});

it('returns the date_published', async () => {
// To pass this test, fill out the date_published selector
// in ./src/extractors/custom/www.aol.com/index.js.
const html =
fs.readFileSync('./fixtures/www.aol.com/1480618816916.html');
const articleUrl =
'http://www.aol.com/article/news/2016/12/01/son-of-slain-police-officer-given-teddy-bears-made-from-dads-un/21618553/';

const { date_published } =
await Mercury.parse(articleUrl, html, { fallback: false });

// Update these values with the expected values from
// the article.
assert.equal(date_published, '2016-12-01T18:01:00.000Z');
});

it('returns the lead_image_url', async () => {
// To pass this test, fill out the lead_image_url selector
// in ./src/extractors/custom/www.aol.com/index.js.
const html =
fs.readFileSync('./fixtures/www.aol.com/1480618816916.html');
const articleUrl =
'http://www.aol.com/article/news/2016/12/01/son-of-slain-police-officer-given-teddy-bears-made-from-dads-un/21618553/';

const { lead_image_url } =
await Mercury.parse(articleUrl, html, { fallback: false });

// Update these values with the expected values from
// the article.
assert.equal(lead_image_url, 'http://o.aolcdn.com/dims-shared/dims3/GLOB/crop/475x312+0+0/resize/1028x675!/format/jpg/quality/85/http%3A%2F%2Fo.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2Fc8242ab14e089c284b031379d025d64%2F204656928%2FScreen%2BShot%2B2016-12-01%2Bat%2B1.15.51%2BPM.png');
});

it('returns the content', async () => {
// To pass this test, fill out the content selector
// in ./src/extractors/custom/www.aol.com/index.js.
// You may also want to make use of the clean and transform
// options.
const html =
fs.readFileSync('./fixtures/www.aol.com/1480618816916.html');
const url =
'http://www.aol.com/article/news/2016/12/01/son-of-slain-police-officer-given-teddy-bears-made-from-dads-un/21618553/';

const { content } =
await Mercury.parse(url, html, { fallback: false });

const $ = cheerio.load(content || '');

const first13 = excerptContent($('*').first().text(), 13);

// Update these values with the expected values from
// the article.
assert.equal(first13, 'ST. LOUIS, MO (KTVI) – Amid unimaginable grief, the widow of slain Saint');
});
});

0 comments on commit b415d1d

Please sign in to comment.