Prechádzať zdrojové kódy

:sparkles: improve

1. update gitignore and package.json
2. add author in text mode
3. replace t.co link with media type string

Signed-off-by: LI JIAHAO <lijiahao99131@gmail.com>
LI JIAHAO 6 rokov pred
rodič
commit
ff22389984
6 zmenil súbory, kde vykonal 35 pridanie a 9 odobranie
  1. 4 1
      .gitignore
  2. 3 2
      dist/twitter.js
  3. 11 1
      dist/webshot.js
  4. 2 2
      package.json
  5. 3 2
      src/twitter.ts
  6. 12 1
      src/webshot.ts

+ 4 - 1
.gitignore

@@ -1,4 +1,7 @@
 yarn-error.log
 node_modules
 .idea
-*.lock
+*.lock
+*.json
+!config.example.json
+nohup.out

+ 3 - 2
dist/twitter.js

@@ -107,20 +107,21 @@ class default_1 {
                 }
                 if (lock.threads[lock.feed[lock.workon]].offset === 0)
                     tweets.splice(1);
-                return this.webshot(this.mode, tweets, (msg, text) => {
+                return this.webshot(this.mode, tweets, (msg, text, author) => {
                     lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
                         logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
                         let hash = JSON.stringify(subscriber) + text;
                         logger.debug(hash);
                         hash = sha1(JSON.stringify(subscriber) + text);
                         logger.debug(hash);
+                        const twtext = `${author.name}(@${author.screen_name}):\n${text}`;
                         const send = () => {
                             this.bot.bot('send_msg', {
                                 message_type: subscriber.chatType,
                                 user_id: subscriber.chatID,
                                 group_id: subscriber.chatID,
                                 discuss_id: subscriber.chatID,
-                                message: this.mode === 0 ? msg : text,
+                                message: this.mode === 0 ? msg : twtext,
                             });
                         };
                         if (this.redisClient) {

+ 11 - 1
dist/webshot.js

@@ -6,6 +6,11 @@ const log4js = require("log4js");
 const pngjs_1 = require("pngjs");
 const puppeteer = require("puppeteer");
 const read = require("read-all-stream");
+const typeInZH = {
+    photo: '[图片]',
+    video: '[视频]',
+    animated_gif: '[GIF]',
+};
 const logger = log4js.getLogger('webshot');
 logger.level = global.loglevel;
 class Webshot extends CallableInstance {
@@ -177,10 +182,15 @@ class Webshot extends CallableInstance {
                         text = text.replace(new RegExp(url.url, 'gm'), url.expanded_url);
                     });
                 }
+                if (twi.extended_entities) {
+                    twi.extended_entities.media.forEach(media => {
+                        text = text.replace(new RegExp(media.url, 'gm'), typeInZH[media.type]);
+                    });
+                }
                 text = text.replace(/&/gm, '&amp;')
                     .replace(/\[/gm, '&#91;')
                     .replace(/\]/gm, '&#93;');
-                callback(cqstr, text);
+                callback(cqstr, text, twi.user);
             });
         });
         return promise;

+ 2 - 2
package.json

@@ -8,7 +8,7 @@
   },
   "repository": {
     "type": "git",
-    "url": "https://github.com/rikakomoe/cqhttp-twitter-bot.git"
+    "url": "git+https://github.com/rikakomoe/cqhttp-twitter-bot.git"
   },
   "keywords": [
     "cq",
@@ -24,7 +24,7 @@
   },
   "homepage": "https://github.com/rikakomoe/cqhttp-twitter-bot",
   "scripts": {
-    "build": "rm -r dist && tsc --outDir dist",
+    "build": "rm -rf dist && tsc --outDir dist",
     "lint": "tslint --fix -c tslint.json --project ./"
   },
   "dependencies": {

+ 3 - 2
src/twitter.ts

@@ -148,20 +148,21 @@ export default class {
         return;
       }
       if (lock.threads[lock.feed[lock.workon]].offset === 0) tweets.splice(1);
-      return (this.webshot as any)(this.mode, tweets, (msg, text) => {
+      return (this.webshot as any)(this.mode, tweets, (msg, text, author) => {
         lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
           logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
           let hash = JSON.stringify(subscriber) + text;
           logger.debug(hash);
           hash = sha1(JSON.stringify(subscriber) + text);
           logger.debug(hash);
+          const twtext = `${author.name}(@${author.screen_name}):\n${text}`;
           const send = () => {
             this.bot.bot('send_msg', {
               message_type: subscriber.chatType,
               user_id: subscriber.chatID,
               group_id: subscriber.chatID,
               discuss_id: subscriber.chatID,
-              message: this.mode === 0 ? msg : text,
+              message: this.mode === 0 ? msg : twtext,
             });
           };
           if (this.redisClient) {

+ 12 - 1
src/webshot.ts

@@ -6,6 +6,12 @@ import * as puppeteer from 'puppeteer';
 import { Browser } from 'puppeteer';
 import * as read from 'read-all-stream';
 
+const typeInZH = {
+  photo: '[图片]',
+  video: '[视频]',
+  animated_gif: '[GIF]',
+};
+
 const logger = log4js.getLogger('webshot');
 logger.level = (global as any).loglevel;
 
@@ -178,10 +184,15 @@ class Webshot extends CallableInstance {
             text = text.replace(new RegExp(url.url, 'gm'), url.expanded_url);
           });
         }
+        if (twi.extended_entities) {
+          twi.extended_entities.media.forEach(media => {
+            text = text.replace(new RegExp(media.url, 'gm'), typeInZH[media.type]);
+          });
+        }
         text = text.replace(/&/gm, '&amp;')
           .replace(/\[/gm, '&#91;')
           .replace(/\]/gm, '&#93;');
-        callback(cqstr, text);
+        callback(cqstr, text, twi.user);
       });
     });
     return promise;