2021-12-31 12:12:43 +00:00
|
|
|
// Requires axios and rate-limiter-flexible
|
|
|
|
var axios = require("axios");
|
|
|
|
var RateLimiterMemory = require("rate-limiter-flexible").RateLimiterMemory;
|
2021-12-30 15:35:26 +00:00
|
|
|
|
|
|
|
function buildArgsHash(args) {
|
|
|
|
let argsHash = {};
|
|
|
|
args.forEach(arg => {
|
|
|
|
const params = arg.split(':');
|
2022-01-04 06:45:46 +00:00
|
|
|
argsHash[params[0]] = params.slice(1).join(':');
|
2021-12-30 15:35:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return argsHash;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:43 +00:00
|
|
|
let gRateLimiter = new RateLimiterMemory({
|
|
|
|
points: 200,
|
|
|
|
duration: 1,
|
|
|
|
}); // Per wikipedia's recommendation, 200 reqs per second
|
|
|
|
|
|
|
|
async function requestWikipediaRequest(baseUrl) {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
await gRateLimiter.consume(baseUrl, 1);
|
|
|
|
break;
|
|
|
|
} catch (limited) {
|
|
|
|
if (limited instanceof Error) {
|
|
|
|
throw limited;
|
|
|
|
} else {
|
|
|
|
await new Promise(resolve => {
|
|
|
|
setTimeout(resolve, limited.msBeforeNext);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function requestWikipediaDone(baseUrl) {
|
|
|
|
await gRateLimiter.reward(baseUrl, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generatePrefetchedWikipediaTagHtml(args, content) {
|
|
|
|
const argsHash = buildArgsHash(args);
|
|
|
|
const title = argsHash['title'];
|
2022-01-04 06:45:46 +00:00
|
|
|
const escapedTitle = encodeURIComponent(title);
|
2021-12-31 12:12:43 +00:00
|
|
|
|
|
|
|
const lang = argsHash['lang'] !== undefined ? argsHash['lang'] : 'en';
|
|
|
|
const baseUrl = `https://${lang}.wikipedia.org`;
|
|
|
|
|
2022-01-04 06:45:46 +00:00
|
|
|
const url = `${baseUrl}/api/rest_v1/page/summary/${escapedTitle}`;
|
2021-12-31 12:12:43 +00:00
|
|
|
await requestWikipediaRequest(baseUrl);
|
|
|
|
let response = await axios.get(url, {
|
|
|
|
headers: {
|
|
|
|
'accept': 'application/json; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/Summary/1.4.2"',
|
|
|
|
'api-user-agent': `Hexo Wikipedia Tag (Prefetch, from ${hexo.config.url})`,
|
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
timeout: 3000,
|
|
|
|
params: {
|
|
|
|
redirect: false,
|
|
|
|
},
|
|
|
|
transitional: {
|
|
|
|
silentJSONParsing: false,
|
|
|
|
},
|
|
|
|
}).catch(reason => {
|
|
|
|
hexo.log.warn(`fetch failed for "${url}": ${reason}`);
|
|
|
|
return Promise.reject(reason);
|
|
|
|
});
|
|
|
|
await requestWikipediaDone(baseUrl);
|
|
|
|
let extractedText = response.data.extract;
|
|
|
|
let contentText = extractedText;
|
|
|
|
if (argsHash['wikiButton'] === 'true') {
|
|
|
|
contentText += `<p><a href="${baseUrl}/wiki/${title}">Wikipedia:${title}</a></p>`;
|
|
|
|
}
|
|
|
|
return `<blockquote>${contentText}</blockquote>`
|
|
|
|
}
|
|
|
|
|
2021-12-30 15:35:26 +00:00
|
|
|
function generateWikipediaTagHtml(args, content) {
|
|
|
|
const argsHash = buildArgsHash(args);
|
|
|
|
const title = argsHash['title'];
|
2022-01-04 06:45:46 +00:00
|
|
|
const escapedTitle = encodeURIComponent(title);
|
2021-12-30 15:35:26 +00:00
|
|
|
|
|
|
|
const lang = argsHash['lang'] !== undefined ? argsHash['lang'] : 'en';
|
|
|
|
const baseUrl = `https://${lang}.wikipedia.org`;
|
|
|
|
|
2022-01-04 06:45:46 +00:00
|
|
|
const url = `${baseUrl}/api/rest_v1/page/summary/${escapedTitle}?redirect=false`;
|
2021-12-30 15:35:26 +00:00
|
|
|
|
2021-12-31 12:12:43 +00:00
|
|
|
const tagId = "wikipedia-"+Math.round(Math.random() * 100000);
|
2021-12-30 15:35:26 +00:00
|
|
|
const embeddedScript = `
|
|
|
|
window.addEventListener('load', function() {
|
|
|
|
var element = document.getElementById('${tagId}');
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.addEventListener("load", function() {
|
|
|
|
var result = this.response;
|
|
|
|
const extract = result.extract;
|
|
|
|
element.prepend(extract);
|
|
|
|
});
|
|
|
|
req.addEventListener("error", function() {
|
|
|
|
element.prepend('Failed to fetch wikipedia data for "${title}".');
|
|
|
|
});
|
|
|
|
req.open('GET', '${url}');
|
|
|
|
req.responseType = 'json';
|
|
|
|
req.setRequestHeader('accept', 'application/json; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/Summary/1.4.2"');
|
2021-12-31 12:12:43 +00:00
|
|
|
req.setRequestHeader('api-user-agent', 'Hexo Wikipedia Tag (from ${hexo.config.url})');
|
2021-12-30 15:35:26 +00:00
|
|
|
req.send();
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
let contentText = `<script>${embeddedScript}</script>`;
|
|
|
|
if (argsHash['wikiButton'] === 'true') {
|
|
|
|
contentText += `<p><a href="${baseUrl}/wiki/${title}">Wikipedia:${title}</a></p>`;
|
2021-12-31 12:12:43 +00:00
|
|
|
} else {
|
|
|
|
contentText += `<noscript><p><a href="${baseUrl}/wiki/${title}">Wikipedia:${title}</a></p></noscript>`;
|
2021-12-30 15:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return `<blockquote id='${tagId}'>${contentText}</blockquote>`;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:43 +00:00
|
|
|
function wikipediaTag(args, content) {
|
|
|
|
// We use the client-fetching method as fallback
|
|
|
|
return generatePrefetchedWikipediaTagHtml(args, content).catch(reason => {
|
|
|
|
return generateWikipediaTagHtml(args, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hexo.extend.tag.register('wikipedia', wikipediaTag, {async: true});
|