diff --git a/plugins/english/curspe.ts b/plugins/english/curspe.ts new file mode 100644 index 0000000000..607b0a4881 --- /dev/null +++ b/plugins/english/curspe.ts @@ -0,0 +1,395 @@ +import { fetchApi } from '@libs/fetch'; +import { Filters, FilterTypes } from '@libs/filterInputs'; +import { NovelStatus } from '@libs/novelStatus'; +import { + assertNoChallenge, + checkResponse, + createPluginError, + PluginError, +} from '@libs/pluginError'; +import { storage } from '@libs/storage'; +import { Plugin } from '@/types/plugin'; +import { CheerioAPI, load as parseHTML } from 'cheerio'; + +/** + * curspe.com is WordPress (theme JormunTL). Its REST API needs a login, so + * the plugin reads the pages a visitor gets: + * + * - browse: `/novels/?orderby=&status=&language=&genres=a,b&min_chapters=&pg=N` + * (24 per page; past the end the page says "No series match") + * - search: `/?s=&post_type=webnovel` (every match on one page) + * - novel: `/novels//`; the whole chapter list is in the page as + * `a.wn-chapter-item[data-locked="0|1"]` + * - chapter: `/novels///`, text in `.chapter-content`; a + * premium chapter shows `.locked-chapter-overlay` instead + * + * Paths: `novels/` and `novels//`. + */ +const SITE = 'https://curspe.com/'; + +const clean = (s: unknown): string => + String(s ?? '') + .replace(/\s+/g, ' ') + .trim(); + +const absolute = (u: string | undefined): string | undefined => { + if (!u) return undefined; + if (/^https?:/i.test(u)) return u; + if (u.startsWith('//')) return 'https:' + u; + return SITE + u.replace(/^\/+/, ''); +}; + +/** A curspe.com URL as a path (`novels/...`, no slashes at the ends). */ +const pathOf = (url: string | undefined): string | null => { + const m = String(url || '').match( + /^https?:\/\/(?:www\.)?curspe\.com\/(novels\/[^?#]+?)\/?(?:[?#].*)?$/i, + ); + return m ? m[1].replace(/\/+$/, '') : null; +}; + +const MONTHS: Record = { + jan: 1, + feb: 2, + mar: 3, + apr: 4, + may: 5, + jun: 6, + jul: 7, + aug: 8, + sep: 9, + oct: 10, + nov: 11, + dec: 12, +}; + +/** "Mar 7, 2026" -> "2026-03-07" (by hand: no time-zone shift). */ +const isoDate = (text: string): string | undefined => { + const m = clean(text).match(/^([A-Za-z]{3})[a-z]*\.? (\d{1,2}), (\d{4})$/); + const month = m && MONTHS[m[1].toLowerCase()]; + if (!m || !month) return undefined; + return ( + m[3] + '-' + String(month).padStart(2, '0') + '-' + m[2].padStart(2, '0') + ); +}; + +const STATUS: Record = { + translating: NovelStatus.Ongoing, + ongoing: NovelStatus.Ongoing, + completed: NovelStatus.Completed, + 'on hold': NovelStatus.OnHiatus, + hiatus: NovelStatus.OnHiatus, + dropped: NovelStatus.Cancelled, + licensed: NovelStatus.Licensed, +}; + +class CurspePlugin implements Plugin.PluginBase { + id = 'curspe'; + name = 'Curspe'; + icon = 'src/en/curspe/icon.png'; + site = SITE; + version = '1.0.0'; + + pluginSettings = { + hideLocked: { + value: false, + label: 'Hide locked chapters', + type: 'Switch', + }, + }; + + // The options the site's own browse page offers; several genres must all + // match. + filters = { + sort: { + type: FilterTypes.Picker, + label: 'Sort', + value: 'views', + options: [ + { label: 'Most viewed', value: 'views' }, + { label: 'Newest', value: 'date' }, + { label: 'Trending', value: 'trending' }, + { label: 'Most favourited', value: 'favorites' }, + { label: 'Top rated', value: 'rating' }, + { label: 'Most chapters', value: 'chapters' }, + { label: 'Title A-Z', value: 'title' }, + ], + }, + status: { + type: FilterTypes.Picker, + label: 'Status', + value: '', + options: [ + { label: 'All', value: '' }, + { label: 'Translating', value: 'translating' }, + { label: 'Completed', value: 'completed' }, + { label: 'On hold', value: 'onhold' }, + { label: 'Hiatus', value: 'hiatus' }, + { label: 'Dropped', value: 'dropped' }, + { label: 'Licensed', value: 'licensed' }, + ], + }, + language: { + type: FilterTypes.Picker, + label: 'Original language', + value: '', + options: [ + { label: 'All', value: '' }, + { label: 'Chinese', value: 'chinese' }, + { label: 'Japanese', value: 'japanese' }, + { label: 'Korean', value: 'korean' }, + ], + }, + minChapters: { + type: FilterTypes.Picker, + label: 'Minimum chapters', + value: '', + options: [ + { label: 'Any length', value: '' }, + { label: '10+', value: '10' }, + { label: '25+', value: '25' }, + { label: '50+', value: '50' }, + { label: '100+', value: '100' }, + { label: '200+', value: '200' }, + { label: '500+', value: '500' }, + ], + }, + genres: { + type: FilterTypes.CheckboxGroup, + label: 'Genres (all of)', + value: [], + options: [ + { label: 'Action', value: 'action' }, + { label: 'Adult', value: 'adult' }, + { label: 'Adventure', value: 'adventure' }, + { label: 'Arts', value: 'arts' }, + { label: 'Comedy', value: 'comedy' }, + { label: 'Cultivation', value: 'cultivation' }, + { label: 'Drama', value: 'drama' }, + { label: 'Ecchi', value: 'ecchi' }, + { label: 'Fantasy', value: 'fantasy' }, + { label: 'Game', value: 'game' }, + { label: 'Gender Bender', value: 'gender-bender' }, + { label: 'Harem', value: 'harem' }, + { label: 'Historical', value: 'historical' }, + { label: 'Horror', value: 'horror' }, + { label: 'Josei', value: 'josei' }, + { label: 'Martial', value: 'martial' }, + { label: 'Martial Arts', value: 'martial-arts' }, + { label: 'Mature', value: 'mature' }, + { label: 'Mecha', value: 'mecha' }, + { label: 'Modern Day', value: 'modern-day' }, + { label: 'Mystery', value: 'mystery' }, + { label: 'Psychological', value: 'psychological' }, + { label: 'Reincarnation', value: 'reincarnation' }, + { label: 'Romance', value: 'romance' }, + { label: 'School Life', value: 'school-life' }, + { label: 'Sci-Fi', value: 'sci-fi' }, + { label: 'Seinen', value: 'seinen' }, + { label: 'Shoujo', value: 'shoujo' }, + { label: 'Shoujo Ai', value: 'shoujo-ai' }, + { label: 'Shounen', value: 'shounen' }, + { label: 'Slice of Life', value: 'slice-of-life' }, + { label: 'Smut', value: 'smut' }, + { label: 'Sports', value: 'sports' }, + { label: 'Supernatural', value: 'supernatural' }, + { label: 'System', value: 'system' }, + { label: 'Tragedy', value: 'tragedy' }, + { label: 'Transmigration', value: 'transmigration' }, + { label: 'Virtual Reality', value: 'virtual-reality' }, + { label: 'Wuxia', value: 'wuxia' }, + { label: 'Xianxia', value: 'xianxia' }, + { label: 'Xuanhuan', value: 'xuanhuan' }, + { label: 'Yuri', value: 'yuri' }, + ], + }, + } satisfies Filters; + + private async fetchPage(url: string): Promise { + const r = await fetchApi(url, { headers: { Accept: 'text/html' } }); + checkResponse(r, { search: url.includes('?s=') }); + const html = await r.text(); + assertNoChallenge(html, url, r.url); + return parseHTML(html); + } + + private layoutChanged(what: string, url: string) { + return createPluginError(PluginError.InvalidResponse, { + message: `Curspe's ${what}: its layout changed.`, + url, + }); + } + + async popularNovels( + pageNo: number, + { + showLatestNovels, + filters, + }: Plugin.PopularNovelsOptions, + ): Promise { + const query = [ + 'orderby=' + + encodeURIComponent( + showLatestNovels ? 'date' : filters?.sort?.value || 'views', + ), + ]; + if (filters?.status?.value) { + query.push('status=' + encodeURIComponent(filters.status.value)); + } + if (filters?.language?.value) { + query.push('language=' + encodeURIComponent(filters.language.value)); + } + if (filters?.minChapters?.value) { + query.push( + 'min_chapters=' + encodeURIComponent(filters.minChapters.value), + ); + } + const genres = filters?.genres?.value || []; + if (genres.length) { + query.push('genres=' + genres.map(encodeURIComponent).join(',')); + } + if (pageNo > 1) query.push('pg=' + pageNo); + const url = SITE + 'novels/?' + query.join('&'); + const $ = await this.fetchPage(url); + const cards = $('article.jarc-card'); + if (!cards.length) { + if (/No series match/i.test($('body').text())) return []; + throw this.layoutChanged('browse page has no novel cards', url); + } + const novels: Plugin.NovelItem[] = []; + cards.each((_, el) => { + const link = $(el).find('a.jarc-name').first(); + const path = pathOf(link.attr('href')); + if (!path) return; + novels.push({ + name: clean(link.text()), + path, + cover: absolute($(el).find('a.jarc-cover img').first().attr('src')), + }); + }); + return novels; + } + + async searchNovels( + searchTerm: string, + pageNo: number, + ): Promise { + // Every match is on the first results page; later pages are 404. + if (pageNo > 1) return []; + const url = + SITE + '?s=' + encodeURIComponent(searchTerm) + '&post_type=webnovel'; + const $ = await this.fetchPage(url); + const novels: Plugin.NovelItem[] = []; + const seen: Record = {}; + $('article').each((_, el) => { + const link = $(el).find('h3 a, h2 a').first(); + const path = pathOf(link.attr('href')); + if (!path || path.split('/').length !== 2 || seen[path]) return; + seen[path] = true; + novels.push({ + name: clean(link.text()), + path, + cover: absolute($(el).find('img').first().attr('src')), + }); + }); + if (!novels.length && !/No Results Found/i.test($('body').text())) { + throw this.layoutChanged('search page has no results and no notice', url); + } + return novels; + } + + async parseNovel(novelPath: string): Promise { + const slug = novelPath.replace(/^\/+|\/+$/g, '').split('/')[1]; + if (!slug) { + throw createPluginError(PluginError.NovelNotFound, { + message: 'Not a Curspe novel path: ' + novelPath, + }); + } + const path = 'novels/' + slug; + const url = SITE + path + '/'; + const $ = await this.fetchPage(url); + const name = clean($('h1.wn-title').first().text()); + if (!name) { + throw createPluginError(PluginError.NovelNotFound, { url }); + } + const meta: Record = {}; + $('.wn-meta-row').each((_, el) => { + meta[clean($(el).find('.wn-meta-label').text()).toLowerCase()] = clean( + $(el).find('.wn-meta-value').text(), + ); + }); + + const hideLocked = storage.get('hideLocked'); + const chapters: Plugin.ChapterItem[] = []; + const seen: Record = {}; + $('a.wn-chapter-item').each((_, el) => { + const a = $(el); + const chapterPath = pathOf(a.attr('href')); + if (!chapterPath || chapterPath.split('/').length < 3) return; + if (seen[chapterPath]) return; + seen[chapterPath] = true; + const locked = a.attr('data-locked') === '1'; + if (locked && hideLocked) return; + const title = + clean(a.find('.wn-chapter-title').first().text()) || + clean(a.attr('data-title')); + const num = title.match(/chapter\s*([0-9]+(?:\.[0-9]+)?)/i)?.[1]; + chapters.push({ + name: (locked ? '🔒 ' : '') + title, + path: chapterPath, + releaseTime: isoDate(a.find('.wn-chapter-meta span').first().text()), + chapterNumber: num ? parseFloat(num) : undefined, + }); + }); + + const novel: Plugin.SourceNovel = { path, name, chapters }; + const cover = absolute($('.wn-cover img').first().attr('src')); + if (cover) novel.cover = cover; + const synopsis = $('.wn-synopsis').first(); + const summary = + synopsis + .find('p') + .map((_, p) => clean($(p).text())) + .get() + .filter(Boolean) + .join('\n\n') || clean(synopsis.text()); + if (summary) novel.summary = summary; + if (meta.author) novel.author = meta.author; + const status = clean($('.wn-status-pill').first().text()).toLowerCase(); + if (status) novel.status = STATUS[status] || NovelStatus.Unknown; + const genres = $('.wn-genre-list a.wn-genre') + .map((_, g) => clean($(g).text())) + .get(); + if (genres.length) novel.genres = genres.join(', '); + return novel; + } + + async parseChapter(chapterPath: string): Promise { + const url = SITE + chapterPath.replace(/^\/+|\/+$/g, '') + '/'; + const $ = await this.fetchPage(url); + if ($('.locked-chapter-overlay').length) { + throw createPluginError(PluginError.AuthRequired, { + message: + 'This is a premium chapter on Curspe. Unlock it on curspe.com (signed in, in WebView), then reload it.', + url, + }); + } + const content = $('.chapter-content').first(); + if (!content.length) { + throw this.layoutChanged('chapter page has no text block', url); + } + content + .find( + 'script, style, noscript, ins, iframe, button, nav, form, .adsbygoogle, [class*="ad-slot"]', + ) + .remove(); + if (clean(content.text()).length < 20) { + throw createPluginError(PluginError.EmptyContent, { url }); + } + return (content.html() || '').trim(); + } + + resolveUrl = (path: string): string => + SITE + path.replace(/^\/+|\/+$/g, '') + '/'; +} + +export default new CurspePlugin(); diff --git a/public/static/src/en/curspe/icon.png b/public/static/src/en/curspe/icon.png new file mode 100644 index 0000000000..ffa228934a Binary files /dev/null and b/public/static/src/en/curspe/icon.png differ