webshot.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const axios_1 = require("axios");
  13. const CallableInstance = require("callable-instance");
  14. const fs_1 = require("fs");
  15. const html_entities_1 = require("html-entities");
  16. const message_1 = require("mirai-ts/dist/message");
  17. const pngjs_1 = require("pngjs");
  18. const puppeteer = require("puppeteer");
  19. const loggers_1 = require("./loggers");
  20. const writeOutTo = (path, data) => __awaiter(void 0, void 0, void 0, function* () {
  21. yield new Promise(resolve => data.pipe(fs_1.createWriteStream(path)).on('close', resolve));
  22. return path;
  23. });
  24. const xmlEntities = new html_entities_1.XmlEntities();
  25. const typeInZH = {
  26. photo: '[图片]',
  27. video: '[视频]',
  28. animated_gif: '[GIF]',
  29. };
  30. const logger = loggers_1.getLogger('webshot');
  31. const mkdirP = dir => { if (!fs_1.existsSync(dir))
  32. fs_1.mkdirSync(dir, { recursive: true }); };
  33. const baseName = path => path.split(/[/\\]/).slice(-1)[0];
  34. class Webshot extends CallableInstance {
  35. constructor(outDir, mode, onready) {
  36. super('webshot');
  37. this.renderWebshot = (url, height, webshotDelay) => {
  38. const writeOutPic = (pic) => writeOutTo(`${this.outDir}/${url.replace(/[:\/]/g, '_')}.png`, pic);
  39. const promise = new Promise(resolve => {
  40. const width = 600;
  41. logger.info(`shooting ${width}*${height} webshot for ${url}`);
  42. this.browser.newPage()
  43. .then(page => {
  44. page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')
  45. .then(() => page.setViewport({
  46. width,
  47. height,
  48. isMobile: true,
  49. }))
  50. .then(() => page.setBypassCSP(true))
  51. .then(() => page.goto(url, { waitUntil: 'load', timeout: 150000 }))
  52. // hide header, "more options" button, like and retweet count
  53. .then(() => page.addStyleTag({
  54. content: '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;}',
  55. }))
  56. .then(() => page.waitFor(webshotDelay))
  57. .then(() => page.addScriptTag({
  58. content: 'document.documentElement.scrollTop=0;',
  59. }))
  60. .then(() => page.screenshot())
  61. .then(screenshot => {
  62. new pngjs_1.PNG({
  63. filterType: 4,
  64. }).on('parsed', function () {
  65. // remove comment area
  66. let boundary = null;
  67. let x = 0;
  68. for (let y = 0; y < this.height; y++) {
  69. const idx = (this.width * y + x) << 2;
  70. if (this.data[idx] !== 255) {
  71. boundary = y;
  72. break;
  73. }
  74. }
  75. if (boundary !== null) {
  76. logger.info(`found boundary at ${boundary}, cropping image`);
  77. this.data = this.data.slice(0, (this.width * boundary) << 2);
  78. this.height = boundary;
  79. boundary = null;
  80. x = Math.floor(this.width / 2);
  81. let flag = false;
  82. let cnt = 0;
  83. for (let y = this.height - 1; y >= 0; y--) {
  84. const idx = (this.width * y + x) << 2;
  85. if ((this.data[idx] === 255) === flag) {
  86. cnt++;
  87. flag = !flag;
  88. }
  89. else
  90. continue;
  91. // line above the "comment", "retweet", "like", "share" button row
  92. if (cnt === 6) {
  93. boundary = y + 1;
  94. }
  95. // if there are a "retweet" count and "like" count row, this will be the line above it
  96. if (cnt === 8) {
  97. const b = y + 1;
  98. if (this.height - b <= 200)
  99. boundary = b;
  100. break;
  101. }
  102. }
  103. if (boundary != null) {
  104. logger.info(`found boundary at ${boundary}, trimming image`);
  105. this.data = this.data.slice(0, (this.width * boundary) << 2);
  106. this.height = boundary;
  107. }
  108. writeOutPic(this.pack()).then(data => {
  109. logger.info(`finished webshot for ${url}`);
  110. resolve({ data, boundary });
  111. });
  112. }
  113. else if (height >= 8 * 1920) {
  114. logger.warn('too large, consider as a bug, returning');
  115. writeOutPic(this.pack()).then(data => {
  116. resolve({ data, boundary: 0 });
  117. });
  118. }
  119. else {
  120. logger.info('unable to find boundary, try shooting a larger image');
  121. resolve({ data: '', boundary });
  122. }
  123. }).parse(screenshot);
  124. })
  125. .then(() => page.close());
  126. });
  127. });
  128. return promise.then(data => {
  129. if (data.boundary === null)
  130. return this.renderWebshot(url, height + 1920, webshotDelay);
  131. else
  132. return data.data;
  133. });
  134. };
  135. this.fetchImage = (url, tag) => new Promise(resolve => {
  136. logger.info(`fetching ${url}`);
  137. axios_1.default({
  138. method: 'get',
  139. url,
  140. responseType: 'stream',
  141. }).then(res => {
  142. if (res.status === 200) {
  143. logger.info(`successfully fetched ${url}`);
  144. resolve(res.data);
  145. }
  146. else {
  147. logger.error(`failed to fetch ${url}: ${res.status}`);
  148. resolve();
  149. }
  150. }).catch(err => {
  151. logger.error(`failed to fetch ${url}: ${err.message}`);
  152. resolve();
  153. });
  154. }).then(data => writeOutTo(`${this.outDir}/${tag}${baseName(url)}`, data));
  155. mkdirP(this.outDir = outDir);
  156. // tslint:disable-next-line: no-conditional-assignment
  157. if (this.mode = mode) {
  158. onready();
  159. }
  160. else {
  161. puppeteer.launch({
  162. args: [
  163. '--no-sandbox',
  164. '--disable-setuid-sandbox',
  165. '--disable-dev-shm-usage',
  166. '--disable-accelerated-2d-canvas',
  167. '--no-first-run',
  168. '--no-zygote',
  169. '--single-process',
  170. '--disable-gpu',
  171. '--lang=ja-JP,ja',
  172. ]
  173. })
  174. .then(browser => this.browser = browser)
  175. .then(() => {
  176. logger.info('launched puppeteer browser');
  177. if (onready)
  178. onready();
  179. });
  180. }
  181. }
  182. webshot(tweets, callback, webshotDelay) {
  183. let promise = new Promise(resolve => {
  184. resolve();
  185. });
  186. tweets.forEach(twi => {
  187. promise = promise.then(() => {
  188. logger.info(`working on ${twi.user.screen_name}/${twi.id_str}`);
  189. });
  190. const originTwi = twi.retweeted_status || twi;
  191. const messageChain = [];
  192. // text processing
  193. let author = `${twi.user.name} (@${twi.user.screen_name}):\n`;
  194. if (twi.retweeted_status)
  195. author += `RT @${twi.retweeted_status.user.screen_name}: `;
  196. let text = originTwi.full_text;
  197. promise = promise.then(() => {
  198. if (originTwi.entities && originTwi.entities.urls && originTwi.entities.urls.length) {
  199. originTwi.entities.urls.forEach(url => {
  200. text = text.replace(new RegExp(url.url, 'gm'), url.expanded_url);
  201. });
  202. }
  203. if (originTwi.extended_entities) {
  204. originTwi.extended_entities.media.forEach(media => {
  205. text = text.replace(new RegExp(media.url, 'gm'), this.mode === 1 ? typeInZH[media.type] : '');
  206. });
  207. }
  208. if (this.mode > 0)
  209. messageChain.push(message_1.default.Plain(author + xmlEntities.decode(text)));
  210. });
  211. // invoke webshot
  212. if (this.mode === 0) {
  213. const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;
  214. promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay))
  215. .then(webshotFilePath => {
  216. if (webshotFilePath)
  217. messageChain.push(message_1.default.Image('', '', baseName(webshotFilePath)));
  218. });
  219. // fetch extra images
  220. }
  221. else if (1 - this.mode % 2) {
  222. if (originTwi.extended_entities) {
  223. promise = promise.then(() => originTwi.extended_entities.media.forEach(media => {
  224. this.fetchImage(media.media_url_https, `${twi.user.screen_name}-${twi.id_str}--`)
  225. .then(path => messageChain.push(message_1.default.Image('', '', baseName(path))));
  226. }));
  227. }
  228. // append URLs, if any
  229. }
  230. else if (this.mode === 0) {
  231. if (originTwi.entities && originTwi.entities.urls && originTwi.entities.urls.length) {
  232. promise = promise.then(() => {
  233. const urls = originTwi.entities.urls
  234. .filter(urlObj => urlObj.indices[0] < originTwi.display_text_range[1])
  235. .map(urlObj => urlObj.expanded_url);
  236. if (urls.length) {
  237. messageChain.push(message_1.default.Plain(urls.join('\n')));
  238. }
  239. });
  240. }
  241. }
  242. promise.then(() => {
  243. logger.info(`done working on ${twi.user.screen_name}/${twi.id_str}, message chain:`);
  244. logger.info(JSON.stringify(messageChain));
  245. callback(messageChain, text, author);
  246. });
  247. });
  248. return promise;
  249. }
  250. }
  251. exports.default = Webshot;