parse_page.js 2.9 KB

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