com3d2_world.ts 2.5 KB

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