|
| 1 | +import { CheerioAPI, load as parseHTML } from 'cheerio'; |
| 2 | +import { fetchApi } from '@libs/fetch'; |
| 3 | +import { Plugin } from '@/types/plugin'; |
| 4 | +import { Filters, FilterTypes } from '@libs/filterInputs'; |
| 5 | +import { storage } from '@libs/storage'; |
| 6 | +import { defaultCover } from '@libs/defaultCover'; |
| 7 | +import { NovelStatus } from '@libs/novelStatus'; |
| 8 | + |
| 9 | +/** |
| 10 | + * BaiLianTales moved from the Madara theme to its own "jormuntl" WordPress |
| 11 | + * theme, so it no longer fits the madara multisrc template. |
| 12 | + * |
| 13 | + * - Listing: `/novel/page/<n>/?orderby=…&status=…&genre[]=…`, cards are |
| 14 | + * `article.novel-card`. |
| 15 | + * - Search: `/page/<n>/?s=<term>`, results in `#jt-search-results`. |
| 16 | + * - Novel page: `/novel/<slug>/`. The table of contents shows 50 chapters |
| 17 | + * per page; further pages are plain links (`?toc_page=<n>`). |
| 18 | + * - Chapter page: `/novel/<slug>/<chapter-slug>/`, text in |
| 19 | + * `.chapter-content`. Premium chapters render a login overlay |
| 20 | + * instead, so they are listed as locked (🔒) and can be hidden |
| 21 | + * with the "Hide locked chapters" setting. |
| 22 | + */ |
| 23 | +class BaiLianTales implements Plugin.PluginBase { |
| 24 | + id = 'bailiantales'; |
| 25 | + name = 'BaiLianTales'; |
| 26 | + version = '2.3.0'; |
| 27 | + icon = 'src/en/bailiantales/icon.png'; |
| 28 | + site = 'https://bailiantales.com/'; |
| 29 | + |
| 30 | + pluginSettings = { |
| 31 | + hideLocked: { |
| 32 | + value: '', |
| 33 | + label: 'Hide locked chapters', |
| 34 | + type: 'Switch', |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + private async fetchPage(url: string): Promise<CheerioAPI | undefined> { |
| 39 | + const res = await fetchApi(url); |
| 40 | + // Paging past the last listing or search page is a 404, not an error. |
| 41 | + if (res.status === 404) return undefined; |
| 42 | + // Throw (carrying the HTTP status) so a runner-side block is reported |
| 43 | + // INCONCLUSIVE per docs/testing.md instead of an empty-result FAIL. |
| 44 | + if (!res.ok) { |
| 45 | + throw Object.assign(new Error('Request failed: ' + res.status), { |
| 46 | + status: res.status, |
| 47 | + }); |
| 48 | + } |
| 49 | + return parseHTML(await res.text()); |
| 50 | + } |
| 51 | + |
| 52 | + private toPath(href: string | undefined): string | undefined { |
| 53 | + if (!href?.startsWith(this.site)) return undefined; |
| 54 | + return href.slice(this.site.length); |
| 55 | + } |
| 56 | + |
| 57 | + async popularNovels( |
| 58 | + pageNo: number, |
| 59 | + { |
| 60 | + showLatestNovels, |
| 61 | + filters, |
| 62 | + }: Plugin.PopularNovelsOptions<typeof this.filters>, |
| 63 | + ): Promise<Plugin.NovelItem[]> { |
| 64 | + const params = [ |
| 65 | + 'orderby=' + (showLatestNovels ? 'updated' : filters.sort.value), |
| 66 | + ]; |
| 67 | + if (filters.status.value) params.push('status=' + filters.status.value); |
| 68 | + for (const genre of filters.genres.value) { |
| 69 | + params.push('genre[]=' + genre); |
| 70 | + } |
| 71 | + |
| 72 | + const $ = await this.fetchPage( |
| 73 | + this.site + 'novel/page/' + pageNo + '/?' + params.join('&'), |
| 74 | + ); |
| 75 | + if (!$) return []; |
| 76 | + |
| 77 | + const novels: Plugin.NovelItem[] = []; |
| 78 | + $('article.novel-card').each((_, el) => { |
| 79 | + const link = $(el).find('h3 a'); |
| 80 | + const path = this.toPath(link.attr('href')); |
| 81 | + if (!path) return; |
| 82 | + novels.push({ |
| 83 | + name: link.text().trim(), |
| 84 | + path, |
| 85 | + cover: $(el).find('img').attr('src') || defaultCover, |
| 86 | + }); |
| 87 | + }); |
| 88 | + return novels; |
| 89 | + } |
| 90 | + |
| 91 | + private parseChapters($: CheerioAPI): Plugin.ChapterItem[] { |
| 92 | + const hideLocked = storage.get('hideLocked'); |
| 93 | + const chapters: Plugin.ChapterItem[] = []; |
| 94 | + $('#novel-toc .chapter-item-improved').each((_, el) => { |
| 95 | + const path = this.toPath($(el).find('a').first().attr('href')); |
| 96 | + if (!path) return; |
| 97 | + const locked = $(el).find('[data-jt-unlock]').length > 0; |
| 98 | + if (locked && hideLocked) return; |
| 99 | + const name = $(el).find('.jt-toc-title').text().trim(); |
| 100 | + chapters.push({ |
| 101 | + name: locked ? `🔒 ${name}` : name, |
| 102 | + path, |
| 103 | + releaseTime: $(el).find('.jt-toc-date').text().trim() || undefined, |
| 104 | + }); |
| 105 | + }); |
| 106 | + return chapters; |
| 107 | + } |
| 108 | + |
| 109 | + async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> { |
| 110 | + const $ = await this.fetchPage(this.site + novelPath); |
| 111 | + if (!$) throw new Error('Novel not found'); |
| 112 | + |
| 113 | + const novel: Plugin.SourceNovel = { |
| 114 | + path: novelPath, |
| 115 | + name: $('.jt-novel-details h1').text().trim() || 'Untitled', |
| 116 | + cover: $('.novel-hero img.wp-post-image').attr('src') || defaultCover, |
| 117 | + summary: $('.jt-hero-synopsis p') |
| 118 | + .map((_, el) => $(el).text().trim()) |
| 119 | + .get() |
| 120 | + .filter(line => line && !/^synopsis:?$/i.test(line)) |
| 121 | + .join('\n\n'), |
| 122 | + genres: $('.jt-novel-details a.genre-tag') |
| 123 | + .map((_, el) => $(el).text().trim()) |
| 124 | + .get() |
| 125 | + .join(', '), |
| 126 | + }; |
| 127 | + |
| 128 | + switch ($('.jt-novel-details .status-badge').text().trim().toLowerCase()) { |
| 129 | + case 'ongoing': |
| 130 | + novel.status = NovelStatus.Ongoing; |
| 131 | + break; |
| 132 | + case 'completed': |
| 133 | + novel.status = NovelStatus.Completed; |
| 134 | + break; |
| 135 | + case 'hiatus': |
| 136 | + novel.status = NovelStatus.OnHiatus; |
| 137 | + break; |
| 138 | + } |
| 139 | + |
| 140 | + const chapters = this.parseChapters($); |
| 141 | + const tocPages = $('#novel-toc .jt-page-jump option').length; |
| 142 | + for (let page = 2; page <= tocPages; page++) { |
| 143 | + const toc = await this.fetchPage( |
| 144 | + this.site + novelPath + '?toc_page=' + page, |
| 145 | + ); |
| 146 | + if (toc) chapters.push(...this.parseChapters(toc)); |
| 147 | + } |
| 148 | + novel.chapters = chapters; |
| 149 | + |
| 150 | + return novel; |
| 151 | + } |
| 152 | + |
| 153 | + async parseChapter(chapterPath: string): Promise<string> { |
| 154 | + const $ = await this.fetchPage(this.site + chapterPath); |
| 155 | + if (!$) throw new Error('Chapter not found'); |
| 156 | + if ($('.locked-chapter-overlay').length) { |
| 157 | + throw new Error( |
| 158 | + 'This chapter is locked. Unlock it on BaiLianTales to read it.', |
| 159 | + ); |
| 160 | + } |
| 161 | + const content = $('.chapter-content').first(); |
| 162 | + if (!content.length) throw new Error('Could not find the chapter text.'); |
| 163 | + |
| 164 | + // Strip the hidden anti-copy watermarks (spans and HTML comments) the |
| 165 | + // site sprinkles through the text ("[bailiantales.com]", …). |
| 166 | + content |
| 167 | + .find( |
| 168 | + 'script, style, .sr-only, .watermark-hidden, [data-watermark], [aria-hidden="true"]', |
| 169 | + ) |
| 170 | + .remove(); |
| 171 | + content.find('[style]').each((_, el) => { |
| 172 | + const style = $(el).attr('style') || ''; |
| 173 | + if (/display\s*:\s*none|font-size\s*:\s*0(?![.\d])/i.test(style)) { |
| 174 | + $(el).remove(); |
| 175 | + } |
| 176 | + }); |
| 177 | + content |
| 178 | + .find('*') |
| 179 | + .addBack() |
| 180 | + .contents() |
| 181 | + .filter((_, node) => node.type === 'comment') |
| 182 | + .remove(); |
| 183 | + return content.html() || ''; |
| 184 | + } |
| 185 | + |
| 186 | + async searchNovels( |
| 187 | + searchTerm: string, |
| 188 | + pageNo: number, |
| 189 | + ): Promise<Plugin.NovelItem[]> { |
| 190 | + const $ = await this.fetchPage( |
| 191 | + this.site + |
| 192 | + 'page/' + |
| 193 | + pageNo + |
| 194 | + '/?s=' + |
| 195 | + encodeURIComponent(searchTerm.trim()), |
| 196 | + ); |
| 197 | + if (!$) return []; |
| 198 | + |
| 199 | + const novels: Plugin.NovelItem[] = []; |
| 200 | + $('#jt-search-results article').each((_, el) => { |
| 201 | + const link = $(el).find('h3 a'); |
| 202 | + const path = this.toPath(link.attr('href')); |
| 203 | + // Only novel pages (`novel/<slug>/`), not chapters or other posts. |
| 204 | + if (!path || !/^novel\/[^/]+\/$/.test(path)) return; |
| 205 | + novels.push({ |
| 206 | + name: link.text().trim(), |
| 207 | + path, |
| 208 | + cover: $(el).find('img').attr('src') || defaultCover, |
| 209 | + }); |
| 210 | + }); |
| 211 | + return novels; |
| 212 | + } |
| 213 | + |
| 214 | + resolveUrl = (path: string) => this.site + path; |
| 215 | + |
| 216 | + filters = { |
| 217 | + sort: { |
| 218 | + label: 'Sort By', |
| 219 | + value: 'date', |
| 220 | + options: [ |
| 221 | + { label: 'Recently Added', value: 'date' }, |
| 222 | + { label: 'Recently Updated', value: 'updated' }, |
| 223 | + // Only novels that have been rated are listed. |
| 224 | + { label: 'Highest Rated', value: 'rating' }, |
| 225 | + { label: 'Most Chapters', value: 'chapters' }, |
| 226 | + { label: 'A-Z', value: 'title' }, |
| 227 | + ], |
| 228 | + type: FilterTypes.Picker, |
| 229 | + }, |
| 230 | + status: { |
| 231 | + label: 'Status', |
| 232 | + value: '', |
| 233 | + options: [ |
| 234 | + { label: 'All', value: '' }, |
| 235 | + { label: 'Ongoing', value: 'ongoing' }, |
| 236 | + { label: 'Completed', value: 'completed' }, |
| 237 | + { label: 'Hiatus', value: 'hiatus' }, |
| 238 | + ], |
| 239 | + type: FilterTypes.Picker, |
| 240 | + }, |
| 241 | + genres: { |
| 242 | + label: 'Genres', |
| 243 | + value: [], |
| 244 | + options: [ |
| 245 | + { label: 'ABO', value: 'abo' }, |
| 246 | + { label: 'Action', value: 'action' }, |
| 247 | + { label: 'Adult', value: 'adult' }, |
| 248 | + { label: 'Adventure', value: 'adventure' }, |
| 249 | + { label: 'BL', value: 'bl' }, |
| 250 | + { label: 'Comedy', value: 'comedy' }, |
| 251 | + { label: 'Cultivation', value: 'cultivation' }, |
| 252 | + { label: 'Drama', value: 'drama' }, |
| 253 | + { label: 'Fantasy', value: 'fantasy' }, |
| 254 | + { label: 'Game', value: 'game' }, |
| 255 | + { label: 'Historical', value: 'historical' }, |
| 256 | + { label: 'Horror', value: 'horror' }, |
| 257 | + { label: 'Martial Arts', value: 'martial-arts' }, |
| 258 | + { label: 'Mature', value: 'mature' }, |
| 259 | + { label: 'Mecha', value: 'mecha' }, |
| 260 | + { label: 'Modern Day', value: 'modern-day' }, |
| 261 | + { label: 'Mystery', value: 'mystery' }, |
| 262 | + { label: 'Reincarnation', value: 'reincarnation' }, |
| 263 | + { label: 'Romance', value: 'romance' }, |
| 264 | + { label: 'School Life', value: 'school-life' }, |
| 265 | + { label: 'Sci-Fi', value: 'sci-fi' }, |
| 266 | + { label: 'Shoujo Ai', value: 'shoujo-ai' }, |
| 267 | + { label: 'Slice of Life', value: 'slice-of-life' }, |
| 268 | + { label: 'Smut', value: 'smut' }, |
| 269 | + { label: 'Supernatural', value: 'supernatural' }, |
| 270 | + { label: 'System', value: 'system' }, |
| 271 | + { label: 'Tragedy', value: 'tragedy' }, |
| 272 | + { label: 'Transmigration', value: 'transmigration' }, |
| 273 | + { label: 'Virtual Reality', value: 'virtual-reality' }, |
| 274 | + { label: 'Wuxia', value: 'wuxia' }, |
| 275 | + { label: 'Xianxia', value: 'xianxia' }, |
| 276 | + { label: 'Xuanhuan', value: 'xuanhuan' }, |
| 277 | + { label: 'Yaoi', value: 'yaoi' }, |
| 278 | + { label: 'Yuri', value: 'yuri' }, |
| 279 | + ], |
| 280 | + type: FilterTypes.CheckboxGroup, |
| 281 | + }, |
| 282 | + } satisfies Filters; |
| 283 | +} |
| 284 | + |
| 285 | +export default new BaiLianTales(); |
0 commit comments