Some refactors

This commit is contained in:
syuilo 2017-02-06 06:50:06 +09:00
parent b483262e84
commit 5a3181c0f4
5 changed files with 20 additions and 19 deletions

View File

@ -12,9 +12,9 @@ import * as client from 'cheerio-httpcli';
client.set('referer', false); client.set('referer', false);
client.set('timeout', 10000); client.set('timeout', 10000);
import ISummary from './isummary'; import Summary from './summary';
export default async (url: URL.Url): Promise<ISummary> => { export default async (url: URL.Url): Promise<Summary> => {
const res = await client.fetch(url.href); const res = await client.fetch(url.href);
if (res.error) { if (res.error) {

View File

@ -6,16 +6,19 @@
import * as URL from 'url'; import * as URL from 'url';
import requireAll = require('require-all'); import requireAll = require('require-all');
import tracer from 'trace-redirect'; import tracer from 'trace-redirect';
import ISummary from './isummary'; import Summary from './summary';
import IPlugin from './iplugin'; import IPlugin from './iplugin';
import general from './general'; import general from './general';
// Load builtin plugins // Load builtin plugins
const builtinPlugins = requireAll({ const _builtinPlugins = requireAll({
dirname: __dirname + '/plugins' dirname: __dirname + '/plugins'
}) as { [key: string]: IPlugin }; }) as { [key: string]: IPlugin };
export interface IOptions { const builtinPlugins = Object.keys(_builtinPlugins)
.map(key => _builtinPlugins[key]);
type Options = {
/** /**
* *
*/ */
@ -25,27 +28,25 @@ export interface IOptions {
* Custom Plugins * Custom Plugins
*/ */
plugins?: IPlugin[]; plugins?: IPlugin[];
} };
type Result = ISummary & { type Result = Summary & {
url: string; url: string;
}; };
/** /**
* Summary an web page * Summary an web page
* @param {string} url URL of web page you want to summary * @param {string} url URL of web page you want to summary
* @param {IOptions} options The options * @param {Options} options The options
* @return {Promise<Result>} Promised summary * @return {Promise<Result>} Promised summary
*/ */
export default async (url: string, options: IOptions): Promise<Result> => { export default async (url: string, options: Options): Promise<Result> => {
const opts = Object.assign({ const opts = Object.assign({
followRedirects: true, followRedirects: true,
plugins: null plugins: null
}, options) as IOptions; }, options) as Options;
const plugins = Object.keys(builtinPlugins) const plugins = builtinPlugins.concat(opts.plugins || []);
.map(key => builtinPlugins[key])
.concat(opts.plugins || []);
// Follow redirects // Follow redirects
const actualUrl = opts.followRedirects ? await tracer(url) : url; const actualUrl = opts.followRedirects ? await tracer(url) : url;

View File

@ -4,7 +4,7 @@ import * as client from 'cheerio-httpcli';
client.set('referer', false); client.set('referer', false);
client.set('timeout', 10000); client.set('timeout', 10000);
export function test (url: URL.Url) { export function test(url: URL.Url) {
return url.hostname === 'www.amazon.com' || return url.hostname === 'www.amazon.com' ||
url.hostname === 'www.amazon.co.jp' || url.hostname === 'www.amazon.co.jp' ||
url.hostname === 'www.amazon.ca' || url.hostname === 'www.amazon.ca' ||
@ -21,7 +21,7 @@ export function test (url: URL.Url) {
url.hostname === 'www.amazon.au' url.hostname === 'www.amazon.au'
}; };
export async function summary (url: URL.Url) { export async function summary(url: URL.Url) {
const res = await client.fetch(url.href); const res = await client.fetch(url.href);
const $: client.CheerioStaticEx = res.$; const $: client.CheerioStaticEx = res.$;

View File

@ -5,11 +5,11 @@ import clip from './../utils/clip';
const log = debug('summaly:plugins:wikipedia'); const log = debug('summaly:plugins:wikipedia');
export function test (url: URL.Url) { export function test(url: URL.Url) {
return /\.wikipedia\.org$/.test(url.hostname); return /\.wikipedia\.org$/.test(url.hostname);
}; };
export function summary (url: URL.Url) { export function summary(url: URL.Url) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
const lang = url.host.split('.')[0]; const lang = url.host.split('.')[0];
const title = url.pathname.split('/')[2]; const title = url.pathname.split('/')[2];

View File

@ -1,4 +1,4 @@
interface ISummary { type Summary = {
description: string; description: string;
icon: string; icon: string;
sitename: string; sitename: string;
@ -6,4 +6,4 @@ interface ISummary {
title: string; title: string;
} }
export default ISummary; export default Summary;