parse_page.js 3.3 KB

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