webshot.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const https = require("https");
  4. const log4js = require("log4js");
  5. const pngjs_1 = require("pngjs");
  6. const read = require("read-all-stream");
  7. const webshot = require("webshot");
  8. const logger = log4js.getLogger('webshot');
  9. logger.level = global.loglevel;
  10. function renderWebshot(url, height, webshotDelay) {
  11. const promise = new Promise(resolve => {
  12. const options = {
  13. windowSize: {
  14. width: 1080,
  15. height,
  16. },
  17. userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
  18. renderDelay: webshotDelay,
  19. quality: 100,
  20. customCSS: 'html{zoom:2}header{display:none!important}',
  21. };
  22. logger.info(`shooting ${options.windowSize.width}*${height} webshot for ${url}`);
  23. webshot(url, options).pipe(new pngjs_1.PNG({
  24. filterType: 4,
  25. }))
  26. .on('parsed', function () {
  27. let boundary = null;
  28. let x = 0;
  29. for (let y = 0; y < this.height; y++) {
  30. const idx = (this.width * y + x) << 2;
  31. if (this.data[idx] !== 255) {
  32. boundary = y;
  33. break;
  34. }
  35. }
  36. if (boundary !== null) {
  37. logger.info(`found boundary at ${boundary}, cropping image`);
  38. this.data = this.data.slice(0, (this.width * boundary) << 2);
  39. this.height = boundary;
  40. boundary = null;
  41. x = Math.floor(this.width / 2);
  42. for (let y = this.height - 1; y >= 0; 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}, trimming image`);
  51. this.data = this.data.slice(0, (this.width * boundary) << 2);
  52. this.height = boundary;
  53. }
  54. read(this.pack(), 'base64').then(data => {
  55. logger.info(`finished webshot for ${url}`);
  56. resolve({ data, boundary });
  57. });
  58. }
  59. else if (height >= 8 * 1920) {
  60. logger.warn('too large, consider as a bug, returning');
  61. read(this.pack(), 'base64').then(data => {
  62. logger.info(`finished webshot for ${url}`);
  63. resolve({ data, boundary: 0 });
  64. });
  65. }
  66. else {
  67. logger.info('unable to found boundary, try shooting a larger image');
  68. resolve({ data: '', boundary });
  69. }
  70. });
  71. });
  72. return promise.then(data => {
  73. if (data.boundary === null)
  74. return renderWebshot(url, height + 1920, webshotDelay);
  75. else
  76. return data.data;
  77. });
  78. }
  79. function fetchImage(url) {
  80. return new Promise(resolve => {
  81. logger.info(`fetching ${url}`);
  82. https.get(url, res => {
  83. if (res.statusCode === 200) {
  84. read(res, 'base64').then(data => {
  85. logger.info(`successfully fetched ${url}`);
  86. resolve(data);
  87. });
  88. }
  89. else {
  90. logger.error(`failed to fetch ${url}: ${res.statusCode}`);
  91. resolve();
  92. }
  93. }).on('error', (err) => {
  94. logger.error(`failed to fetch ${url}: ${err.message}`);
  95. resolve();
  96. });
  97. });
  98. }
  99. function default_1(tweets, callback, webshotDelay) {
  100. let promise = new Promise(resolve => {
  101. resolve();
  102. });
  103. tweets.forEach(twi => {
  104. let cqstr = '';
  105. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  106. promise = promise.then(() => renderWebshot(url, 1920, webshotDelay))
  107. .then(base64Webshot => {
  108. if (base64Webshot)
  109. cqstr += `[CQ:image,file=base64://${base64Webshot}]`;
  110. });
  111. if (twi.extended_entities) {
  112. twi.extended_entities.media.forEach(media => {
  113. promise = promise.then(() => fetchImage(media.media_url_https))
  114. .then(base64Image => {
  115. if (base64Image)
  116. cqstr += `[CQ:image,file=base64://${base64Image}]`;
  117. });
  118. });
  119. }
  120. promise.then(() => callback(cqstr));
  121. });
  122. return promise;
  123. }
  124. exports.default = default_1;