Browse Source

Add aggregator for ENG blog

ghorsington 5 years ago
parent
commit
3711245ae6
2 changed files with 78 additions and 1 deletions
  1. 76 0
      commands/aggregators/com3d2_world.js
  2. 2 1
      db.js

+ 76 - 0
commands/aggregators/com3d2_world.js

@@ -0,0 +1,76 @@
+const html = require("node-html-parser"); 
+const axios = require("axios");
+const db = require("../../db.js");
+
+const kissDiaryRoot = "https://com3d2.world/r18/notices.php";
+
+async function aggregate() {
+    let lastDiary = db.get("latestCom3D2WorldDiaryEntry").value();
+    
+    try {
+        let mainPageRes = await axios.get(kissDiaryRoot);
+        
+        if(mainPageRes.status != 200)
+            return [];
+
+        let rootNode = html.parse(mainPageRes.data, {
+                pre: true,
+                script: false,
+                style: false
+        });
+
+        let diaryEntries = rootNode.querySelectorAll("div.frame a");
+
+        if(!diaryEntries) {
+            console.log("[COM3D2 WORLD BLOG] Failed to find listing!");
+        }
+
+        let result = [];
+        let latestEntry = lastDiary;
+
+        for(let a of diaryEntries) {
+            if(!a.rawAttributes.id)
+                continue;
+            
+            let id = +a.rawAttributes.id;
+
+            if(id <= lastDiary)
+                continue;
+
+            if(id > latestEntry)
+                latestEntry = id;
+
+            let diaryLink = `${kissDiaryRoot}?no=${id}`;
+            let res = await axios.get(diaryLink);
+            if(res.status != 200)
+                continue;
+
+            let node = html.parse(res.data, {
+                pre: true,
+                script: false,
+                style: false
+            });
+
+            let title = node.querySelector("div.frame div.notice_title th");
+            let contents = node.querySelectorAll("div.frame div")[1];
+
+            result.push({
+                id: `com3d2-world-notices-${id}`,
+                link: diaryLink,
+                title: title.text,
+                author: "com3d2.world",
+                contents: contents.outerHTML,
+                embedColor: 0xa39869
+            });
+        }
+
+        db.set("latestCom3D2WorldDiaryEntry", latestEntry).write();
+        return result;
+    } catch(err) {
+        return [];
+    }
+}
+
+module.exports = {
+    aggregate: aggregate
+};

+ 2 - 1
db.js

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