1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- const cheerio = require('cheerio');
- const removeClasses = [
- 'table',
- 'div',
- 'script',
- 'input',
- 'style',
- 'ul.gallery',
- '.mw-editsection',
- 'sup.reference',
- 'ol.references',
- '.error',
- '.nomobile',
- '.noprint',
- '.noexcerpt',
- '.sortkey'
- ];
- /**
- * Parses a wiki page to get it's description.
- * @param {import('discord.js').Message} msg - The Discord message.
- * @param {String} title - The title of the page.
- * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
- * @param {String} wiki - The wiki for the page.
- * @param {String} thumbnail - The default thumbnail for the wiki.
- */
- function parse_page(msg, title, embed, wiki, thumbnail) {
- if ( !msg || ( embed.description && embed.thumbnail?.url !== thumbnail ) ) return;
- got.get(wiki + 'api.php?action=parse&prop=text|images|parsewarnings§ion=0&disablelimitreport=true&disableeditsection=true&disabletoc=true&page=' + encodeURIComponent( title ) + '&format=json').then( response => {
- if ( response?.body?.parse?.parsewarnings?.length ) log_warn(response.body.parse.parsewarnings);
- if ( response.statusCode !== 200 || !response?.body?.parse?.text ) {
- console.log( '- ' + response.statusCode + ': Error while parsing the page: ' + response?.body?.error?.info );
- return;
- }
- var change = false;
- var $ = cheerio.load(response.body.parse.text['*']);
- if ( embed.thumbnail?.url === thumbnail ) {
- var image = response.body.parse.images.find( pageimage => ( /\.(?:png|jpg|jpeg|gif)$/.test(pageimage.toLowerCase()) && pageimage.toLowerCase().includes( title.toLowerCase().replace( / /g, '_' ) ) ) );
- if ( !image ) {
- thumbnail = $('img').first().prop('src');
- if ( thumbnail && !/^(?:https?:)?\/\//.test(thumbnail) ) image = response.body.parse.images.find( pageimage => ( /\.(?:png|jpg|jpeg|gif)$/.test(pageimage.toLowerCase()) ) );
- }
- if ( image ) thumbnail = wiki.toLink('Special:FilePath/' + image);
- if ( thumbnail ) {
- embed.setThumbnail( thumbnail.replace( /^(?:https?:)?\/\//, 'https://' ) );
- change = true;
- }
- }
- if ( !embed.description ) {
- $('h1, h2, h3, h4, h5, h6').nextAll().remove();
- $('h1, h2, h3, h4, h5, h6').remove();
- $(removeClasses.join(', '), $('.mw-parser-output')).remove();
- var description = $.text().trim().escapeFormatting();
- if ( description ) {
- if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
- embed.setDescription( description );
- change = true;
- }
- }
-
- if ( change ) msg.edit( msg.content, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
- }, error => {
- console.log( '- Error while parsing the page: ' + error );
- } );
- }
- module.exports = parse_page;
|