webshot.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. webshot(url, options).pipe(new pngjs_1.PNG({
  22. filterType: 4
  23. }))
  24. .on('parsed', function () {
  25. let boundary = null;
  26. for (let y = 0; y < this.height; y++) {
  27. const x = 0;
  28. let idx = (this.width * y + x) << 2;
  29. if (this.data[idx] !== 255) {
  30. boundary = y;
  31. break;
  32. }
  33. }
  34. if (boundary != null) {
  35. this.data = this.data.slice(0, (this.width * boundary) << 2);
  36. this.height = boundary;
  37. read(this.pack(), 'base64').then(data => {
  38. resolve({ data, boundary });
  39. });
  40. }
  41. else {
  42. resolve({ data: '', boundary });
  43. }
  44. });
  45. });
  46. return promise.then(data => {
  47. if (data.boundary != null)
  48. return renderWebshot(url, height * 2);
  49. else
  50. return data.data;
  51. });
  52. }
  53. /*function fetchImage(): Promise<string> {
  54. }*/
  55. function default_1(twitter, callback) {
  56. twitter.forEach(twi => {
  57. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  58. renderWebshot(url, 1920)
  59. .then(base64Webshot => {
  60. console.log(base64Webshot);
  61. });
  62. });
  63. }
  64. exports.default = default_1;