com3d2_world.ts 2.4 KB

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