Bladeren bron

:beers: interval

LI JIAHAO 6 jaren geleden
bovenliggende
commit
a00e7b32d5
4 gewijzigde bestanden met toevoegingen van 34 en 12 verwijderingen
  1. 1 0
      config.example.json
  2. 0 0
      src/cqhttp.ts
  3. 15 4
      src/main.ts
  4. 18 8
      src/twitter.ts

+ 1 - 0
config.examle.json → config.example.json

@@ -6,5 +6,6 @@
   "twitter_consumer_secret": "",
   "twitter_access_token_key": "",
   "twitter_access_token_secret": "",
+  "work_interval": 60,
   "lockfile": "subscriber.lock"
 }

+ 0 - 0
src/qq.ts → src/cqhttp.ts


+ 15 - 4
src/main.ts

@@ -6,7 +6,7 @@ import * as log4js from 'log4js';
 import * as path from 'path';
 
 import { list, sub, unsub } from './command';
-import QQBot from './qq';
+import QQBot from './cqhttp';
 import work from './twitter';
 
 const logger = log4js.getLogger();
@@ -28,7 +28,7 @@ const sections = [
     header: 'Documentation',
     content: [
       'Project home: {underline https://github.com/rikakomoe/cqhttp-twitter-bot}',
-      'Example config: {underline https://qwqq.pw/b96yt}',
+      'Example config: {underline https://qwqq.pw/qpfhg}',
     ],
   },
 ];
@@ -68,6 +68,9 @@ if (config.cq_access_token === undefined) {
 if (config.lockfile === undefined) {
   config.lockfile = 'subscriber.lock';
 }
+if (config.work_interval === undefined) {
+  config.work_interval = 60;
+}
 
 let lock: ILock;
 if (fs.existsSync(path.resolve(config.lockfile))) {
@@ -101,6 +104,10 @@ if (fs.existsSync(path.resolve(config.lockfile))) {
   }
 }
 
+Object.keys(lock.threads).forEach(key => {
+  lock.threads[key].offset = -1;
+});
+
 const qq = new QQBot({
   access_token: config.cq_access_token,
   host: config.cq_ws_host,
@@ -111,7 +118,11 @@ const qq = new QQBot({
 });
 
 setTimeout(() => {
-  work(lock, config.lockfile);
-}, 60000);
+  work({
+    lock,
+    lockfile: config.lockfile,
+    workInterval: config.work_interval,
+  });
+}, config.work_interval * 1000);
 
 qq.connect();

+ 18 - 8
src/twitter.ts

@@ -2,14 +2,22 @@ import * as fs from 'fs';
 import * as log4js from 'log4js';
 import * as path from 'path';
 
+interface IWorkerOption {
+  lock: ILock;
+  lockfile: string;
+  workInterval: number;
+}
+
 const logger = log4js.getLogger('twitter');
 logger.level = 'info';
 
-function work(lock: ILock, lockfile: string) {
+function work(opt: IWorkerOption) {
+  const lock = opt.lock;
+  if (opt.workInterval < 1) opt.workInterval = 1;
   if (lock.feed.length === 0) {
     setTimeout(() => {
-      work(lock, lockfile);
-    }, 60000);
+      work(opt);
+    }, opt.workInterval * 1000);
     return;
   }
   if (lock.workon >= lock.feed.length) lock.workon = 0;
@@ -18,18 +26,20 @@ function work(lock: ILock, lockfile: string) {
     lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
     logger.error(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
     lock.feed.splice(lock.workon, 1);
-    fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
-    work(lock, lockfile);
+    fs.writeFileSync(path.resolve(opt.lockfile), JSON.stringify(lock));
+    work(opt);
     return;
   }
 
   // TODO: Work on lock.feed[lock.workon]
 
   lock.workon++;
-  fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
+  let timeout = opt.workInterval * 1000 / lock.feed.length;
+  if (timeout < 1000) timeout = 1000;
+  fs.writeFileSync(path.resolve(opt.lockfile), JSON.stringify(lock));
   setTimeout(() => {
-    work(lock, lockfile);
-  }, 60000);
+    work(opt);
+  }, timeout);
 }
 
 export default work;