なんかもうめっちゃ変えた

This commit is contained in:
syuilo 2016-09-15 02:52:47 +09:00
parent 80f479f9ea
commit bfac32b802
13 changed files with 44 additions and 141 deletions

View File

@ -1,28 +1,21 @@
webcard
summaly
=======
Generate an html of any web page's summary.
Get any web page's summary.
Installation
------------
`$ npm install webcard`
`$ npm install summaly`
Usage
-----
`(url: string, options: Options) => Promise<string>`
### Options
| Property | Type | Description | Default |
| :-------- | :------- | :---------------------------------------- | :------ |
| **proxy** | *string* | URL of proxy that wrap non-https contents | `null` |
`(url: string) => Promise<string>`
### Example
``` javascript
import webcard from 'webcard';
import summaly from 'summaly';
const html = await webcard('http://example.com', {
proxy: 'https://your.proxy.com'
});
const info = await summaly('http://example.com');
```
License

View File

@ -1,11 +1,11 @@
{
"name": "webcard",
"version": "0.0.1",
"description": "Generate an html of any web page's summary",
"name": "summaly",
"version": "1.0.0",
"description": "Get web page's summary",
"author": "syuilo <i@syuilo.com>",
"license": "MIT",
"repository": "https://github.com/syuilo/webcard.git",
"bugs": "https://github.com/syuilo/webcard/issues",
"repository": "https://github.com/syuilo/summaly.git",
"bugs": "https://github.com/syuilo/summaly/issues",
"main": "./built/index.js",
"typings": "./built/index.d.ts",
"scripts": {
@ -25,8 +25,6 @@
"babel-core": "6.13.2",
"babel-polyfill": "6.13.0",
"cheerio-httpcli": "^0.6.9",
"html-entities": "^1.2.0",
"pug": "^2.0.0-beta6",
"request": "^2.74.0"
}
}

View File

