12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import request from "request-promise-native";
- import { Response } from "request";
- import { INewsItem, IAggregator } from "./aggregator";
- import { getRepository } from "typeorm";
- import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
- import cheerio from "cheerio";
- const urlPattern = /diary\.php\?no=(\d+)/i;
- const kissDiaryRoot = "http://www.kisskiss.tv/kiss";
- const FEED_NAME = "kisskisstv-diary";
- async function aggregate() {
- let repo = getRepository(AggroNewsItem);
- let lastPost = await repo.findOne({
- select: [ "newsId" ],
- where: { feedName: FEED_NAME },
- order: { newsId: "DESC" }
- });
- if(!lastPost)
- lastPost = repo.create({
- newsId: 0
- });
-
- try {
- let mainPageRes = await request(`${kissDiaryRoot}/diary.php`, {resolveWithFullResponse: true}) as Response;
-
- if(mainPageRes.statusCode != 200)
- return [];
- let rootNode = cheerio.load(mainPageRes.body);
- let diaryEntryNames = rootNode("table.blog_frame_top");
-
- if(diaryEntryNames.length == 0) {
- console.log("[KISS DIARY] Failed to find listing!");
- }
- let diaryTexts = rootNode("div.blog_frame_middle");
- let items = diaryEntryNames.map((i, e) => ({ table: e, content: diaryTexts.get(i) }));
- let result : INewsItem[] = [];
- let latestEntry = lastPost.newsId;
- for(let {table, content} of items.get() as {table: CheerioElement, content: CheerioElement}[]) {
- let a = cheerio(table).find("a");
- let link = a.attr("href");
- let matches = link ? urlPattern.exec(link) : false;
- if(!matches)
- continue;
-
- let id = +matches[1];
- if(id <= lastPost.newsId)
- continue;
- if(id > latestEntry)
- latestEntry = id;
- let diaryLink = `${kissDiaryRoot}/${link}`;
- let contentCh = cheerio(content);
- let title = a.text();
-
- let bottomFrame = contentCh.find("div.blog_data");
- bottomFrame.remove();
- result.push({
- newsId: id,
- feedId: FEED_NAME,
- link: diaryLink,
- title: title,
- author: "KISS BLOG",
- contents: contentCh.html() ?? "",
- embedColor: 0xf4c100,
- needsTranslation: true
- });
- }
- return result;
- } catch(err) {
- return [];
- }
- }
- export default {
- aggregate: aggregate
- } as IAggregator;
|