Browse Source

Fix KISS diary aggro

ghorsington 5 years ago
parent
commit
7f8e8d822d
1 changed files with 16 additions and 20 deletions
  1. 16 20
      bot/src/commands/aggregators/kiss_diary.ts

+ 16 - 20
bot/src/commands/aggregators/kiss_diary.ts

@@ -30,49 +30,45 @@ async function aggregate() {
             return [];
 
         let rootNode = cheerio.load(mainPageRes.body);
-
-        let diaryEntries = rootNode("div.blog_frame_middle ul.disc li a");
-
-        if(diaryEntries.length == 0) {
+        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 a of diaryEntries.get() as CheerioElement[]) {
-            let matches = urlPattern.exec(a.attribs.href);
+        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 = urlPattern.exec(link);
             if(!matches)
                 continue;
             
             let id = +matches[1];
-
             if(id <= lastPost.newsId)
                 continue;
 
             if(id > latestEntry)
                 latestEntry = id;
 
-            let diaryLink = `${kissDiaryRoot}/${a.attribs.href}`;
-            let res = await request(diaryLink, {resolveWithFullResponse: true}) as Response;
-
-            if(res.statusCode != 200)
-                continue;
-
-            let node = cheerio.load(res.body);
-
-            let title = node("table.blog_frame_top tr td a");
-            let contents = node("div.blog_frame_middle");
-            let bottomFrame = contents.find("div.blog_data");
+            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.text(),
+                title: title,
                 author: "KISS BLOG",
-                contents: contents.html(),
+                contents: contentCh.html(),
                 embedColor: 0xf4c100,
                 needsTranslation: true
             });