@ -1,7 +1,5 @@
import * as URL from 'url';
import * as request from 'request';
const pug = require('pug');
import Options from '../options';
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
@ -10,7 +8,7 @@ const client = require('cheerio-httpcli');
client.referer = false;
client.timeout = 10000;
export default async (url: URL.Url, opts: Options): Promise<string> => {
export default async (url: URL.Url): Promise<any> => {
const res = await client.fetch(url.href);
if (res.error) {
@ -52,7 +50,7 @@ export default async (url: URL.Url, opts: Options): Promise<string> => {
$('link[rel="apple-touch-icon"]').attr('href') ||
$('link[rel="apple-touch-icon image_src"]').attr('href');
image = image ? proxy(URL.resolve(url.href, image)) : null;
image = image ? URL.resolve(url.href, image) : null;
let description =
$('meta[property="misskey:summary"]').attr('content') ||
@ -81,22 +79,15 @@ export default async (url: URL.Url, opts: Options): Promise<string> => {
$('link[rel="icon"]').attr('href') ||
'/favicon.ico';
icon = icon ? proxy(URL.resolve(url.href, icon)) : null;
icon = icon ? URL.resolve(url.href, icon) : null;
return pug.renderFile(`${__dirname}/summary.pug`, {
url: url,
return {
title: title,
icon: icon,
lang: lang,
description: description,
type: type,
image: image,
siteName: siteName
});
function proxy(url: string): string {
return opts.proxy ? `${opts.proxy}/${url}` : url;
}
thumbnail: image,
sitename: siteName
};
}
function promisifyRequest(request: any): (x: any) => Promise<any> {

View File

@ -1,16 +0,0 @@
a(title= url.href, href= url.href, target='_blank')
aside(lang= lang, data-type= type)
if image
div.thumbnail(style={'background-image': 'url(' + image + ')'})
h1.title= title
if description
p.description= description
footer
p.hostname
if url.protocol == 'https:'
i.fa.fa-lock.secure
= url.hostname
if icon
img.icon(src= icon, alt='')
if siteName
p.site-name= siteName

View File

@ -1,6 +1,6 @@
import * as URL from 'url';
import ISummary from './isummary';
import IPlugin from './iplugin';
import Options from './options';
import general from './general';
// Init babel
@ -8,24 +8,17 @@ require('babel-core/register');
require('babel-polyfill');
const plugins: IPlugin[] = [
require('./plugins/wikipedia'),
require('./plugins/soundcloud'),
require('./plugins/youtube')
require('./plugins/wikipedia')
];
export default async (url: string, options?: Options): Promise<string> => {
export default async (url: string): Promise<ISummary> => {
const _url = URL.parse(url, true);
const opts: any = options || {};
if (!opts.hasOwnProperty('proxy')) {
opts.proxy = null;
}
const plugin = plugins.filter(plugin => plugin.test(_url))[0];
if (plugin) {
return await plugin.compile(_url, opts);
return await plugin.summary(_url);
} else {
return await general(_url, opts);
return await general(_url);
}
}

View File

@ -1,9 +1,9 @@
import * as URL from 'url';
import Options from './options';
import ISummary from './isummary';
interface IPlugin {
test: (url: URL.Url) => boolean;
compile: (url: URL.Url, opts: Options) => Promise<string>;
summary: (url: URL.Url) => Promise<ISummary>;
}
export default IPlugin;
export default IPlugin;

9
src/isummary.ts Normal file
View File

@ -0,0 +1,9 @@
interface ISummary {
title: string;
icon: string;
description: string;
thumbnail: string;
sitename: string;
}
export default ISummary;

View File

@ -1,5 +0,0 @@
interface Options {
proxy: string;
}
export default Options;

View File

@ -1,28 +0,0 @@
import * as URL from 'url';
import * as request from 'request';
import Options from '../../options';
exports.test = (url: URL.Url) => {
return url.hostname == 'soundcloud.com';
};
exports.compile = async (url: URL.Url, opts: Options) => {
request({
url: 'http://soundcloud.com/oembed',
method: 'get',
qs: {
format: 'json',
url: url.href
}
}, (err, response, body) => {
if (err) {
throw err;
} else if (response.statusCode !== 200) {
return null;
} else {
const parsed = JSON.parse(body);
const html = parsed.html;
return html.replace('height="400"', 'height="200"');
}
});
};

View File

@ -1,6 +1,5 @@
import * as URL from 'url';
const pug = require('pug');
import Options from '../../options';
import ISummary from '../isummary';
const client = require('cheerio-httpcli');
client.referer = false;
@ -10,7 +9,7 @@ exports.test = (url: URL.Url) => {
return /\.wikipedia\.org$/.test(url.hostname);
};
exports.compile = async (url: URL.Url, opts: Options) => {
exports.summary = async (url: URL.Url) => {
const res = await client.fetch(url.href);
const $: any = res.$;
@ -20,12 +19,11 @@ exports.compile = async (url: URL.Url, opts: Options) => {
? $('#mw-content-text > p:first-of-type').text()
: $('#bodyContent > div:first-of-type > p:first-of-type').text();
return pug.renderFile(`${__dirname}/../../general/summary.pug`, {
url: url,
return {
title: decodeURI(url.pathname.split('/')[2]),
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
description: text,
image: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
siteName: 'Wikipedia'
});
thumbnail: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
sitename: 'Wikipedia'
};
};

View File

@ -1,27 +0,0 @@
import * as URL from 'url';
const pug = require('pug');
import Options from '../../options';
exports.test = (url: URL.Url) =>
url.hostname == 'youtube.com' ||
url.hostname == 'www.youtube.com' ||
url.hostname == 'youtu.be'
;
exports.compile = async (url: URL.Url, opts: Options) => {
let videoId: string;
switch (url.hostname) {
case 'www.youtube.com':
case 'youtube.com':
videoId = url.query.v;
break;
case 'youtu.be':
videoId = url.pathname;
break;
}
return pug.renderFile(`${__dirname}/view.pug`, {
videoId
});
};

View File

@ -1 +0,0 @@
iframe.youtube(src='https://www.youtube.com/embed/' + videoId, width='380', height='250', frameborder='0', allowfullscreen)

View File

@ -26,11 +26,9 @@
"./node_modules/typescript/lib/lib.es6.d.ts",
"./typings/bundle.d.ts",
"./src/index.ts",
"./src/isummary.ts",
"./src/iplugin.ts",
"./src/options.ts",
"./src/general/index.ts",
"./src/plugins/wikipedia/index.ts",
"./src/plugins/youtube/index.ts",
"./src/plugins/soundcloud/index.ts"
"./src/general.ts",
"./src/plugins/wikipedia.ts"
]
}