diff --git a/README.md b/README.md index ea2a526..805dba5 100644 --- a/README.md +++ b/README.md @@ -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` - -### Options -| Property | Type | Description | Default | -| :-------- | :------- | :---------------------------------------- | :------ | -| **proxy** | *string* | URL of proxy that wrap non-https contents | `null` | +`(url: string) => Promise` ### 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 diff --git a/package.json b/package.json index ab7487c..2737154 100644 --- a/package.json +++ b/package.json @@ -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 ", "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" } } diff --git a/src/general/index.ts b/src/general.ts similarity index 85% rename from src/general/index.ts rename to src/general.ts index aed8d2d..e4808c7 100644 --- a/src/general/index.ts +++ b/src/general.ts @@ -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 => { +export default async (url: URL.Url): Promise => { const res = await client.fetch(url.href); if (res.error) { @@ -52,7 +50,7 @@ export default async (url: URL.Url, opts: Options): Promise => { $('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 => { $('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 { diff --git a/src/general/summary.pug b/src/general/summary.pug deleted file mode 100644 index f2bc2c3..0000000 --- a/src/general/summary.pug +++ /dev/null @@ -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 diff --git a/src/index.ts b/src/index.ts index da2b4f7..1f56393 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 => { +export default async (url: string): Promise => { 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); } } diff --git a/src/iplugin.ts b/src/iplugin.ts index 115afdc..9218bf4 100644 --- a/src/iplugin.ts +++ b/src/iplugin.ts @@ -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; + summary: (url: URL.Url) => Promise; } -export default IPlugin; \ No newline at end of file +export default IPlugin; diff --git a/src/isummary.ts b/src/isummary.ts new file mode 100644 index 0000000..99ace7a --- /dev/null +++ b/src/isummary.ts @@ -0,0 +1,9 @@ +interface ISummary { + title: string; + icon: string; + description: string; + thumbnail: string; + sitename: string; +} + +export default ISummary; diff --git a/src/options.ts b/src/options.ts deleted file mode 100644 index 561b55f..0000000 --- a/src/options.ts +++ /dev/null @@ -1,5 +0,0 @@ -interface Options { - proxy: string; -} - -export default Options; \ No newline at end of file diff --git a/src/plugins/soundcloud/index.ts b/src/plugins/soundcloud/index.ts deleted file mode 100644 index 7026cc1..0000000 --- a/src/plugins/soundcloud/index.ts +++ /dev/null @@ -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"'); - } - }); -}; diff --git a/src/plugins/wikipedia/index.ts b/src/plugins/wikipedia.ts similarity index 68% rename from src/plugins/wikipedia/index.ts rename to src/plugins/wikipedia.ts index b08d074..629cbfa 100644 --- a/src/plugins/wikipedia/index.ts +++ b/src/plugins/wikipedia.ts @@ -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' + }; }; diff --git a/src/plugins/youtube/index.ts b/src/plugins/youtube/index.ts deleted file mode 100644 index 6322706..0000000 --- a/src/plugins/youtube/index.ts +++ /dev/null @@ -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 - }); -}; diff --git a/src/plugins/youtube/view.pug b/src/plugins/youtube/view.pug deleted file mode 100644 index 980d486..0000000 --- a/src/plugins/youtube/view.pug +++ /dev/null @@ -1 +0,0 @@ -iframe.youtube(src='https://www.youtube.com/embed/' + videoId, width='380', height='250', frameborder='0', allowfullscreen) diff --git a/tsconfig.json b/tsconfig.json index 5b5c67b..e1290b5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" ] }