webshot.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const log4js = require("log4js");
  4. const webshot = require("webshot");
  5. const read = require("read-all-stream");
  6. const pngjs_1 = require("pngjs");
  7. const logger = log4js.getLogger('webshot');
  8. logger.level = 'info';
  9. function renderWebshot(url, height) {
  10. let promise = new Promise(resolve => {
  11. const options = {
  12. windowSize: {
  13. width: 1080,
  14. height: height,
  15. },
  16. userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
  17. renderDelay: 5000,
  18. quality: 100,
  19. customCSS: 'html{zoom:2}header{display:none!important}',
  20. };
  21. logger.info(`shooting ${options.windowSize.width}*${height} webshot for ${url}`);
  22. webshot(url, options).pipe(new pngjs_1.PNG({
  23. filterType: 4
  24. }))
  25. .on('parsed', function () {
  26. let boundary = null;
  27. for (let y = 0; y < this.height; y++) {
  28. const x = 0;
  29. let idx = (this.width * y + x) << 2;
  30. if (this.data[idx] !== 255) {
  31. boundary = y;
  32. break;
  33. }
  34. }
  35. if (boundary != null) {
  36. logger.info(`found boundary at ${boundary}, cropping image`);
  37. this.data = this.data.slice(0, (this.width * boundary) << 2);
  38. this.height = boundary;
  39. read(this.pack(), 'base64').then(data => {
  40. logger.info(`finished webshot for ${url}`);
  41. resolve({ data, boundary });
  42. });
  43. }
  44. else {
  45. logger.warn(`unable to found boundary, try shooting a larger image`);
  46. resolve({ data: '', boundary });
  47. }
  48. });
  49. });
  50. return promise.then(data => {
  51. if (data.boundary != null)
  52. return renderWebshot(url, height * 2);
  53. else
  54. return data.data;
  55. });
  56. }
  57. /*function fetchImage(): Promise<string> {
  58. }*/
  59. function default_1(twitter, callback) {
  60. twitter.forEach(twi => {
  61. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  62. renderWebshot(url, 1920)
  63. .then(base64Webshot => {
  64. console.log(base64Webshot);
  65. });
  66. });
  67. }
  68. exports.default = default_1;