Bläddra i källkod

add query command

Mike L 3 år sedan
förälder
incheckning
9e6f697f0d
6 ändrade filer med 95 tillägg och 5 borttagningar
  1. 19 1
      dist/command.js
  2. 5 0
      dist/koishi.js
  3. 24 1
      dist/twitter.js
  4. 18 2
      src/command.ts
  5. 6 1
      src/koishi.ts
  6. 23 0
      src/twitter.ts

+ 19 - 1
dist/command.js

@@ -1,6 +1,6 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.view = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
+exports.query = exports.view = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
 const fs = require("fs");
 const path = require("path");
 const datetime_1 = require("./datetime");
@@ -163,3 +163,21 @@ function view(chat, args, reply) {
     promptOnError(twitter_1.sendAllStories)(match.userName, chat, conf.skip, conf.count);
 }
 exports.view = view;
+function query(chat, args, reply) {
+    var _a;
+    if (args.length === 0) {
+        return reply('找不到要查询 Instagram 限时动态的用户。');
+    }
+    const match = (_a = twitter_1.parseLink(args[0])) === null || _a === void 0 ? void 0 : _a.userName;
+    if (!match) {
+        return reply('链接格式有误。');
+    }
+    try {
+        twitter_1.sendTimeline(match, chat);
+    }
+    catch (e) {
+        logger.error(`error querying timeline, error: ${e}`);
+        reply('机器人尚未加载完毕,请稍后重试。');
+    }
+}
+exports.query = query;

+ 5 - 0
dist/koishi.js

@@ -181,6 +181,10 @@ class default_1 {
                     case 'igstory_get':
                         command_1.view(chat, cmdObj.args, reply);
                         break;
+                    case 'igstory_query':
+                    case 'igstory_gettimeline':
+                        command_1.query(chat, cmdObj.args, reply);
+                        break;
                     case 'igstory_sub':
                     case 'igstory_subscribe':
                         this.botInfo.sub(chat, cmdObj.args, reply);
@@ -197,6 +201,7 @@ class default_1 {
                         if (cmdObj.args.length === 0) {
                             reply(`Instagram 限时动态搬运机器人:
 /igstory - 查询当前聊天中的 Instagram Stories 动态订阅
+/igstory_query〈链接|用户名〉- 列出该用户当前可用的 Stories 时间线
 /igstory_sub[scribe]〈链接|用户名〉- 订阅 Instagram Stories 搬运
 /igstory_unsub[scribe]〈链接|用户名〉- 退订 Instagram Stories 搬运
 /igstory_view〈链接|用户名〉[skip=〈跳过条数〉] [count=〈最大条数(默认:10)〉] - 查看该用户全部或指定范围的 Stories\

+ 24 - 1
dist/twitter.js

@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
     });
 };
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.sendAllStories = exports.sendStory = exports.ScreenNameNormalizer = exports.SessionManager = exports.parseLink = exports.linkBuilder = void 0;
+exports.sendAllStories = exports.sendStory = exports.sendTimeline = exports.ScreenNameNormalizer = exports.SessionManager = exports.parseLink = exports.linkBuilder = void 0;
 const crypto = require("crypto");
 const fs = require("fs");
 const http = require("http");
@@ -18,6 +18,7 @@ const url_1 = require("url");
 const util_1 = require("util");
 const instagram_private_api_1 = require("instagram-private-api");
 const socks_proxy_agent_1 = require("socks-proxy-agent");
+const datetime_1 = require("./datetime");
 const loggers_1 = require("./loggers");
 const utils_1 = require("./utils");
 const webshot_1 = require("./webshot");
@@ -140,6 +141,10 @@ class ScreenNameNormalizer {
 }
 exports.ScreenNameNormalizer = ScreenNameNormalizer;
 ScreenNameNormalizer.normalize = (username) => `${username.toLowerCase().replace(/^@/, '')}:`;
+let sendTimeline = (username, receiver) => {
+    throw Error();
+};
+exports.sendTimeline = sendTimeline;
 let sendStory = (username, storyId, receiver) => {
     throw Error();
 };
@@ -425,6 +430,24 @@ class default_1 {
             });
         };
         ScreenNameNormalizer._queryUser = this.queryUser;
+        exports.sendTimeline = (rawUserName, receiver) => {
+            const reply = msg => this.bot.sendTo(receiver, msg);
+            workNow({
+                rawUserName,
+                action: userName => {
+                    const storyItems = Object.values(this.cache[userName].stories)
+                        .sort((i1, i2) => -utils_1.BigNumOps.compare(i2.pk, i1.pk));
+                    if (storyItems.length === 0)
+                        return reply(`当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
+                    return reply('#. 编号:发送时间\n' + storyItems.map(({ original }, index) => `\n${index + 1}. ${original.pk}: ${datetime_1.relativeDate(original.taken_at * 1000)}`).join(''))
+                        .then(() => reply(`请使用 /igstory_view ${userName} skip=<#-1> count=1
+或 /igstory_view https://www.instagram.com/stories/${userName}/<编号>/
+查看指定的限时动态。`));
+                },
+                reply,
+                retryAction: () => exports.sendTimeline(rawUserName, receiver),
+            });
+        };
         exports.sendStory = (rawUserName, storyId, receiver) => {
             const reply = msg => this.bot.sendTo(receiver, msg);
             const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);

