mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-05-29 09:27:16 +09:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import * as URL from 'url';
|
|
import * as request from 'request-promise-native';
|
|
import * as debug from 'debug';
|
|
import summary from '../summary';
|
|
import clip from './../utils/clip';
|
|
|
|
const log = debug('summaly:plugins:wikipedia');
|
|
|
|
export function test(url: URL.Url): boolean {
|
|
return /\.wikipedia\.org$/.test(url.hostname);
|
|
}
|
|
|
|
export async function summarize(url: URL.Url): Promise<summary> {
|
|
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=${encodeURIComponent(title)}`;
|
|
|
|
log(`lang is ${lang}`);
|
|
log(`title is ${title}`);
|
|
log(`endpoint is ${endpoint}`);
|
|
|
|
let body = await request(endpoint);
|
|
body = JSON.parse(body);
|
|
log(body);
|
|
|
|
if (!('query' in body) || !('pages' in body.query)) {
|
|
throw 'fetch failed';
|
|
}
|
|
|
|
const info = body.query.pages[Object.keys(body.query.pages)[0]];
|
|
|
|
return {
|
|
title: info.title,
|
|
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
|
|
description: clip(info.extract, 300),
|
|
thumbnail: `https://wikipedia.org/static/images/project-logos/${lang}wiki.png`,
|
|
player: null,
|
|
sitename: 'Wikipedia'
|
|
};
|
|
}
|