com3d2_world.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { INewsItem, IAggregator } from "./aggregator";
  2. import { getRepository } from "typeorm";
  3. import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
  4. import cheerio from "cheerio";
  5. import { logger } from "src/logging";
  6. import got from "got";
  7. const kissDiaryRoot = "https://com3d2.world/r18/notices.php";
  8. const FEED_NAME = "com3d2-world-notices";
  9. async function aggregate() {
  10. const 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. const mainPageRes = await got.get(kissDiaryRoot);
  22. if(mainPageRes.statusCode != 200) {
  23. logger.error("[COM3D2 WORLD BLOG] Failed to load page. Got response code: %s", mainPageRes.statusCode);
  24. return [];
  25. }
  26. const rootNode = cheerio.load(mainPageRes.body);
  27. const diaryEntries = rootNode("div.frame a");
  28. if(!diaryEntries) {
  29. logger.error("[COM3D2 WORLD BLOG] Failed to find listing!");
  30. return [];
  31. }
  32. const result : INewsItem[] = [];
  33. let latestEntry = lastPost.newsId;
  34. for(const a of diaryEntries.toArray()) {
  35. if(!a.attribs.id)
  36. continue;
  37. const id = +a.attribs.id;
  38. if(id <= lastPost.newsId)
  39. continue;
  40. if(id > latestEntry)
  41. latestEntry = id;
  42. const diaryLink = `${kissDiaryRoot}?no=${id}`;
  43. const res = await got.get(diaryLink);
  44. if(res.statusCode != 200)
  45. continue;
  46. const node = cheerio.load(res.body);
  47. const title = node("div.frame div.notice_title th");
  48. const contents = node("div.frame div").get(1);
  49. result.push({
  50. newsId: id,
  51. feedId: FEED_NAME,
  52. link: diaryLink,
  53. title: title.text(),
  54. author: "com3d2.world",
  55. contents: cheerio.html(contents),
  56. embedColor: 0xa39869
  57. });
  58. }
  59. return result;
  60. } catch(err) {
  61. logger.error("Failed to process com3d2.world news: %s", err);
  62. return [];
  63. }
  64. }
  65. export default {
  66. aggregate: aggregate
  67. } as IAggregator;