Procházet zdrojové kódy

fix saving media, fix missing blockquote id

Mike L před 4 roky
rodič
revize
5843c43bc5
2 změnil soubory, kde provedl 15 přidání a 9 odebrání
  1. 7 3
      dist/webshot.js
  2. 8 6
      src/webshot.ts

+ 7 - 3
dist/webshot.js

@@ -232,7 +232,7 @@ class Webshot extends CallableInstance {
             axios_1.default({
                 method: 'get',
                 url,
-                responseType: 'stream',
+                responseType: 'arraybuffer',
                 timeout: 150000,
             }).then(res => {
                 if (res.status === 200) {
@@ -251,7 +251,7 @@ class Webshot extends CallableInstance {
             var _a;
             return (ext => {
                 const mediaTempFilePath = temp.path({ suffix: `.${ext}` });
-                data.pipe(fs_1.createWriteStream(mediaTempFilePath));
+                fs_1.writeFileSync(mediaTempFilePath, Buffer.from(data));
                 const path = `file://${mediaTempFilePath}`;
                 switch (ext) {
                     case 'jpg':
@@ -358,7 +358,11 @@ class Webshot extends CallableInstance {
             }
             if (originTwi.is_quote_status) {
                 promise = promise.then(() => {
-                    messageChain += `\n回复此命令查看引用的推文:\n/twitter_view ${originTwi.quoted_status.id_str}`;
+                    var _a, _b;
+                    const match = /\/status\/(\d+)/.exec((_a = originTwi.quoted_status_permalink) === null || _a === void 0 ? void 0 : _a.expanded);
+                    const blockQuoteIdStr = match ? match[1] : (_b = originTwi.quoted_status) === null || _b === void 0 ? void 0 : _b.id_str;
+                    if (blockQuoteIdStr)
+                        messageChain += `\n回复此命令查看引用的推文:\n/twitter_view ${blockQuoteIdStr}`;
                 });
             }
             promise.then(() => {

+ 8 - 6
src/webshot.ts

@@ -1,5 +1,5 @@
-import { createWriteStream } from 'fs';
-import { Readable, Stream } from 'stream';
+import { writeFileSync } from 'fs';
+import { Readable } from 'stream';
 import { promisify } from 'util';
 
 import axios from 'axios';
@@ -257,12 +257,12 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
     );
   };
 
-  private fetchMedia = (url: string): Promise<string> => new Promise<Stream>((resolve, reject) => {
+  private fetchMedia = (url: string): Promise<string> => new Promise<ArrayBuffer>((resolve, reject) => {
     logger.info(`fetching ${url}`);
     axios({
       method: 'get',
       url,
-      responseType: 'stream',
+      responseType: 'arraybuffer',
       timeout: 150000,
     }).then(res => {
       if (res.status === 200) {
@@ -279,7 +279,7 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
   }).then(data =>
     (ext => {
       const mediaTempFilePath = temp.path({suffix: `.${ext}`});
-      data.pipe(createWriteStream(mediaTempFilePath));
+      writeFileSync(mediaTempFilePath, Buffer.from(data));
       const path = `file://${mediaTempFilePath}`;
       switch (ext) {
         case 'jpg':
@@ -390,7 +390,9 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
       // refer to quoted tweet, if any
       if (originTwi.is_quote_status) {
         promise = promise.then(() => {
-          messageChain += `\n回复此命令查看引用的推文:\n/twitter_view ${originTwi.quoted_status.id_str}`;
+          const match = /\/status\/(\d+)/.exec(originTwi.quoted_status_permalink?.expanded);
+          const blockQuoteIdStr = match ? match[1] : originTwi.quoted_status?.id_str;
+          if (blockQuoteIdStr) messageChain += `\n回复此命令查看引用的推文:\n/twitter_view ${blockQuoteIdStr}`;
         });
       }
       promise.then(() => {