webshot.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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}',
  29. }))
  30. .then(() => page.waitFor(webshotDelay))
  31. .then(() => page.screenshot())
  32. .then(screenshot => {
  33. new pngjs_1.PNG({
  34. filterType: 4,
  35. }).on('parsed', function () {
  36. let boundary = null;
  37. let x = 0;
  38. for (let y = 0; y < this.height; y++) {
  39. const idx = (this.width * y + x) << 2;
  40. if (this.data[idx] !== 255) {
  41. boundary = y;
  42. break;
  43. }
  44. }
  45. if (boundary !== null) {
  46. logger.info(`found boundary at ${boundary}, cropping image`);
  47. this.data = this.data.slice(0, (this.width * boundary) << 2);
  48. this.height = boundary;
  49. boundary = null;
  50. x = Math.floor(this.width / 2);
  51. for (let y = this.height - 1; y >= 0; y--) {
  52. const idx = (this.width * y + x) << 2;
  53. if (this.data[idx] !== 255) {
  54. boundary = y;
  55. break;
  56. }
  57. }
  58. if (boundary != null) {
  59. logger.info(`found boundary at ${boundary}, trimming image`);
  60. this.data = this.data.slice(0, (this.width * boundary) << 2);
  61. this.height = boundary;
  62. }
  63. read(this.pack(), 'base64').then(data => {
  64. logger.info(`finished webshot for ${url}`);
  65. resolve({ data, boundary });
  66. });
  67. }
  68. else if (height >= 8 * 1920) {
  69. logger.warn('too large, consider as a bug, returning');
  70. read(this.pack(), 'base64').then(data => {
  71. logger.info(`finished webshot for ${url}`);
  72. resolve({ data, boundary: 0 });
  73. });
  74. }
  75. else {
  76. logger.info('unable to found boundary, try shooting a larger image');
  77. resolve({ data: '', boundary });
  78. }
  79. }).parse(screenshot);
  80. })
  81. .then(() => page.close());
  82. });
  83. });
  84. return promise.then(data => {
  85. if (data.boundary === null)
  86. return this.renderWebshot(url, height + 1920, webshotDelay);
  87. else
  88. return data.data;
  89. });
  90. };
  91. this.fetchImage = (url) => new Promise(resolve => {
  92. logger.info(`fetching ${url}`);
  93. https.get(url, res => {
  94. if (res.statusCode === 200) {
  95. read(res, 'base64').then(data => {
  96. logger.info(`successfully fetched ${url}`);
  97. resolve(data);
  98. });
  99. }
  100. else {
  101. logger.error(`failed to fetch ${url}: ${res.statusCode}`);
  102. resolve();
  103. }
  104. }).on('error', (err) => {
  105. logger.error(`failed to fetch ${url}: ${err.message}`);
  106. resolve();
  107. });
  108. });
  109. puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--lang=zh-CN,zh'] })
  110. .then(browser => this.browser = browser)
  111. .then(() => {
  112. logger.info('launched puppeteer browser');
  113. if (onready)
  114. onready();
  115. });
  116. }
  117. webshot(tweets, callback, webshotDelay) {
  118. let promise = new Promise(resolve => {
  119. resolve();
  120. });
  121. tweets.forEach(twi => {
  122. let cqstr = '';
  123. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  124. promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay))
  125. .then(base64Webshot => {
  126. if (base64Webshot)
  127. cqstr += `[CQ:image,file=base64://${base64Webshot}]`;
  128. });
  129. if (twi.extended_entities) {
  130. twi.extended_entities.media.forEach(media => {
  131. promise = promise.then(() => this.fetchImage(media.media_url_https))
  132. .then(base64Image => {
  133. if (base64Image)
  134. cqstr += `[CQ:image,file=base64://${base64Image}]`;
  135. });
  136. });
  137. }
  138. if (twi.entities && twi.entities.urls && twi.entities.urls.length) {
  139. promise = promise.then(() => {
  140. const urls = twi.entities.urls
  141. .filter(urlObj => urlObj.indices[0] < twi.display_text_range[1])
  142. .map(urlObj => urlObj.expanded_url);
  143. if (urls.length) {
  144. cqstr += '\n';
  145. cqstr += urls.join('\n');
  146. }
  147. });
  148. }
  149. promise.then(() => callback(cqstr));
  150. });
  151. return promise;
  152. }
  153. }
  154. exports.default = Webshot;