+ 18 - 2
src/command.ts

@@ -7,7 +7,7 @@ import * as path from 'path';
 import { relativeDate } from './datetime';
 import { getLogger } from './loggers';
 import {
-  sendStory, sendAllStories, ScreenNameNormalizer as normalizer,
+  sendAllStories, sendStory, sendTimeline, ScreenNameNormalizer as normalizer,
   linkBuilder, parseLink
 } from './twitter';
 
@@ -170,4 +170,20 @@ function view(chat: IChat, args: string[], reply: (msg: string) => any): void {
   promptOnError(sendAllStories)(match.userName, chat, conf.skip, conf.count);
 }
 
-export { parseCmd, sub, list, unsub, view };
+function query(chat: IChat, args: string[], reply: (msg: string) => any): void {
+  if (args.length === 0) {
+    return reply('找不到要查询 Instagram 限时动态的用户。');
+  }
+  const match = parseLink(args[0])?.userName;
+  if (!match) {
+    return reply('链接格式有误。');
+  }
+  try {
+    sendTimeline(match, chat);
+  } catch (e) {
+    logger.error(`error querying timeline, error: ${e}`);
+    reply('机器人尚未加载完毕,请稍后重试。');
+  }
+}
+
+export { parseCmd, sub, list, unsub, view, query };

+ 6 - 1
src/koishi.ts

@@ -2,7 +2,7 @@ import { App, Bot, segment, Session, sleep } from 'koishi';
 import 'koishi-adapter-onebot';
 import { Message as CQMessage, SenderInfo } from 'koishi-adapter-onebot';
 
-import { parseCmd, view } from './command';
+import { parseCmd, query, view } from './command';
 import { getLogger } from './loggers';
 import { chainPromises } from './utils';
 
@@ -200,6 +200,10 @@ export default class {
         case 'igstory_get':
           view(chat, cmdObj.args, reply);
           break;
+        case 'igstory_query':
+        case 'igstory_gettimeline':
+          query(chat, cmdObj.args, reply);
+          break;
         case 'igstory_sub':
         case 'igstory_subscribe':
           this.botInfo.sub(chat, cmdObj.args, reply);
@@ -216,6 +220,7 @@ export default class {
           if (cmdObj.args.length === 0) {
             reply(`Instagram 限时动态搬运机器人:
 /igstory - 查询当前聊天中的 Instagram Stories 动态订阅
+/igstory_query〈链接|用户名〉- 列出该用户当前可用的 Stories 时间线
 /igstory_sub[scribe]〈链接|用户名〉- 订阅 Instagram Stories 搬运
 /igstory_unsub[scribe]〈链接|用户名〉- 退订 Instagram Stories 搬运
 /igstory_view〈链接|用户名〉[skip=〈跳过条数〉] [count=〈最大条数(默认:10)〉] - 查看该用户全部或指定范围的 Stories\

+ 23 - 0
src/twitter.ts

@@ -13,6 +13,7 @@ import {
 import { RequestError } from 'request-promise/errors';
 import { SocksProxyAgent } from 'socks-proxy-agent';
 
+import { relativeDate } from './datetime';
 import { getLogger } from './loggers';
 import QQBot from './koishi';
 import { Arr, BigNumOps, chainPromises } from './utils';
@@ -165,6 +166,10 @@ export class ScreenNameNormalizer {
   }
 }
 
+export let sendTimeline = (username: string, receiver: IChat): void => {
+  throw Error();
+}
+
 export let sendStory = (username: string, storyId: string, receiver: IChat): void => {
   throw Error();
 }
@@ -296,6 +301,24 @@ export default class {
     };
 
     ScreenNameNormalizer._queryUser = this.queryUser;
+    sendTimeline = (rawUserName, receiver) => {
+      const reply = msg => this.bot.sendTo(receiver, msg);
+      workNow({
+        rawUserName,
+        action: userName => {
+          const storyItems = Object.values(this.cache[userName].stories)
+            .sort((i1, i2) => -BigNumOps.compare(i2.pk, i1.pk)); // ascending!
+          if (storyItems.length === 0) return reply(`当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
+          return reply('#. 编号:发送时间\n' + storyItems.map(({original}, index) =>
+              `\n${index + 1}. ${original.pk}: ${relativeDate(original.taken_at * 1000)}`).join(''))
+            .then(() => reply(`请使用 /igstory_view ${userName} skip=<#-1> count=1
+或 /igstory_view https://www.instagram.com/stories/${userName}/<编号>/
+查看指定的限时动态。`));
+        },
+        reply,
+        retryAction: () => sendTimeline(rawUserName, receiver),
+      });
+    }
     sendStory = (rawUserName, storyId, receiver) => {
       const reply = msg => this.bot.sendTo(receiver, msg);
       const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);