kiss_diary.ts 2.6 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. const urlPattern = /diary\.php\?no=(\d+)/i;
  8. const kissDiaryRoot = "http://www.kisskiss.tv/kiss";
  9. const FEED_NAME = "kisskisstv-diary";
  10. async function aggregate() {
  11. let 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. let mainPageRes = await request(`${kissDiaryRoot}/diary.php`, {resolveWithFullResponse: true}) as Response;
  23. if(mainPageRes.statusCode != 200)
  24. return [];
  25. let rootNode = cheerio.load(mainPageRes.body);
  26. let diaryEntryNames = rootNode("table.blog_frame_top");
  27. if(diaryEntryNames.length == 0) {
  28. console.log("[KISS DIARY] Failed to find listing!");
  29. }
  30. let diaryTexts = rootNode("div.blog_frame_middle");
  31. let items = diaryEntryNames.map((i, e) => ({ table: e, content: diaryTexts.get(i) }));
  32. let result : INewsItem[] = [];
  33. let latestEntry = lastPost.newsId;
  34. for(let {table, content} of items.get() as {table: CheerioElement, content: CheerioElement}[]) {
  35. let a = cheerio(table).find("a");
  36. let link = a.attr("href");
  37. let matches = link ? urlPattern.exec(link) : false;
  38. if(!matches)
  39. continue;
  40. let id = +matches[1];
  41. if(id <= lastPost.newsId)
  42. continue;
  43. if(id > latestEntry)
  44. latestEntry = id;
  45. let diaryLink = `${kissDiaryRoot}/${link}`;
  46. let contentCh = cheerio(content);
  47. let title = a.text();
  48. let bottomFrame = contentCh.find("div.blog_data");
  49. bottomFrame.remove();
  50. result.push({
  51. newsId: id,
  52. feedId: FEED_NAME,
  53. link: diaryLink,
  54. title: title,
  55. author: "KISS BLOG",
  56. contents: contentCh.html() ?? "",
  57. embedColor: 0xf4c100,
  58. needsTranslation: true
  59. });
  60. }
  61. return result;
  62. } catch(err) {
  63. return [];
  64. }
  65. }
  66. export default {
  67. aggregate: aggregate
  68. } as IAggregator;