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('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);
if (res.error) {

View File

@ -6,16 +6,19 @@
import * as URL from 'url';
import requireAll = require('require-all');
import tracer from 'trace-redirect';
import ISummary from './isummary';
import Summary from './summary';
import IPlugin from './iplugin';
import general from './general';
// Load builtin plugins
const builtinPlugins = requireAll({
const _builtinPlugins = requireAll({
dirname: __dirname + '/plugins'
}) 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
*/
plugins?: IPlugin[];
}
};
type Result = ISummary & {
type Result = Summary & {
url: string;
};
/**
* Summary an web page
* @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
*/
export default async (url: string, options: IOptions): Promise<Result> => {
export default async (url: string, options: Options): Promise<Result> => {
const opts = Object.assign({
followRedirects: true,
plugins: null
}, options) as IOptions;
}, options) as Options;
const plugins = Object.keys(builtinPlugins)
.map(key => builtinPlugins[key])
.concat(opts.plugins || []);
const plugins = builtinPlugins.concat(opts.plugins || []);
// Follow redirects
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('timeout', 10000);
export function test (url: URL.Url) {
export function test(url: URL.Url) {
return url.hostname === 'www.amazon.com' ||
url.hostname === 'www.amazon.co.jp' ||
url.hostname === 'www.amazon.ca' ||
@ -21,7 +21,7 @@ export function test (url: URL.Url) {
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 $: client.CheerioStaticEx = res.$;

View File

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

View File

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