Browse Source

Merge branch 'instagram' into stories

Mike L 3 years ago
parent
commit
a3100bebbe
5 changed files with 29 additions and 4 deletions
  1. 1 0
      config.example.json
  2. 2 1
      dist/main.js
  3. 10 1
      dist/twitter.js
  4. 2 1
      src/main.ts
  5. 14 1
      src/twitter.ts

+ 1 - 0
config.example.json

@@ -11,6 +11,7 @@
   "mode": 0,
   "playwright_ws_spec_endpoint": "http://127.0.0.1:8080/playwright-ws.json",
   "resume_on_start": false,
+  "inactive_hours": ["3:00-7:00"],
   "work_interval": 60,
   "webshot_delay": 20000,
   "webshot_cookies_lockfile": "",

+ 2 - 1
dist/main.js

@@ -54,7 +54,7 @@ const warningFields = [
     'cq_ws_host', 'cq_ws_port', 'cq_access_token',
 ];
 const optionalFields = [
-    'lockfile', 'work_interval', 'webshot_delay', 'loglevel', 'mode', 'resume_on_start', 'ig_socks_proxy',
+    'lockfile', 'inactive_hours', 'work_interval', 'webshot_delay', 'loglevel', 'mode', 'resume_on_start', 'ig_socks_proxy',
 ].concat(warningFields);
 if (requiredFields.some((value) => config[value] === undefined)) {
     console.log(`${requiredFields.join(', ')} are required`);
@@ -134,6 +134,7 @@ const worker = new twitter_1.default({
     proxyUrl: config.ig_socks_proxy,
     lock,
     lockfile: config.lockfile,
+    inactiveHours: config.inactive_hours,
     workInterval: config.work_interval,
     bot: qq,
     webshotDelay: config.webshot_delay,

+ 10 - 1
dist/twitter.js

@@ -209,6 +209,8 @@ class default_1 {
         };
         this.cache = {};
         this.workForAll = () => {
+            if (this.isInactiveTime)
+                return;
             const idToUserMap = {};
             Promise.all(Object.entries(this.lock.threads).map(entry => {
                 const id = entry[1].id;
@@ -249,7 +251,7 @@ class default_1 {
             logger.debug(`current cache: ${JSON.stringify(this.cache)}`);
             if (this.workInterval < 1)
                 this.workInterval = 1;
-            if (lock.feed.length === 0) {
+            if (this.isInactiveTime || lock.feed.length === 0) {
                 setTimeout(this.work, this.workInterval * 1000);
                 return;
             }
@@ -335,6 +337,7 @@ class default_1 {
         this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
         this.lockfile = opt.lockfile;
         this.lock = opt.lock;
+        this.inactiveHours = opt.inactiveHours;
         this.workInterval = opt.workInterval;
         this.bot = opt.bot;
         this.webshotDelay = opt.webshotDelay;
@@ -381,5 +384,11 @@ class default_1 {
             });
         };
     }
+    get isInactiveTime() {
+        const timeToEpoch = (h = 0, m = 0) => new Date().setHours(h, m, 0, 0);
+        return this.inactiveHours
+            .map(rangeStr => ((start, end) => ({ start, end }))(...rangeStr.split('-', 2).map(timeStr => timeToEpoch(...timeStr.split(':', 2).map(Number)))))
+            .some(range => (now => now >= range.start && now < range.end)(Date.now()));
+    }
 }
 exports.default = default_1;

+ 2 - 1
src/main.ts

@@ -65,7 +65,7 @@ const warningFields = [
 ];
 
 const optionalFields = [
-  'lockfile', 'work_interval', 'webshot_delay', 'loglevel', 'mode', 'resume_on_start', 'ig_socks_proxy',
+  'lockfile', 'inactive_hours', 'work_interval', 'webshot_delay', 'loglevel', 'mode', 'resume_on_start', 'ig_socks_proxy',
 ].concat(warningFields);
 
 if (requiredFields.some((value) => config[value] === undefined)) {
@@ -150,6 +150,7 @@ const worker = new Worker({
   proxyUrl: config.ig_socks_proxy,
   lock,
   lockfile: config.lockfile,
+  inactiveHours: config.inactive_hours,
   workInterval: config.work_interval,
   bot: qq,
   webshotDelay: config.webshot_delay,

+ 14 - 1
src/twitter.ts

@@ -46,6 +46,7 @@ interface IWorkerOption {
   lockfile: string;
   webshotCookiesLockfile: string;
   bot: QQBot;
+  inactiveHours: string[];
   workInterval: number;
   webshotDelay: number;
   mode: number;
@@ -203,6 +204,7 @@ export default class {
   private client: IgApiClient;
   private lock: ILock;
   private lockfile: string;
+  private inactiveHours: string[];
   private workInterval: number;
   private bot: QQBot;
   private webshotDelay: number;
@@ -232,6 +234,7 @@ export default class {
     this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
     this.lockfile = opt.lockfile;
     this.lock = opt.lock;
+    this.inactiveHours = opt.inactiveHours;
     this.workInterval = opt.workInterval;
     this.bot = opt.bot;
     this.webshotDelay = opt.webshotDelay;
@@ -334,6 +337,7 @@ export default class {
   } = {};
 
   private workForAll = () => {
+    if (this.isInactiveTime) return;
     const idToUserMap: {[id: number]: UserFeedResponseUser} = {};
     Promise.all(Object.entries(this.lock.threads).map(entry => {
       const id = entry[1].id;
@@ -367,11 +371,20 @@ export default class {
       });
   };
 
+  public get isInactiveTime() {
+    const timeToEpoch = (h = 0, m = 0) => new Date().setHours(h, m, 0, 0);
+    return this.inactiveHours
+      .map(rangeStr => ((start, end) => ({start, end}))(
+        ...rangeStr.split('-', 2).map(timeStr => timeToEpoch(...timeStr.split(':', 2).map(Number))) as [number, number?]
+      ))
+      .some(range => (now => now >= range.start && now < range.end)(Date.now()));
+  }
+
   public work = () => {
     const lock = this.lock;
     logger.debug(`current cache: ${JSON.stringify(this.cache)}`);
     if (this.workInterval < 1) this.workInterval = 1;
-    if (lock.feed.length === 0) {
+    if (this.isInactiveTime || lock.feed.length === 0) {
       setTimeout(this.work, this.workInterval * 1000);
       return;
     }