parse_page.js 2.9 KB

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