com3d2_world.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import request from "request-promise-native";
  2. import { Response } from "request";
  3. import { INewsItem, IAggregator } from "./aggregator";
  4. import { getRepository } from "typeorm";
  5. import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
  6. import cheerio from "cheerio";
  7. const kissDiaryRoot = "https://com3d2.world/r18/notices.php";
  8. const FEED_NAME = "com3d2-world-notices";
  9. async function aggregate() {
  10. let repo = getRepository(AggroNewsItem);
  11. let lastPost = await repo.findOne({
  12. select: [ "newsId" ],
  13. where: { feedName: FEED_NAME },
  14. order: { newsId: "DESC" }
  15. });
  16. if(!lastPost)
  17. lastPost = repo.create({
  18. newsId: 0
  19. });
  20. try {
  21. let mainPageRes = await request(kissDiaryRoot, {resolveWithFullResponse: true}) as Response;
  22. if(mainPageRes.statusCode != 200)
  23. return [];
  24. let rootNode = cheerio.load(mainPageRes.body);
  25. let diaryEntries = rootNode("div.frame a");
  26. if(!diaryEntries) {
  27. console.log("[COM3D2 WORLD BLOG] Failed to find listing!");
  28. }
  29. let result : INewsItem[] = [];
  30. let latestEntry = lastPost.newsId;
  31. for(let a of diaryEntries.get() as CheerioElement[]) {
  32. if(!a.attribs.id)
  33. continue;
  34. let id = +a.attribs.id;
  35. if(id <= lastPost.newsId)
  36. continue;
  37. if(id > latestEntry)
  38. latestEntry = id;
  39. let diaryLink = `${kissDiaryRoot}?no=${id}`;
  40. let res = await request(diaryLink, {resolveWithFullResponse: true}) as Response;
  41. if(res.statusCode != 200)
  42. continue;
  43. let node = cheerio.load(res.body);
  44. let title = node("div.frame div.notice_title th");
  45. let contents = node("div.frame div").get(1);
  46. result.push({
  47. newsId: id,
  48. feedId: FEED_NAME,
  49. link: diaryLink,
  50. title: title.text(),
  51. author: "com3d2.world",
  52. contents: cheerio.html(contents),
  53. embedColor: 0xa39869
  54. });
  55. }
  56. return result;
  57. } catch(err) {
  58. return [];
  59. }
  60. }
  61. export default {
  62. aggregate: aggregate
  63. } as IAggregator;