Bladeren bron

Merge branch 'koishi-production' into mediaonly-koishi

Mike L 3 jaren geleden
bovenliggende
commit
216d52e561
6 gewijzigde bestanden met toevoegingen van 22 en 36 verwijderingen
  1. 4 4
      dist/koishi.js
  2. 3 5
      dist/twitter.js
  3. 4 9
      dist/webshot.js
  4. 3 4
      src/koishi.ts
  5. 4 5
      src/twitter.ts
  6. 4 9
      src/webshot.ts

+ 4 - 4
dist/koishi.js

@@ -90,7 +90,7 @@ class default_1 {
         this.sendToUser = (userID, message) => new Promise((resolve, reject) => {
             this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve).catch(reject));
         });
-        this.sendTo = (subscriber, messageChain) => Promise.all((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
+        this.sendTo = (subscriber, messageChain, noErrors = false) => Promise.all((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
             switch (subscriber.chatType) {
                 case 'group':
                     return this.sendToGroup(subscriber.chatID.toString(), msg);
@@ -106,9 +106,9 @@ class default_1 {
             logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response: ${response}`);
         })
             .catch(reason => {
-            reason = exports.Message.ellipseBase64(reason);
-            logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
-            throw Error(reason);
+            logger.error(exports.Message.ellipseBase64(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`));
+            if (!noErrors)
+                throw reason instanceof Error ? reason : Error(reason);
         });
         this.initBot = () => {
             this.app = new koishi_1.App({

+ 3 - 5
dist/twitter.js

@@ -14,7 +14,6 @@ const fs = require("fs");
 const path = require("path");
 const Twitter = require("twitter");
 const loggers_1 = require("./loggers");
-const koishi_1 = require("./koishi");
 const utils_1 = require("./utils");
 const webshot_1 = require("./webshot");
 class ScreenNameNormalizer {
@@ -142,15 +141,14 @@ class default_1 {
         };
         this.sendTweets = (source, ...to) => (msg, text, author) => {
             to.forEach(subscriber => {
-                logger.info(`pushing data${source ? ` of ${koishi_1.Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
+                logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
                 retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
                     if (count <= maxTrials) {
                         logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
                     }
                     else {
-                        logger.warn(`${count - 1} consecutive failures while sending` +
-                            'message chain, trying plain text instead...');
-                        terminate(this.bot.sendTo(subscriber, author + text));
+                        logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
+                        terminate(this.bot.sendTo(subscriber, author + text, true));
                     }
                 });
             });

+ 4 - 9
dist/webshot.js

@@ -80,7 +80,7 @@ class Webshot extends CallableInstance {
                     })
                         .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
                         .then(() => page.addStyleTag({
-                        content: 'header{display:none!important}[data-testid="caret"],[data-testid="tweet"]+*>:nth-last-child(-n+2){display:none}',
+                        content: 'header,#layers{display:none!important}[data-testid="caret"],[role="group"],[data-testid="tweet"]+*>:nth-last-child(2){display:none}',
                     }))
                         .then(() => page.addStyleTag({
                         content: '*{font-family:-apple-system,".Helvetica Neue DeskInterface",Hiragino Sans,Hiragino Sans GB,sans-serif!important}',
@@ -147,15 +147,10 @@ class Webshot extends CallableInstance {
                             const idx = (x, y) => (this.width * y + x) << 2;
                             let boundary = null;
                             const x = zoomFactor * 2;
-                            for (let y = 0; y < this.height; y += zoomFactor) {
-                                if (this.data[idx(x, y)] !== 255 &&
+                            for (let y = x; y < this.height; y += zoomFactor) {
+                                if (this.data[idx(x, y)] !== this.data[idx(x, y - zoomFactor)] &&
                                     this.data[idx(x, y)] === this.data[idx(x + zoomFactor * 10, y)]) {
-                                    if (this.data[idx(x, y + 18 * zoomFactor)] !== 255) {
-                                        boundary = null;
-                                    }
-                                    else {
-                                        boundary = y;
-                                    }
+                                    boundary = y;
                                     break;
                                 }
                             }

+ 3 - 4
src/koishi.ts

@@ -104,7 +104,7 @@ export default class {
     this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve).catch(reject));
   });
 
-  public sendTo = (subscriber: IChat, messageChain: string) => Promise.all(
+  public sendTo = (subscriber: IChat, messageChain: string, noErrors = false) => Promise.all(
     (splitted => [splitted.message, ...splitted.attachments])(
       Message.separateAttachment(messageChain)
     ).map(msg => {
@@ -122,9 +122,8 @@ export default class {
       logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response: ${response}`);
     })
     .catch(reason => {
-      reason = Message.ellipseBase64(reason);
-      logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
-      throw Error(reason);
+      logger.error(Message.ellipseBase64(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`));
+      if (!noErrors) throw reason instanceof Error ? reason : Error(reason);
     });
 
   private initBot = () => {

+ 4 - 5
src/twitter.ts

@@ -4,7 +4,7 @@ import * as Twitter from 'twitter';
 import TwitterTypes from 'twitter-d';
 
 import { getLogger } from './loggers';
-import QQBot, { Message } from './koishi';
+import QQBot from './koishi';
 import { chainPromises, BigNumOps } from './utils';
 import Webshot from './webshot';
 
@@ -280,16 +280,15 @@ export default class {
 
   private sendTweets = (source?: string, ...to: IChat[]) => (msg: string, text: string, author: string) => {
     to.forEach(subscriber => {
-      logger.info(`pushing data${source ? ` of ${Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
+      logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
       retryOnError(
         () => this.bot.sendTo(subscriber, msg),
         (_, count, terminate: (doNothing: Promise<void>) => void) => {
           if (count <= maxTrials) {
             logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
           } else {
-            logger.warn(`${count - 1} consecutive failures while sending` +
-            'message chain, trying plain text instead...');
-            terminate(this.bot.sendTo(subscriber, author + text));
+            logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
+            terminate(this.bot.sendTo(subscriber, author + text, true));
           }
         });
     });

+ 4 - 9
src/webshot.ts

@@ -106,7 +106,7 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
             .then(() => page.goto(url, {waitUntil: 'load', timeout: getTimeout()}))
             // hide header, "more options" button, like and retweet count
             .then(() => page.addStyleTag({
-              content: 'header{display:none!important}[data-testid="caret"],[data-testid="tweet"]+*>:nth-last-child(-n+2){display:none}',
+              content: 'header,#layers{display:none!important}[data-testid="caret"],[role="group"],[data-testid="tweet"]+*>:nth-last-child(2){display:none}',
             }))
             .then(() => page.addStyleTag({
               content: '*{font-family:-apple-system,".Helvetica Neue DeskInterface",Hiragino Sans,Hiragino Sans GB,sans-serif!important}',
@@ -175,17 +175,12 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
                 const idx = (x: number, y: number) => (this.width * y + x) << 2;
                 let boundary: number = null;
                 const x = zoomFactor * 2;
-                for (let y = 0; y < this.height; y += zoomFactor) {
+                for (let y = x; y < this.height; y += zoomFactor) {
                   if (
-                    this.data[idx(x, y)] !== 255 &&
+                    this.data[idx(x, y)] !== this.data[idx(x, y - zoomFactor)] &&
                     this.data[idx(x, y)] === this.data[idx(x + zoomFactor * 10, y)]
                   ) {
-                    if (this.data[idx(x, y + 18 * zoomFactor)] !== 255) {
-                      // footer kicks in
-                      boundary = null;
-                    } else {
-                      boundary = y;
-                    }
+                    boundary = y;
                     break;
                   }
                 }