webshot.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const http = require("http");
  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 = 'info';
  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. for (let y = 0; y < this.height; y++) {
  29. const x = 0;
  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. read(this.pack(), 'base64').then(data => {
  41. logger.info(`finished webshot for ${url}`);
  42. resolve({ data, boundary });
  43. });
  44. }
  45. else if (height >= 8 * 1920) {
  46. logger.warn('too large, consider as a bug, returning');
  47. read(this.pack(), 'base64').then(data => {
  48. logger.info(`finished webshot for ${url}`);
  49. resolve({ data, boundary: 0 });
  50. });
  51. }
  52. else {
  53. logger.info('unable to found boundary, try shooting a larger image');
  54. resolve({ data: '', boundary });
  55. }
  56. });
  57. });
  58. return promise.then(data => {
  59. if (data.boundary === null)
  60. return renderWebshot(url, height + 1920, webshotDelay);
  61. else
  62. return data.data;
  63. });
  64. }
  65. function fetchImage(url) {
  66. return new Promise(resolve => {
  67. logger.info(`fetching ${url}`);
  68. http.get(url, res => {
  69. if (res.statusCode === 200) {
  70. read(res, 'base64').then(data => {
  71. logger.info(`successfully fetched ${url}`);
  72. return data;
  73. });
  74. }
  75. });
  76. });
  77. }
  78. function default_1(tweets, callback, webshotDelay) {
  79. tweets.forEach(twi => {
  80. let cqstr = '';
  81. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  82. let promise = renderWebshot(url, 1920, webshotDelay)
  83. .then(base64Webshot => {
  84. if (base64Webshot)
  85. cqstr += `[CQ:image,file=base64://${base64Webshot}]`;
  86. });
  87. if (twi.extended_entities) {
  88. twi.extended_entities.media.forEach(media => {
  89. promise = promise.then(() => fetchImage(media.media_url_https))
  90. .then(base64Image => {
  91. if (base64Image)
  92. cqstr += `[CQ:image,file=base64://${base64Image}]`;
  93. });
  94. });
  95. }
  96. // TODO: Translate
  97. promise.then(() => callback(cqstr));
  98. });
  99. }
  100. exports.default = default_1;