From cda9a0baac72b848073966dd49a28ad9eba58e55 Mon Sep 17 00:00:00 2001 From: nyagami Date: Sun, 24 Mar 2024 13:53:43 +0700 Subject: [PATCH] Update lastest chapter logic (#853) * update lastestchapter logic * update version --- plugins/english/ranobes.ts | 41 +++++++++++-------------------- plugins/spanish/tunovelaligera.ts | 23 ++++++----------- test_web/index.html | 2 -- test_web/static/js/index.js | 12 ++++----- types/plugin.ts | 2 +- 5 files changed, 29 insertions(+), 51 deletions(-) diff --git a/plugins/english/ranobes.ts b/plugins/english/ranobes.ts index 32985ff..11324df 100644 --- a/plugins/english/ranobes.ts +++ b/plugins/english/ranobes.ts @@ -16,7 +16,7 @@ class RanobesPlugin implements Plugin.PagePlugin { icon = 'src/en/ranobes/icon.png'; site = 'https://ranobes.top'; filters?: Filters | undefined; - version = '1.0.0'; + version = '1.1.0'; async sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); @@ -119,6 +119,17 @@ class RanobesPlugin implements Plugin.PagePlugin { novel.totalPages = Number(data.pages_count); novel.chapters = this.parseChapters(data); + const latestChapterUrl = data.chapters[0].link; + const latestChapterName = data.chapters[0].title; + const latestChapterDate = data.chapters[0].date; + + novel.latestChapter = latestChapterUrl + ? { + path: latestChapterUrl.replace(this.site, ''), + name: latestChapterName, + releaseTime: new Date(latestChapterDate).toISOString(), + } + : undefined; return novel; } @@ -126,35 +137,13 @@ class RanobesPlugin implements Plugin.PagePlugin { const pagePath = novelPath.split('-')[0]; const firstUrl = this.site + '/chapters' + pagePath.replace('novels/', ''); const pageUrl = firstUrl + '/page/' + page; - - const body = await fetchApi(firstUrl).then(r => r.text()); - - let loadedCheerio = parseHTML(body); - let json = loadedCheerio('#dle-content main').next().html()!; - let data = JSON.parse(json.replace('window.__DATA__ =', '')); - - const latestChapterUrl = data.chapters[0].link; - const latestChapterName = data.chapters[0].title; - const latestChapterDate = data.chapters[0].date; - - const latestChapter: Plugin.ChapterItem | undefined = latestChapterUrl - ? { - path: latestChapterUrl.replace(this.site, ''), - name: latestChapterName, - releaseTime: new Date(latestChapterDate).toISOString(), - } - : undefined; - - await this.sleep(1000); - const pageBody = await fetchApi(pageUrl).then(r => r.text()); - loadedCheerio = parseHTML(pageBody); - json = loadedCheerio('#dle-content main').next().html()!; - data = JSON.parse(json.replace('window.__DATA__ =', '')); + const loadedCheerio = parseHTML(pageBody); + const json = loadedCheerio('#dle-content main').next().html()!; + const data = JSON.parse(json.replace('window.__DATA__ =', '')); const chapters = this.parseChapters(data); return { chapters, - latestChapter, }; } diff --git a/plugins/spanish/tunovelaligera.ts b/plugins/spanish/tunovelaligera.ts index b9d6be4..baa3246 100644 --- a/plugins/spanish/tunovelaligera.ts +++ b/plugins/spanish/tunovelaligera.ts @@ -10,7 +10,7 @@ class TuNovelaLigera implements Plugin.PagePlugin { name = 'TuNovelaLigera'; icon = 'src/es/tunovelaligera/icon.png'; site = 'https://tunovelaligera.com'; - version = '1.0.0'; + version = '1.1.0'; async sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); @@ -122,17 +122,6 @@ class TuNovelaLigera implements Plugin.PagePlugin { novel.summary = loadedCheerio('div.summary__content > p').text().trim(); novel.chapters = this.parseChapters(loadedCheerio); - - return novel; - } - - async parsePage(novelPath: string, page: string): Promise { - const novelUrl = this.site + novelPath; - const pageUrl = this.site + novelPath + '?lcp_page0=' + page; - const result = await fetchApi(novelUrl); - const body = await result.text(); - - let loadedCheerio = parseHTML(body); const latestChapterEle = loadedCheerio('#lcp_instance_0 li').first(); const latestChapterUrl = loadedCheerio(latestChapterEle) .find('a') @@ -142,18 +131,22 @@ class TuNovelaLigera implements Plugin.PagePlugin { .text() .replace(/[\t\n]/g, '') .trim(); - const latestChapter: Plugin.ChapterItem | undefined = latestChapterUrl + novel.latestChapter = latestChapterUrl ? { path: latestChapterUrl.replace(this.site, ''), name: latestChapterName, } : undefined; - await this.sleep(1000); + + return novel; + } + + async parsePage(novelPath: string, page: string): Promise { + const pageUrl = this.site + novelPath + '?lcp_page0=' + page; const pageText = await fetchApi(pageUrl).then(res => res.text()); const chapters = this.parseChapters(parseHTML(pageText)); return { chapters, - latestChapter, }; } async parseChapter(chapterPath: string): Promise { diff --git a/test_web/index.html b/test_web/index.html index b14c35d..de49587 100644 --- a/test_web/index.html +++ b/test_web/index.html @@ -197,8 +197,6 @@ Loading...
-
-
diff --git a/test_web/static/js/index.js b/test_web/static/js/index.js index 848543c..23a6163 100644 --- a/test_web/static/js/index.js +++ b/test_web/static/js/index.js @@ -849,6 +849,11 @@ class PluginWrapper { ), ); } + if (sourceNovel.latestChapter) { + novel_data + .children('div + div') + .append(PluginWrapper.createChapterItem(sourceNovel.latestChapter)); + } novel_item.replaceWith(novel_data); if ( @@ -872,7 +877,6 @@ class PluginWrapper { /** @type {JQuery} */ const novelPathInput = $('#parsePage #parse-page-novel-path'); const pageInput = $('#parsePage #parse-page-index'); - const latestChapter_item = $('#parseNovel latest-chapter-item'); const chapter_list = $('#parsePage .chapter-list'); const novelPath = novelPathInput.val(); const page = pageInput.val(); @@ -895,12 +899,6 @@ class PluginWrapper { ).json(); if ('error' in sourcePage) throw `Error getting the novel ${sourcePage.error}`; - latestChapter_item.empty(); - if (sourcePage.latestChapter) { - latestChapter_item.append( - PluginWrapper.createChapterItem(sourcePage.latestChapter), - ); - } if ( sourcePage?.chapters?.length !== new Set(sourcePage?.chapters?.map(r => r.path) || []).size diff --git a/types/plugin.ts b/types/plugin.ts index 50a9963..ee9ea1d 100644 --- a/types/plugin.ts +++ b/types/plugin.ts @@ -32,11 +32,11 @@ export namespace Plugin { artist?: string; status?: string; chapters?: ChapterItem[]; + latestChapter?: ChapterItem; } export interface SourcePage { chapters: ChapterItem[]; - latestChapter?: ChapterItem; } export interface PopularNovelsOptions<