diff --git a/package.json b/package.json index abcc764..c7613d2 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/plugins/wikipedia.ts b/src/plugins/wikipedia.ts index c797472..739bad9 100644 --- a/src/plugins/wikipedia.ts +++ b/src/plugins/wikipedia.ts @@ -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' + }); + }); +});