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 kissDiaryRoot = "https://com3d2.world/r18/notices.php"; const FEED_NAME = "com3d2-world-notices"; 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, {resolveWithFullResponse: true}) as Response; if(mainPageRes.statusCode != 200) return []; let rootNode = cheerio.load(mainPageRes.body); let diaryEntries = rootNode("div.frame a"); if(!diaryEntries) { console.log("[COM3D2 WORLD BLOG] Failed to find listing!"); } let result : INewsItem[] = []; let latestEntry = lastPost.newsId; for(let a of diaryEntries.get() as CheerioElement[]) { if(!a.attribs.id) continue; let id = +a.attribs.id; if(id <= lastPost.newsId) continue; if(id > latestEntry) latestEntry = id; let diaryLink = `${kissDiaryRoot}?no=${id}`; let res = await request(diaryLink, {resolveWithFullResponse: true}) as Response; if(res.statusCode != 200) continue; let node = cheerio.load(res.body); let title = node("div.frame div.notice_title th"); let contents = node("div.frame div").get(1); result.push({ newsId: id, feedId: FEED_NAME, link: diaryLink, title: title.text(), author: "com3d2.world", contents: cheerio.html(contents), embedColor: 0xa39869 }); } return result; } catch(err) { return []; } } export default { aggregate: aggregate } as IAggregator;