mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-05-13 17:47:17 +09:00
This commit is contained in:
parent
dc12f09e79
commit
be910ba116
@ -14,6 +14,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "0.0.2",
|
"@types/node": "0.0.2",
|
||||||
|
"@types/request": "0.0.39",
|
||||||
"event-stream": "3.3.4",
|
"event-stream": "3.3.4",
|
||||||
"gulp": "3.9.1",
|
"gulp": "3.9.1",
|
||||||
"gulp-typescript": "3.1.3",
|
"gulp-typescript": "3.1.3",
|
||||||
@ -22,6 +23,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cheerio-httpcli": "0.6.9",
|
"cheerio-httpcli": "0.6.9",
|
||||||
"escape-regexp": "0.0.1",
|
"escape-regexp": "0.0.1",
|
||||||
"html-entities": "1.2.0"
|
"html-entities": "1.2.0",
|
||||||
|
"request": "2.79.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,23 @@
|
|||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
|
import * as request from 'request';
|
||||||
const client = require('cheerio-httpcli');
|
|
||||||
client.referer = false;
|
|
||||||
client.timeout = 10000;
|
|
||||||
|
|
||||||
exports.test = (url: URL.Url) =>
|
exports.test = (url: URL.Url) =>
|
||||||
/\.wikipedia\.org$/.test(url.hostname);
|
/\.wikipedia\.org$/.test(url.hostname);
|
||||||
|
|
||||||
exports.summary = async (url: URL.Url) => {
|
exports.summary = (url: URL.Url) => new Promise((res, rej) => {
|
||||||
const res = await client.fetch(url.href);
|
const lang = url.host.split('.')[0];
|
||||||
const $: any = res.$;
|
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('.'));
|
request(endpoint, (err, _, body) => {
|
||||||
const isDesktop = !/\.m\.wikipedia\.org$/.test(url.hostname);
|
body = JSON.parse(body);
|
||||||
const text: string = isDesktop
|
const info = body.query.pages[Object.keys(body.query.pages)[0]];
|
||||||
? $('#mw-content-text > p:first-of-type').text()
|
res({
|
||||||
: $('#mw-content-text > div:first-of-type > p:first-of-type').text();
|
title: info.title,
|
||||||
|
|
||||||
return {
|
|
||||||
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: info.extract,
|
||||||
thumbnail: `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'
|
||||||
};
|
});
|
||||||
};
|
});
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user