com3d2_world.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const html = require("node-html-parser");
  2. const axios = require("axios");
  3. const db = require("../../db.js");
  4. const kissDiaryRoot = "https://com3d2.world/r18/notices.php";
  5. async function aggregate() {
  6. let lastDiary = db.get("latestCom3D2WorldDiaryEntry").value();
  7. try {
  8. let mainPageRes = await axios.get(kissDiaryRoot);
  9. if(mainPageRes.status != 200)
  10. return [];
  11. let rootNode = html.parse(mainPageRes.data, {
  12. pre: true,
  13. script: false,
  14. style: false
  15. });
  16. let diaryEntries = rootNode.querySelectorAll("div.frame a");
  17. if(!diaryEntries) {
  18. console.log("[COM3D2 WORLD BLOG] Failed to find listing!");
  19. }
  20. let result = [];
  21. let latestEntry = lastDiary;
  22. for(let a of diaryEntries) {
  23. if(!a.rawAttributes.id)
  24. continue;
  25. let id = +a.rawAttributes.id;
  26. if(id <= lastDiary)
  27. continue;
  28. if(id > latestEntry)
  29. latestEntry = id;
  30. let diaryLink = `${kissDiaryRoot}?no=${id}`;
  31. let res = await axios.get(diaryLink);
  32. if(res.status != 200)
  33. continue;
  34. let node = html.parse(res.data, {
  35. pre: true,
  36. script: false,
  37. style: false
  38. });
  39. let title = node.querySelector("div.frame div.notice_title th");
  40. let contents = node.querySelectorAll("div.frame div")[1];
  41. result.push({
  42. id: `com3d2-world-notices-${id}`,
  43. link: diaryLink,
  44. title: title.text,
  45. author: "com3d2.world",
  46. contents: contents.outerHTML,
  47. embedColor: 0xa39869
  48. });
  49. }
  50. db.set("latestCom3D2WorldDiaryEntry", latestEntry).write();
  51. return result;
  52. } catch(err) {
  53. return [];
  54. }
  55. }
  56. module.exports = {
  57. aggregate: aggregate
  58. };