Merge pull request #11 from postlight/feat-politico-extractor

feat: added politico extractor
pull/12/head
Toy Vano 8 years ago committed by GitHub
commit 3c99404566

61
dist/mercury.js vendored

@ -874,7 +874,63 @@ var LittleThingsExtractor = {
clean: []
},
lead_image_url: null,
lead_image_url: {
selectors: [['meta[name="og:image"]', 'value']]
},
next_page_url: null,
excerpt: null
};
// Rename CustomExtractor
// to fit your publication
// (e.g., NYTimesExtractor)
var PoliticoExtractor = {
domain: 'www.politico.com',
title: {
selectors: [
// enter title selectors
['meta[name="og:title"]', 'value']]
},
author: {
selectors: [
// enter author selectors
]
},
content: {
selectors: [
// enter content selectors
'.story-main-content', '.content-group', '.story-core', '.story-text'],
// 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: ['figcaption']
},
date_published: {
selectors: [
// enter date selectors
['.timestamp[time="datetime"]']]
},
lead_image_url: {
selectors: [
// enter lead_image_url selectors
['meta[name="og:image"]', 'value']]
},
dek: {
selectors: [['meta[name="description"]', 'value']]
},
next_page_url: null,
@ -894,7 +950,8 @@ var Extractors = {
'www.yahoo.com': YahooExtractor,
'www.buzzfeed.com': BuzzfeedExtractor,
'fandom.wikia.com': WikiaExtractor,
'www.littlethings.com': LittleThingsExtractor
'www.littlethings.com': LittleThingsExtractor,
'www.politico.com': PoliticoExtractor
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -11,6 +11,7 @@ import { YahooExtractor } from './custom/www.yahoo.com';
import { BuzzfeedExtractor } from './custom/www.buzzfeed.com';
import { WikiaExtractor } from './custom/fandom.wikia.com';
import { LittleThingsExtractor } from './custom/www.littlethings.com';
import { PoliticoExtractor } from './custom/www.politico.com';
const Extractors = {
@ -27,6 +28,7 @@ const Extractors = {
'www.buzzfeed.com': BuzzfeedExtractor,
'fandom.wikia.com': WikiaExtractor,
'www.littlethings.com': LittleThingsExtractor,
'www.politico.com': PoliticoExtractor,
};

@ -0,0 +1,67 @@
// Rename CustomExtractor
// to fit your publication
// (e.g., NYTimesExtractor)
export const PoliticoExtractor = {
domain: 'www.politico.com',
title: {
selectors: [
// enter title selectors
['meta[name="og:title"]', 'value'],
],
},
author: {
selectors: [
// enter author selectors
],
},
content: {
selectors: [
// enter content selectors
'.story-main-content',
'.content-group', '.story-core',
'.story-text',
],
// 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: [
'figcaption',
],
},
date_published: {
selectors: [
// enter date selectors
['.timestamp[time="datetime"]'],
],
},
lead_image_url: {
selectors: [
// enter lead_image_url selectors
['meta[name="og:image"]', 'value'],
],
},
dek: {
selectors: [
['meta[name="description"]', 'value'],
],
},
next_page_url: null,
excerpt: null,
};

@ -0,0 +1,134 @@
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';
// Rename CustomExtractor
describe('PoliticoExtractor', () => {
it('is selected properly', () => {
// To pass this test, rename your extractor in
// ./src/extractors/custom/www.politico.com/index.js
// (e.g., CustomExtractor => NYTimesExtractor)
// then add your new extractor to
// src/extractors/all.js
const url =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
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.politico.com/index.js.
const html =
fs.readFileSync('./fixtures/www.politico.com/1475617690069.html');
const articleUrl =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
const { title } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(title, 'Insiders: Trump will sink Pence in VP debate');
});
it('returns the author', ((async)) () => {
// To pass this test, fill out the author selector
// in ./src/extractors/custom/www.politico.com/index.js.
const html =
fs.readFileSync('./fixtures/www.politico.com/1475617690069.html');
const articleUrl =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
const { author } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(author, '');
});
it('returns the date_published', ((async)) () => {
// To pass this test, fill out the date_published selector
// in ./src/extractors/custom/www.politico.com/index.js.
const html =
fs.readFileSync('./fixtures/www.politico.com/1475617690069.html');
const articleUrl =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
const { date_published } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(date_published, '');
});
it('returns the dek', ((async)) () => {
// To pass this test, fill out the dek selector
// in ./src/extractors/custom/www.politico.com/index.js.
const html =
fs.readFileSync('./fixtures/www.politico.com/1475617690069.html');
const articleUrl =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
const { dek } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(dek, '"Is it just me or are the two VP candidates infinitely more appealing than their running mates?" said a Pennsylvania Republican.');
});
it('returns the lead_image_url', ((async)) () => {
// To pass this test, fill out the lead_image_url selector
// in ./src/extractors/custom/www.politico.com/index.js.
const html =
fs.readFileSync('./fixtures/www.politico.com/1475617690069.html');
const articleUrl =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
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://static.politico.com/0f/e7/5ee9a89044d1a01f74140bcd5b9e/caucus-vp-preview.jpg');
});
it('returns the content', ((async)) () => {
// To pass this test, fill out the content selector
// in ./src/extractors/custom/www.politico.com/index.js.
// You may also want to make use of the clean and transform
// options.
const html =
fs.readFileSync('./fixtures/www.politico.com/1475617690069.html');
const url =
'http://www.politico.com/story/2016/10/who-will-win-the-vp-debate-229079?lo=ut_a1';
const { content } =
await Mercury.parse(url, html, { fallback: false });
const $ = cheerio.load(content || '');
const first13 = $('*').first()
.text()
.trim()
.split(/\s+/)
.slice(0, 13)
.join(' ');
// Update these values with the expected values from
// the article.
assert.equal(first13, 'Tim Kaine isnt Mike Pences only opponent Tuesday night in the only debate');
});
});
Loading…
Cancel
Save