Skip to content

Commit

Permalink
なんかもうめっちゃ変えた
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Sep 14, 2016
1 parent 80f479f commit bfac32b
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 141 deletions.
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}
23 changes: 7 additions & 16 deletions src/general/index.ts → src/general.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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') ||
Expand Down Expand Up @@ -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> {
Expand Down
16 changes: 0 additions & 16 deletions src/general/summary.pug

This file was deleted.

17 changes: 5 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import * as URL from 'url';
import ISummary from './isummary';
import IPlugin from './iplugin';
import Options from './options';
import general from './general';

// Init babel
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);
}
}
6 changes: 3 additions & 3 deletions src/iplugin.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/isummary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface ISummary {
title: string;
icon: string;
description: string;
thumbnail: string;
sitename: string;
}

export default ISummary;
5 changes: 0 additions & 5 deletions src/options.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/plugins/soundcloud/index.ts

This file was deleted.

14 changes: 6 additions & 8 deletions src/plugins/wikipedia/index.ts → src/plugins/wikipedia.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.$;

Expand All @@ -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'
};
};
27 changes: 0 additions & 27 deletions src/plugins/youtube/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/plugins/youtube/view.pug

This file was deleted.

8 changes: 3 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}

0 comments on commit bfac32b

Please sign in to comment.