mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-05-12 17:17:34 +09:00
Merge pull request #8 from armchair-philosophy/escape-commonJS
escape from commonJS concept
This commit is contained in:
commit
af20d95845
@ -4,9 +4,13 @@ import ISummary from './isummary';
|
|||||||
import IPlugin from './iplugin';
|
import IPlugin from './iplugin';
|
||||||
import general from './general';
|
import general from './general';
|
||||||
|
|
||||||
|
/* plugins */
|
||||||
|
import * as amazon from './plugins/amazon';
|
||||||
|
import * as wikipedia from './plugins/wikipedia';
|
||||||
|
|
||||||
const plugins: IPlugin[] = [
|
const plugins: IPlugin[] = [
|
||||||
require('./plugins/amazon'),
|
amazon,
|
||||||
require('./plugins/wikipedia')
|
wikipedia
|
||||||
];
|
];
|
||||||
|
|
||||||
export default async (url: string): Promise<ISummary> => {
|
export default async (url: string): Promise<ISummary> => {
|
||||||
|
@ -4,8 +4,8 @@ const client = require('cheerio-httpcli');
|
|||||||
client.referer = false;
|
client.referer = false;
|
||||||
client.timeout = 10000;
|
client.timeout = 10000;
|
||||||
|
|
||||||
exports.test = (url: URL.Url) =>
|
export function test (url: URL.Url) {
|
||||||
url.hostname === 'www.amazon.com' ||
|
return url.hostname === 'www.amazon.com' ||
|
||||||
url.hostname === 'www.amazon.co.jp' ||
|
url.hostname === 'www.amazon.co.jp' ||
|
||||||
url.hostname === 'www.amazon.ca' ||
|
url.hostname === 'www.amazon.ca' ||
|
||||||
url.hostname === 'www.amazon.com.br' ||
|
url.hostname === 'www.amazon.com.br' ||
|
||||||
@ -19,9 +19,9 @@ exports.test = (url: URL.Url) =>
|
|||||||
url.hostname === 'www.amazon.cn' ||
|
url.hostname === 'www.amazon.cn' ||
|
||||||
url.hostname === 'www.amazon.in' ||
|
url.hostname === 'www.amazon.in' ||
|
||||||
url.hostname === 'www.amazon.au'
|
url.hostname === 'www.amazon.au'
|
||||||
;
|
};
|
||||||
|
|
||||||
exports.summary = async (url: URL.Url) => {
|
export async function summary (url: URL.Url) {
|
||||||
const res = await client.fetch(url.href);
|
const res = await client.fetch(url.href);
|
||||||
const $: any = res.$;
|
const $: any = res.$;
|
||||||
|
|
||||||
|
@ -4,28 +4,31 @@ import * as debug from 'debug';
|
|||||||
|
|
||||||
const log = debug('summaly:plugins:wikipedia');
|
const log = debug('summaly:plugins:wikipedia');
|
||||||
|
|
||||||
exports.test = (url: URL.Url) =>
|
export function test (url: URL.Url) {
|
||||||
/\.wikipedia\.org$/.test(url.hostname);
|
return /\.wikipedia\.org$/.test(url.hostname);
|
||||||
|
};
|
||||||
|
|
||||||
exports.summary = (url: URL.Url) => new Promise((res, rej) => {
|
export function summary (url: URL.Url) {
|
||||||
const lang = url.host.split('.')[0];
|
return new Promise((res, rej) => {
|
||||||
const title = url.pathname.split('/')[2];
|
const lang = url.host.split('.')[0];
|
||||||
const endpoint = `https://${lang}.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=${encodeURIComponent(title)}`;
|
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(`lang is ${lang}`);
|
||||||
log(`title is ${title}`);
|
log(`title is ${title}`);
|
||||||
log(`endpoint is ${endpoint}`);
|
log(`endpoint is ${endpoint}`);
|
||||||
|
|
||||||
request(endpoint, (err, _, body) => {
|
request(endpoint, (err, _, body) => {
|
||||||
log(body);
|
log(body);
|
||||||
body = JSON.parse(body);
|
body = JSON.parse(body);
|
||||||
const info = body.query.pages[Object.keys(body.query.pages)[0]];
|
const info = body.query.pages[Object.keys(body.query.pages)[0]];
|
||||||
res({
|
res({
|
||||||
title: info.title,
|
title: info.title,
|
||||||
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
|
icon: 'https://wikipedia.org/static/favicon/wikipedia.ico',
|
||||||
description: info.extract,
|
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