mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-05-21 21:47:18 +09:00
なんかもうめっちゃ変えた
This commit is contained in:
parent
80f479f9ea
commit
bfac32b802
19
README.md
19
README.md
@ -1,28 +1,21 @@
|
|||||||
webcard
|
summaly
|
||||||
=======
|
=======
|
||||||
|
|
||||||
Generate an html of any web page's summary.
|
Get any web page's summary.
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
`$ npm install webcard`
|
`$ npm install summaly`
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
`(url: string, options: Options) => Promise<string>`
|
`(url: string) => Promise<string>`
|
||||||
|
|
||||||
### Options
|
|
||||||
| Property | Type | Description | Default |
|
|
||||||
| :-------- | :------- | :---------------------------------------- | :------ |
|
|
||||||
| **proxy** | *string* | URL of proxy that wrap non-https contents | `null` |
|
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
``` javascript
|
``` javascript
|
||||||
import webcard from 'webcard';
|
import summaly from 'summaly';
|
||||||
|
|
||||||
const html = await webcard('http://example.com', {
|
const info = await summaly('http://example.com');
|
||||||
proxy: 'https://your.proxy.com'
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
License
|
License
|
||||||
|
12
package.json
12
package.json
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "webcard",
|
"name": "summaly",
|
||||||
"version": "0.0.1",
|
"version": "1.0.0",
|
||||||
"description": "Generate an html of any web page's summary",
|
"description": "Get web page's summary",
|
||||||
"author": "syuilo <i@syuilo.com>",
|
"author": "syuilo <i@syuilo.com>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "https://github.com/syuilo/webcard.git",
|
"repository": "https://github.com/syuilo/summaly.git",
|
||||||
"bugs": "https://github.com/syuilo/webcard/issues",
|
"bugs": "https://github.com/syuilo/summaly/issues",
|
||||||
"main": "./built/index.js",
|
"main": "./built/index.js",
|
||||||
"typings": "./built/index.d.ts",
|
"typings": "./built/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -25,8 +25,6 @@
|
|||||||
"babel-core": "6.13.2",
|
"babel-core": "6.13.2",
|
||||||
"babel-polyfill": "6.13.0",
|
"babel-polyfill": "6.13.0",
|
||||||
"cheerio-httpcli": "^0.6.9",
|
"cheerio-httpcli": "^0.6.9",
|
||||||
"html-entities": "^1.2.0",
|
|
||||||
"pug": "^2.0.0-beta6",
|
|
||||||
"request": "^2.74.0"
|
"request": "^2.74.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
import * as request from 'request';
|
import * as request from 'request';
|
||||||
const pug = require('pug');
|
|
||||||
import Options from '../options';
|
|
||||||
|
|
||||||
const Entities = require('html-entities').AllHtmlEntities;
|
const Entities = require('html-entities').AllHtmlEntities;
|
||||||
const entities = new Entities();
|
const entities = new Entities();
|
||||||
@ -10,7 +8,7 @@ const client = require('cheerio-httpcli');
|
|||||||
client.referer = false;
|
client.referer = false;
|
||||||
client.timeout = 10000;
|
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);
|
const res = await client.fetch(url.href);
|
||||||
|
|
||||||
if (res.error) {
|
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"]').attr('href') ||
|
||||||
$('link[rel="apple-touch-icon image_src"]').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 =
|
let description =
|
||||||
$('meta[property="misskey:summary"]').attr('content') ||
|
$('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') ||
|
$('link[rel="icon"]').attr('href') ||
|
||||||
'/favicon.ico';
|
'/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`, {
|
return {
|
||||||
url: url,
|
|
||||||
title: title,
|
title: title,
|
||||||
icon: icon,
|
icon: icon,
|
||||||
lang: lang,
|
|
||||||
description: description,
|
description: description,
|
||||||
type: type,
|
thumbnail: image,
|
||||||
image: image,
|
sitename: siteName
|
||||||
siteName: siteName
|
};
|
||||||
});
|
|
||||||
|
|
||||||
function proxy(url: string): string {
|
|
||||||
return opts.proxy ? `${opts.proxy}/${url}` : url;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function promisifyRequest(request: any): (x: any) => Promise<any> {
|
function promisifyRequest(request: any): (x: any) => Promise<any> {
|
@ -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
|
|
17
src/index.ts
17
src/index.ts
@ -1,6 +1,6 @@
|
|||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
|
import ISummary from './isummary';
|
||||||
import IPlugin from './iplugin';
|
import IPlugin from './iplugin';
|
||||||
import Options from './options';
|
|
||||||
import general from './general';
|
import general from './general';
|
||||||
|
|
||||||
// Init babel
|
// Init babel
|
||||||
@ -8,24 +8,17 @@ require('babel-core/register');
|
|||||||
require('babel-polyfill');
|
require('babel-polyfill');
|
||||||
|
|
||||||
const plugins: IPlugin[] = [
|
const plugins: IPlugin[] = [
|
||||||
require('./plugins/wikipedia'),
|
require('./plugins/wikipedia')
|
||||||
require('./plugins/soundcloud'),
|
|
||||||
require('./plugins/youtube')
|
|
||||||
];
|
];
|
||||||
|
|
||||||
export default async (url: string, options?: Options): Promise<string> => {
|
export default async (url: string): Promise<ISummary> => {
|
||||||
const _url = URL.parse(url, true);
|
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];
|
const plugin = plugins.filter(plugin => plugin.test(_url))[0];
|
||||||
|
|
||||||
if (plugin) {
|
if (plugin) {
|
||||||
return await plugin.compile(_url, opts);
|
return await plugin.summary(_url);
|
||||||
} else {
|
} else {
|
||||||
return await general(_url, opts);
|
return await general(_url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
import Options from './options';
|
import ISummary from './isummary';
|
||||||
|
|
||||||
interface IPlugin {
|
interface IPlugin {
|
||||||
test: (url: URL.Url) => boolean;
|
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
9
src/isummary.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
interface ISummary {
|
||||||
|
title: string;
|
||||||
|
icon: string;
|
||||||
|
description: string;
|
||||||
|
thumbnail: string;
|
||||||
|
sitename: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ISummary;
|
@ -1,5 +0,0 @@
|
|||||||
interface Options {
|
|
||||||
proxy: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Options;
|
|
@ -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"');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
@ -1,6 +1,5 @@
|
|||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
const pug = require('pug');
|
import ISummary from '../isummary';
|
||||||
import Options from '../../options';
|
|
||||||
|
|
||||||
const client = require('cheerio-httpcli');
|
const client = require('cheerio-httpcli');
|
||||||
client.referer = false;
|
client.referer = false;
|
||||||
@ -10,7 +9,7 @@ exports.test = (url: URL.Url) => {
|
|||||||
return /\.wikipedia\.org$/.test(url.hostname);
|
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 res = await client.fetch(url.href);
|
||||||
const $: any = res.$;
|
const $: any = res.$;
|
||||||
|
|
||||||
@ -20,12 +19,11 @@ exports.compile = async (url: URL.Url, opts: Options) => {
|
|||||||
? $('#mw-content-text > p:first-of-type').text()
|
? $('#mw-content-text > p:first-of-type').text()
|
||||||
: $('#bodyContent > div:first-of-type > p:first-of-type').text();
|
: $('#bodyContent > div:first-of-type > p:first-of-type').text();
|
||||||
|
|
||||||
return pug.renderFile(`${__dirname}/../../general/summary.pug`, {
|
return {
|
||||||
url: url,
|
|
||||||
title: decodeURI(url.pathname.split('/')[2]),
|
title: decodeURI(url.pathname.split('/')[2]),
|
||||||
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
|
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
|
||||||
description: text,
|
description: text,
|
||||||
image: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
|
thumbnail: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
|
||||||
siteName: 'Wikipedia'
|
sitename: 'Wikipedia'
|
||||||
});
|
};
|
||||||
};
|
};
|
@ -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
|
|
||||||
});
|
|
||||||
};
|
|
@ -1 +0,0 @@
|
|||||||
iframe.youtube(src='https://www.youtube.com/embed/' + videoId, width='380', height='250', frameborder='0', allowfullscreen)
|
|
@ -26,11 +26,9 @@
|
|||||||
"./node_modules/typescript/lib/lib.es6.d.ts",
|
"./node_modules/typescript/lib/lib.es6.d.ts",
|
||||||
"./typings/bundle.d.ts",
|
"./typings/bundle.d.ts",
|
||||||
"./src/index.ts",
|
"./src/index.ts",
|
||||||
|
"./src/isummary.ts",
|
||||||
"./src/iplugin.ts",
|
"./src/iplugin.ts",
|
||||||
"./src/options.ts",
|
"./src/general.ts",
|
||||||
"./src/general/index.ts",
|
"./src/plugins/wikipedia.ts"
|
||||||
"./src/plugins/wikipedia/index.ts",
|
|
||||||
"./src/plugins/youtube/index.ts",
|
|
||||||
"./src/plugins/soundcloud/index.ts"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user