This commit is contained in:
syuilo 2017-01-15 09:07:43 +09:00
parent dc12f09e79
commit be910ba116
2 changed files with 20 additions and 22 deletions

View File

@ -14,6 +14,7 @@
},
"devDependencies": {
"@types/node": "0.0.2",
"@types/request": "0.0.39",
"event-stream": "3.3.4",
"gulp": "3.9.1",
"gulp-typescript": "3.1.3",
@ -22,6 +23,7 @@
"dependencies": {
"cheerio-httpcli": "0.6.9",
"escape-regexp": "0.0.1",
"html-entities": "1.2.0"
"html-entities": "1.2.0",
"request": "2.79.0"
}
}

View File

@ -1,27 +1,23 @@
import * as URL from 'url';
const client = require('cheerio-httpcli');
client.referer = false;
client.timeout = 10000;
import * as request from 'request';
exports.test = (url: URL.Url) =>
/\.wikipedia\.org$/.test(url.hostname);
exports.summary = async (url: URL.Url) => {
const res = await client.fetch(url.href);
const $: any = res.$;
exports.summary = (url: URL.Url) => new Promise((res, rej) => {
const lang = url.host.split('.')[0];
const title = url.pathname.split('/')[2];
const endpoint = `https://${lang}.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=${title}`;
const lang = url.hostname.substr(0, url.hostname.indexOf('.'));
const isDesktop = !/\.m\.wikipedia\.org$/.test(url.hostname);
const text: string = isDesktop
? $('#mw-content-text > p:first-of-type').text()
: $('#mw-content-text > div:first-of-type > p:first-of-type').text();
return {
title: decodeURI(url.pathname.split('/')[2]),
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
description: text,
thumbnail: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
sitename: 'Wikipedia'
};
};
request(endpoint, (err, _, body) => {
body = JSON.parse(body);
const info = body.query.pages[Object.keys(body.query.pages)[0]];
res({
title: info.title,
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
description: info.extract,
thumbnail: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
sitename: 'Wikipedia'
});
});
});