Browse Source

more css itweaks, support inactive hours

Mike L 3 years ago
parent
commit
37ca1d1bd3
7 changed files with 39 additions and 21 deletions
  1. 1 0
      config.example.json
  2. 2 1
      dist/main.js
  3. 9 4
      dist/twitter.js
  4. 5 5
      dist/webshot.js
  5. 2 1
      src/main.ts
  6. 14 4
      src/twitter.ts
  7. 6 6
      src/webshot.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,

+ 9 - 4
dist/twitter.js

@@ -300,10 +300,8 @@ class default_1 {
             const lock = this.lock;
             if (this.workInterval < 1)
                 this.workInterval = 1;
-            if (lock.feed.length === 0) {
-                setTimeout(() => {
-                    this.work();
-                }, this.workInterval * 1000);
+            if (this.isInactiveTime || lock.feed.length === 0) {
+                setTimeout(this.work, this.workInterval * 1000);
                 return;
             }
             if (lock.workon >= lock.feed.length)
@@ -384,6 +382,7 @@ class default_1 {
         this.lockfile = opt.lockfile;
         this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
         this.lock = opt.lock;
+        this.inactiveHours = opt.inactiveHours;
         this.workInterval = opt.workInterval;
         this.bot = opt.bot;
         this.webshotDelay = opt.webshotDelay;
@@ -446,5 +445,11 @@ class default_1 {
                 .catch((err) => { this.bot.sendTo(receiver, parseMediaError(err)); });
         };
     }
+    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;

+ 5 - 5
dist/webshot.js

@@ -96,8 +96,8 @@ class Webshot extends CallableInstance {
                         logger.warn(`navigation timed out at ${getTimerTime()} ms`);
                         return null;
                     })
-                        .then(() => page.addStyleTag({ content: 'nav,footer,header+div,header+div+div>div>div+div,header div div div+div,' +
-                            'article section,article section+div>ul>:not(div),article button,canvas{display:none!important} ' +
+                        .then(() => page.addStyleTag({ content: 'nav,footer,header+div,header+div+div>div>div+div,article button,canvas,' +
+                            'article section,article section+div>ul>:not(div),li [tabindex="0"] div:not(:first-child){display:none!important} ' +
                             'section+div{overflow:hidden} section+*>*{position:relative!important} article{border-bottom:1px solid!important}',
                     }))
                         .then(() => page.addStyleTag({
@@ -124,7 +124,7 @@ class Webshot extends CallableInstance {
                         }).on('parsed', function () {
                             const idx = (x, y) => (this.width * y + x) << 2;
                             let boundary = null;
-                            for (let y = this.height - 1; y > this.height - 1920; y -= zoomFactor) {
+                            for (let y = this.height - 1; y > this.height - 3840; y -= zoomFactor) {
                                 if (this.data[idx(zoomFactor, y)] <= 38 &&
                                     this.data[idx(zoomFactor, y)] === this.data[idx(this.width - zoomFactor, y)] &&
                                     this.data[idx(zoomFactor, y + zoomFactor)] === this.data[idx(zoomFactor, y - 2 * zoomFactor)]) {
@@ -165,7 +165,7 @@ class Webshot extends CallableInstance {
             });
             return promise.then(data => {
                 if (data.boundary === null) {
-                    return this.renderWebshot(url, height + 1920, webshotDelay, ...morePostProcessings);
+                    return this.renderWebshot(url, height + 3840, webshotDelay, ...morePostProcessings);
                 }
                 else
                     return data.path;
@@ -230,7 +230,7 @@ class Webshot extends CallableInstance {
                 messageChain += (author + xmlEntities.decode(text));
             if (this.mode === 0) {
                 const url = twitter_1.linkBuilder({ postUrlSegment: item.code });
-                promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay, page => page.addStyleTag({ content: 'header>div>div+div{font-size:smaller; line-height:calc(4/3)}' +
+                promise = promise.then(() => this.renderWebshot(url, 3840, webshotDelay, page => page.addStyleTag({ content: 'header>div>div+div{font-size:12px; line-height:15px; padding-top:0!important}' +
                         `header>div>div+div::before{content:"${item.user.full_name}"; color:#8e8e8e; font-weight:bold}`,
                 })))
                     .then(fileurl => {

+ 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 - 4
src/twitter.ts

@@ -50,6 +50,7 @@ interface IWorkerOption {
   lockfile: string;
   webshotCookiesLockfile: string;
   bot: QQBot;
+  inactiveHours: string[];
   workInterval: number;
   webshotDelay: number;
   mode: number;
@@ -231,6 +232,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;
@@ -263,6 +265,7 @@ export default class {
     this.lockfile = opt.lockfile;
     this.webshotCookiesLockfile = opt.webshotCookiesLockfile;
     this.lock = opt.lock;
+    this.inactiveHours = opt.inactiveHours;
     this.workInterval = opt.workInterval;
     this.bot = opt.bot;
     this.webshotDelay = opt.webshotDelay;
@@ -460,13 +463,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;
     if (this.workInterval < 1) this.workInterval = 1;
-    if (lock.feed.length === 0) {
-      setTimeout(() => {
-        this.work();
-      }, this.workInterval * 1000);
+    if (this.isInactiveTime || lock.feed.length === 0) {
+      setTimeout(this.work, this.workInterval * 1000);
       return;
     }
     if (lock.workon >= lock.feed.length) lock.workon = 0;

+ 6 - 6
src/webshot.ts

@@ -134,8 +134,8 @@ class Webshot extends CallableInstance<[LazyMediaItem[], (...args) => void, numb
             })
             // hide header, footer, "more options" button, like and share count, carousel navigator, and comments
             .then(() => page.addStyleTag({content:
-              'nav,footer,header+div,header+div+div>div>div+div,header div div div+div,' +
-              'article section,article section+div>ul>:not(div),article button,canvas{display:none!important} ' +
+              'nav,footer,header+div,header+div+div>div>div+div,article button,canvas,' +
+              'article section,article section+div>ul>:not(div),li [tabindex="0"] div:not(:first-child){display:none!important} ' +
               'section+div{overflow:hidden} section+*>*{position:relative!important} article{border-bottom:1px solid!important}',
             }))
             .then(() => page.addStyleTag({
@@ -162,7 +162,7 @@ class Webshot extends CallableInstance<[LazyMediaItem[], (...args) => void, numb
               }).on('parsed', function () {
                 const idx = (x: number, y: number) => (this.width * y + x) << 2;
                 let boundary: number = null;
-                for (let y = this.height - 1; y > this.height - 1920; y -= zoomFactor) {
+                for (let y = this.height - 1; y > this.height - 3840; y -= zoomFactor) {
                   if (
                     this.data[idx(zoomFactor, y)] <= 38 &&
                     this.data[idx(zoomFactor, y)] === this.data[idx(this.width - zoomFactor, y)] &&
@@ -204,7 +204,7 @@ class Webshot extends CallableInstance<[LazyMediaItem[], (...args) => void, numb
     });
     return promise.then(data => {
       if (data.boundary === null) {
-        return this.renderWebshot(url, height + 1920, webshotDelay, ...morePostProcessings);
+        return this.renderWebshot(url, height + 3840, webshotDelay, ...morePostProcessings);
       } else return data.path;
     }).catch(error => this.reconnect(error)
       .then(() => this.renderWebshot(url, height, webshotDelay, ...morePostProcessings))
@@ -269,10 +269,10 @@ class Webshot extends CallableInstance<[LazyMediaItem[], (...args) => void, numb
       // invoke webshot
       if (this.mode === 0) {
         const url = linkBuilder({postUrlSegment: item.code});
-        promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay, page =>
+        promise = promise.then(() => this.renderWebshot(url, 3840, webshotDelay, page =>
           // display full name
           page.addStyleTag({content:
-            'header>div>div+div{font-size:smaller; line-height:calc(4/3)}' +
+            'header>div>div+div{font-size:12px; line-height:15px; padding-top:0!important}' +
             `header>div>div+div::before{content:"${item.user.full_name}"; color:#8e8e8e; font-weight:bold}`,
           })
         ))