webshot.js 8.0 KB

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