parse_page.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const cheerio = require('cheerio');
  2. const removeClasses = [
  3. 'table',
  4. 'div',
  5. 'script',
  6. 'input',
  7. 'style',
  8. 'ul.gallery',
  9. '.mw-editsection',
  10. 'sup.reference',
  11. 'ol.references',
  12. '.error',
  13. '.nomobile',
  14. '.noprint',
  15. '.noexcerpt',
  16. '.sortkey'
  17. ];
  18. /**
  19. * Parses a wiki page to get it's description.
  20. * @param {import('discord.js').Message} msg - The Discord message.
  21. * @param {String} title - The title of the page.
  22. * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
  23. * @param {String} wiki - The wiki for the page.
  24. * @param {String} thumbnail - The default thumbnail for the wiki.
  25. */
  26. function parse_page(msg, title, embed, wiki, thumbnail) {
  27. if ( !msg || ( embed.description && embed.thumbnail?.url !== thumbnail ) ) return;
  28. got.get(wiki + 'api.php?action=parse&prop=text|images|parsewarnings&section=0&disablelimitreport=true&disableeditsection=true&disabletoc=true&page=' + encodeURIComponent( title ) + '&format=json').then( response => {
  29. if ( response?.body?.parse?.parsewarnings?.length ) log_warn(response.body.parse.parsewarnings);
  30. if ( response.statusCode !== 200 || !response?.body?.parse?.text ) {
  31. console.log( '- ' + response.statusCode + ': Error while parsing the page: ' + response?.body?.error?.info );
  32. return;
  33. }
  34. var change = false;
  35. var $ = cheerio.load(response.body.parse.text['*']);
  36. if ( embed.thumbnail?.url === thumbnail ) {
  37. var image = response.body.parse.images.find( pageimage => ( /\.(?:png|jpg|jpeg|gif)$/.test(pageimage.toLowerCase()) && pageimage.toLowerCase().includes( title.toLowerCase().replace( / /g, '_' ) ) ) );
  38. if ( !image ) {
  39. thumbnail = $('img').first().prop('src');
  40. if ( thumbnail && !/^(?:https?:)?\/\//.test(thumbnail) ) image = response.body.parse.images.find( pageimage => ( /\.(?:png|jpg|jpeg|gif)$/.test(pageimage.toLowerCase()) ) );
  41. }
  42. if ( image ) thumbnail = wiki.toLink('Special:FilePath/' + image);
  43. if ( thumbnail ) {
  44. embed.setThumbnail( thumbnail.replace( /^(?:https?:)?\/\//, 'https://' ) );
  45. change = true;
  46. }
  47. }
  48. if ( !embed.description ) {
  49. $('h1, h2, h3, h4, h5, h6').nextAll().remove();
  50. $('h1, h2, h3, h4, h5, h6').remove();
  51. $(removeClasses.join(', '), $('.mw-parser-output')).remove();
  52. var description = $.text().trim().escapeFormatting();
  53. if ( description ) {
  54. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  55. embed.setDescription( description );
  56. change = true;
  57. }
  58. }
  59. if ( change ) msg.edit( msg.content, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  60. }, error => {
  61. console.log( '- Error while parsing the page: ' + error );
  62. } );
  63. }
  64. module.exports = parse_page;