webshot.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const CallableInstance = require("callable-instance");
  4. const https = require("https");
  5. const log4js = require("log4js");
  6. const pngjs_1 = require("pngjs");
  7. const puppeteer = require("puppeteer");
  8. const read = require("read-all-stream");
  9. const logger = log4js.getLogger('webshot');
  10. logger.level = global.loglevel;
  11. class Webshot extends CallableInstance {
  12. constructor(onready) {
  13. super('webshot');
  14. this.renderWebshot = (url, height, webshotDelay) => {
  15. const promise = new Promise(resolve => {
  16. const width = 1080;
  17. logger.info(`shooting ${width}*${height} webshot for ${url}`);
  18. this.browser.newPage()
  19. .then(page => {
  20. page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')
  21. .then(() => page.setViewport({
  22. width,
  23. height,
  24. isMobile: true,
  25. }))
  26. .then(() => page.goto(url))
  27. .then(() => page.addStyleTag({
  28. content: 'html{zoom:2}header{display:none!important}path[d=\'M20.207 7.043a1 1 0 0 0-1.414 0L12 13.836 5.207 7.043a1 1 0 0 0-1.414 1.414l7.5 7.5a.996.996 0 0 0 1.414 0l7.5-7.5a1 1 0 0 0 0-1.414z\'],div[role=\'button\']{display: none;}',
  29. }))
  30. .then(() => page.waitFor(webshotDelay))
  31. .then(() => page.addScriptTag({
  32. content: 'document.documentElement.scrollTop=0;',
  33. }))
  34. .then(() => page.screenshot())
  35. .then(screenshot => {
  36. new pngjs_1.PNG({
  37. filterType: 4,
  38. }).on('parsed', function () {
  39. let boundary = null;
  40. let x = 0;
  41. for (let y = 0; y < this.height; y++) {
  42. const idx = (this.width * y + x) << 2;
  43. if (this.data[idx] !== 255) {
  44. boundary = y;
  45. break;
  46. }
  47. }
  48. if (boundary !== null) {
  49. logger.info(`found boundary at ${boundary}, cropping image`);
  50. this.data = this.data.slice(0, (this.width * boundary) << 2);
  51. this.height = boundary;
  52. boundary = null;
  53. x = Math.floor(this.width / 2);
  54. let flag = 0;
  55. for (let y = this.height - 1; y >= 0; y--) {
  56. const idx = (this.width * y + x) << 2;
  57. if (this.data[idx] !== 255) {
  58. boundary = y;
  59. if (!flag)
  60. flag = 1;
  61. else
  62. break;
  63. }
  64. }
  65. if (boundary != null) {
  66. logger.info(`found boundary at ${boundary}, trimming image`);
  67. this.data = this.data.slice(0, (this.width * boundary) << 2);
  68. this.height = boundary;
  69. }
  70. read(this.pack(), 'base64').then(data => {
  71. logger.info(`finished webshot for ${url}`);
  72. resolve({ data, boundary });
  73. });
  74. }
  75. else if (height >= 8 * 1920) {
  76. logger.warn('too large, consider as a bug, returning');
  77. read(this.pack(), 'base64').then(data => {
  78. logger.info(`finished webshot for ${url}`);
  79. resolve({ data, boundary: 0 });
  80. });
  81. }
  82. else {
  83. logger.info('unable to found boundary, try shooting a larger image');
  84. resolve({ data: '', boundary });
  85. }
  86. }).parse(screenshot);
  87. })
  88. .then(() => page.close());
  89. });
  90. });
  91. return promise.then(data => {
  92. if (data.boundary === null)
  93. return this.renderWebshot(url, height + 1920, webshotDelay);
  94. else
  95. return data.data;
  96. });
  97. };
  98. this.fetchImage = (url) => new Promise(resolve => {
  99. logger.info(`fetching ${url}`);
  100. https.get(url, res => {
  101. if (res.statusCode === 200) {
  102. read(res, 'base64').then(data => {
  103. logger.info(`successfully fetched ${url}`);
  104. resolve(data);
  105. });
  106. }
  107. else {
  108. logger.error(`failed to fetch ${url}: ${res.statusCode}`);
  109. resolve();
  110. }
  111. }).on('error', (err) => {
  112. logger.error(`failed to fetch ${url}: ${err.message}`);
  113. resolve();
  114. });
  115. });
  116. puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--lang=zh-CN,zh'] })
  117. .then(browser => this.browser = browser)
  118. .then(() => {
  119. logger.info('launched puppeteer browser');
  120. if (onready)
  121. onready();
  122. });
  123. }
  124. webshot(tweets, callback, webshotDelay) {
  125. let promise = new Promise(resolve => {
  126. resolve();
  127. });
  128. tweets.forEach(twi => {
  129. let cqstr = '';
  130. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  131. promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay))
  132. .then(base64Webshot => {
  133. if (base64Webshot)
  134. cqstr += `[CQ:image,file=base64://${base64Webshot}]`;
  135. });
  136. if (twi.extended_entities) {
  137. twi.extended_entities.media.forEach(media => {
  138. promise = promise.then(() => this.fetchImage(media.media_url_https))
  139. .then(base64Image => {
  140. if (base64Image)
  141. cqstr += `[CQ:image,file=base64://${base64Image}]`;
  142. });
  143. });
  144. }
  145. if (twi.entities && twi.entities.urls && twi.entities.urls.length) {
  146. promise = promise.then(() => {
  147. const urls = twi.entities.urls
  148. .filter(urlObj => urlObj.indices[0] < twi.display_text_range[1])
  149. .map(urlObj => urlObj.expanded_url);
  150. if (urls.length) {
  151. cqstr += '\n';
  152. cqstr += urls.join('\n');
  153. }
  154. });
  155. }
  156. promise.then(() => callback(cqstr));
  157. });
  158. return promise;
  159. }
  160. }
  161. exports.default = Webshot;