parse_page.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const cheerio = require('cheerio');
  2. const {htmlToPlain} = require('../util/functions.js');
  3. const infoboxList = [
  4. '.infobox',
  5. '.portable-infobox',
  6. '.infoboxtable',
  7. '.notaninfobox'
  8. ];
  9. const removeClasses = [
  10. 'table',
  11. 'div',
  12. 'script',
  13. 'input',
  14. 'style',
  15. 'script',
  16. 'noscript',
  17. 'ul.gallery',
  18. '.mw-editsection',
  19. 'sup.reference',
  20. 'ol.references',
  21. '.error',
  22. '.nomobile',
  23. '.noprint',
  24. '.noexcerpt',
  25. '.sortkey'
  26. ];
  27. const keepMainPageTag = [
  28. 'div.main-page-tag-lcs',
  29. 'div.lcs-container'
  30. ];
  31. /**
  32. * Parses a wiki page to get it's description.
  33. * @param {import('discord.js').Message} msg - The Discord message.
  34. * @param {String} title - The title of the page.
  35. * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
  36. * @param {import('../util/wiki.js')} wiki - The wiki for the page.
  37. * @param {String} thumbnail - The default thumbnail for the wiki.
  38. */
  39. function parse_page(msg, title, embed, wiki, thumbnail) {
  40. if ( !msg || ( embed.description && embed.thumbnail?.url !== thumbnail && !embed.brokenInfobox ) ) {
  41. return;
  42. }
  43. got.get( wiki + 'api.php?action=parse&prop=text|images&section=0&disablelimitreport=true&disableeditsection=true&disabletoc=true&page=' + encodeURIComponent( title ) + '&format=json' ).then( response => {
  44. if ( response.statusCode !== 200 || !response?.body?.parse?.text ) {
  45. console.log( '- ' + response.statusCode + ': Error while parsing the page: ' + response?.body?.error?.info );
  46. return;
  47. }
  48. var change = false;
  49. var $ = cheerio.load(response.body.parse.text['*'].replace( /<br\/?>/g, '\n' ));
  50. if ( embed.brokenInfobox && $('aside.portable-infobox').length ) {
  51. var infobox = $('aside.portable-infobox');
  52. embed.fields.forEach( field => {
  53. if ( embed.length > 5500 ) return;
  54. if ( /^`.+`$/.test(field.name) ) {
  55. let label = infobox.find(field.name.replace( /^`(.+)`$/, '[data-source="$1"] .pi-data-label, .pi-data-label[data-source="$1"]' )).html();
  56. label = htmlToPlain(label).trim();
  57. if ( label.length > 50 ) label = label.substring(0, 50) + '\u2026';
  58. if ( label ) field.name = label;
  59. }
  60. if ( /^`.+`$/.test(field.value) ) {
  61. let value = infobox.find(field.value.replace( /^`(.+)`$/, '[data-source="$1"] .pi-data-value, .pi-data-value[data-source="$1"]' )).html();
  62. value = htmlToPlain(value).trim();
  63. if ( value.length > 250 ) value = value.substring(0, 250) + '\u2026';
  64. if ( value ) field.value = value;
  65. }
  66. } );
  67. change = true;
  68. }
  69. if ( embed.thumbnail?.url === thumbnail ) {
  70. var image = response.body.parse.images.find( pageimage => ( /\.(?:png|jpg|jpeg|gif)$/.test(pageimage.toLowerCase()) && pageimage.toLowerCase().includes( title.toLowerCase().replace( / /g, '_' ) ) ) );
  71. if ( !image ) {
  72. thumbnail = $(infoboxList.join(', ')).find('img').filter( (i, img) => {
  73. img = $(img).prop('src')?.toLowerCase();
  74. return ( /^(?:https?:)?\/\//.test(img) && /\.(?:png|jpg|jpeg|gif)(?:\/|\?|$)/.test(img) );
  75. } ).first().prop('src');
  76. if ( !thumbnail ) thumbnail = $('img').filter( (i, img) => {
  77. img = $(img).prop('src')?.toLowerCase();
  78. return ( /^(?:https?:)?\/\//.test(img) && /\.(?:png|jpg|jpeg|gif)(?:\/|\?|$)/.test(img) );
  79. } ).first().prop('src');
  80. if ( !thumbnail ) image = response.body.parse.images.find( pageimage => {
  81. return /\.(?:png|jpg|jpeg|gif)$/.test(pageimage.toLowerCase());
  82. } );
  83. }
  84. if ( image ) thumbnail = wiki.toLink('Special:FilePath/' + image);
  85. if ( thumbnail ) {
  86. embed.setThumbnail( thumbnail.replace( /^(?:https?:)?\/\//, 'https://' ) );
  87. change = true;
  88. }
  89. }
  90. if ( !embed.description && embed.length < 5000 ) {
  91. $('h1, h2, h3, h4, h5, h6').nextAll().remove();
  92. $('h1, h2, h3, h4, h5, h6').remove();
  93. $(infoboxList.join(', ')).remove();
  94. $(removeClasses.join(', '), $('.mw-parser-output')).not(keepMainPageTag.join(', ')).remove();
  95. var description = $.text().trim().replace( /\n{3,}/g, '\n\n' ).escapeFormatting();
  96. if ( description ) {
  97. if ( description.length > 1000 ) description = description.substring(0, 1000) + '\u2026';
  98. embed.setDescription( description );
  99. change = true;
  100. }
  101. }
  102. if ( change ) msg.edit( msg.content, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  103. }, error => {
  104. console.log( '- Error while parsing the page: ' + error );
  105. } );
  106. }
  107. module.exports = parse_page;