Browse Source

Add update checker

ghorsington 5 years ago
parent
commit
ef2e492e8f
3 changed files with 75 additions and 4 deletions
  1. 64 0
      commands/aggregators/com3d2_updates.js
  2. 9 3
      commands/news_aggregator.js
  3. 2 1
      db.js

+ 64 - 0
commands/aggregators/com3d2_updates.js

@@ -0,0 +1,64 @@
+const html = require("node-html-parser"); 
+const axios = require("axios");
+const db = require("../../db.js");
+
+const updatePage = "http://com3d2.jp/update/";
+const changeLogPattern = /\[\s*([^\s\]]+)\s*\]\s*((・.*)\s+)+/gim;
+
+function getVersionNumber(verStr) {
+    let verPart = verStr.replace(/[\.\s]/g, "");
+    if(verPart.length < 4)
+        verPart += "0";
+    return +verPart;
+}
+
+async function aggregate() {
+    let lastVersion = db.get("lastCOMJPVersion").value();
+    
+    try {
+        let mainPageRes = await axios.get(updatePage);
+        
+        if(mainPageRes.status != 200)
+            return [];
+
+        let rootNode = html.parse(mainPageRes.data, {
+                pre: true,
+                script: false,
+                style: false
+        });
+
+        let readme = rootNode.querySelector("div.readme");
+
+        if(!readme) {
+            console.log("[COM3D2 JP UPDATE] Failed to find listing!");
+        }
+
+        let latestVersionChangelog = changeLogPattern.exec(readme.text);
+
+        if(!latestVersionChangelog)
+            return [];
+
+        let version = getVersionNumber(latestVersionChangelog[1]);
+        let text = latestVersionChangelog[0];
+
+        if(version <= lastVersion)
+            return [];
+        
+        db.set("lastCOMJPVersion", version).write();
+
+        return [{
+            id: `com3d2-jp-updates-${version}`,
+            link: updatePage,
+            title: latestVersionChangelog[1],
+            author: "COM3D2 UPDATE",
+            contents: text,
+            embedColor: 0xcccccc
+        }];
+    } catch(err) {
+        return [];
+    }
+}
+
+module.exports = {
+    aggregate: aggregate
+};

+ 9 - 3
commands/news_aggregator.js

@@ -4,13 +4,12 @@ const db = require("../db.js");
 const interval = require("interval-promise");
 const client = require("../client.js");
 const sha1 = require("sha1");
-const html = require("node-html-parser"); 
-const axios = require("axios");
 const path = require("path");
 const fs = require("fs");
 const Discord = require("discord.js");
 
 const UPDATE_INTERVAL = 5;
+const MAX_PREVIEW_LENGTH = 300; 
 
 const aggregators = [];
 const aggregateChannelID = db.get("aggregateChannel").value();
@@ -61,6 +60,13 @@ async function checkFeeds() {
     }
 }
 
+function clipText(text) {
+    if(text.length <= MAX_PREVIEW_LENGTH)
+        return text;
+
+    return `${text.substring(0, MAX_PREVIEW_LENGTH)}...`;
+}
+
 // TODO: Replace with proper forum implementation
 async function addNewsItem(item) {
     let aggrItems = db.get("aggregatedItemsCache");
@@ -82,7 +88,7 @@ async function addNewsItem(item) {
         url: item.link,
         color: item.embedColor,
         timestamp: new Date(),
-        description: `${item.contents.substring(0, Math.min(item.contents.length, 300))}...`,
+        description: clipText(item.contents),
         author: {
             name: item.author
         },

+ 2 - 1
db.js

@@ -136,7 +136,8 @@ db.defaults({
     },
     quotes: [],
     latestKissDiaryEntry: 1229,
-    latestCom3D2WorldDiaryEntry: 11
+    latestCom3D2WorldDiaryEntry: 11,
+    lastCOMJPVersion: 1320
 }).write();
 
 module.exports = db;