diff --git a/plugins/english/thesuniscold.ts b/plugins/english/thesuniscold.ts new file mode 100644 index 0000000000..853c13317d --- /dev/null +++ b/plugins/english/thesuniscold.ts @@ -0,0 +1,214 @@ +import { fetchApi } from '@libs/fetch'; +import { Filters, FilterTypes } from '@libs/filterInputs'; +import { + checkResponse, + createPluginError, + PluginError, +} from '@libs/pluginError'; +import { Plugin } from '@/types/plugin'; +import { load as parseHTML } from 'cheerio'; + +/** + * thesuniscold.translatednovels.com is a WordPress translation blog with four + * novels. Each novel is a page listing its chapters as links; chapters are + * pages or posts. The site lists its novels only in its menu, so they are + * listed here; everything else comes from the public REST API. + */ +const NOVELS: Plugin.NovelItem[] = [ + { + name: '108 Maidens of Destiny', + path: '108-maidens-of-destiny', + cover: + 'https://i0.wp.com/thesuniscold.translatednovels.com/wp-content/uploads/2017/01/108-bigger-212x300.png?resize=212%2C300', + }, + { name: 'Maiden Galaxy', path: 'maiden-galaxy' }, + { + name: 'Investiture of the Star Generals', + path: 'investiture-of-the-star-generals', + cover: + 'https://i0.wp.com/thesuniscold.translatednovels.com/wp-content/uploads/2025/02/92075s.jpg?resize=187%2C272&ssl=1', + }, + { + name: 'Back to the Apocalypse', + path: 'projects/back-to-the-apocalypse', + cover: + 'https://i0.wp.com/thesuniscold.translatednovels.com/wp-content/uploads/2017/02/baijing-264x300.jpg?resize=264%2C300', + }, +]; + +type WpItem = { + title: { rendered: string }; + content: { rendered: string }; +}; + +const clean = (s: unknown): string => + String(s ?? '') + .replace(/\s+/g, ' ') + .trim(); + +/** + * A chapter number, only when "chapter" is near the digits, so a side story + * such as "Extra 3: ..." is not taken for chapter 3. + */ +const chapterNumber = (name: string, path: string): number | null => { + const m = + name.match(/(?:chapter|chap|ch)[^0-9]{0,3}(\d+(?:\.\d+)?)/i) || + path.match(/chapter[-_]?(\d+(?:\.\d+)?)/i); + if (m) return parseFloat(m[1]); + if (/chapter/i.test(name + ' ' + path)) { + const m2 = (name + ' ' + path).match(/(\d+(?:\.\d+)?)/); + if (m2) return parseFloat(m2[1]); + } + return null; +}; + +class TheSunIsColdPlugin implements Plugin.PluginBase { + id = 'thesuniscold'; + name = 'The Sun Is Cold'; + icon = 'src/en/thesuniscold/icon.png'; + site = 'https://thesuniscold.translatednovels.com/'; + version = '1.0.0'; + + filters = { + sort: { + type: FilterTypes.Picker, + label: 'Sort', + value: 'site', + options: [ + { label: 'Site order', value: 'site' }, + { label: 'Title A-Z', value: 'asc' }, + { label: 'Title Z-A', value: 'desc' }, + ], + }, + } satisfies Filters; + + private async api(path: string): Promise { + const url = this.site + 'wp-json/wp/v2/' + path; + const r = await fetchApi(url); + checkResponse(r); + try { + return (await r.json()) as WpItem[]; + } catch { + throw createPluginError(PluginError.InvalidResponse, { url }); + } + } + + private toPath(url: string | undefined): string | null { + if (!url) return null; + const host = this.site.replace(/^https?:\/\//, ''); + const u = url.replace(/^https?:\/\//, ''); + return u.startsWith(host) + ? u.slice(host.length).replace(/(^\/+|\/+$)/g, '') + : null; + } + + async popularNovels( + pageNo: number, + { filters }: Plugin.PopularNovelsOptions, + ): Promise { + if (pageNo !== 1) return []; + const list = NOVELS.slice(); + const sort = filters?.sort?.value || 'site'; + if (sort !== 'site') { + list.sort((a, b) => a.name.localeCompare(b.name)); + if (sort === 'desc') list.reverse(); + } + return list; + } + + async searchNovels( + searchTerm: string, + pageNo: number, + ): Promise { + if (pageNo !== 1) return []; + const q = searchTerm.toLowerCase(); + return NOVELS.filter(n => n.name.toLowerCase().includes(q)); + } + + async parseNovel(novelPath: string): Promise { + const slug = novelPath.replace(/\/+$/, '').split('/').pop() || ''; + const pages = await this.api('pages?slug=' + encodeURIComponent(slug)); + if (pages.length !== 1) { + throw createPluginError(PluginError.NovelNotFound, { + url: this.site + novelPath, + }); + } + const $ = parseHTML(pages[0].content.rendered); + const found: Plugin.ChapterItem[] = []; + const seen: Record = {}; + $('a').each((_, el) => { + const path = this.toPath($(el).attr('href')); + if (!path || seen[path]) return; + if (!path.startsWith(novelPath + '/') && !/^archives\//.test(path)) + return; + const name = clean($(el).text()); + if (!name) return; + seen[path] = true; + found.push({ name, path }); + }); + // The index pages mix a "latest" list with the full archive, so page + // order is not reading order: sort by chapter number when most chapters + // have one; the others go last, in page order. + const numbers = found.map(c => chapterNumber(c.name, c.path)); + let chapters = found; + if ( + found.length > 3 && + numbers.filter(n => n !== null).length >= found.length * 0.6 + ) { + chapters = found + .map((c, i) => ({ c, n: numbers[i] ?? Infinity, i })) + .sort((a, b) => a.n - b.n || a.i - b.i) + .map(x => ({ ...x.c, chapterNumber: numbers[x.i] ?? undefined })); + } + + const novel: Plugin.SourceNovel = { + path: novelPath, + name: clean(parseHTML(pages[0].title.rendered).root().text()), + chapters, + }; + const img = $('img').first(); + const cover = img.attr('data-lazy-src') || img.attr('src'); + if (cover) novel.cover = cover; + const summary = $('p') + .filter( + (_, p) => !$(p).find('a').length && clean($(p).text()).length > 40, + ) + .first() + .text() + .trim(); + if (summary) novel.summary = summary; + return novel; + } + + async parseChapter(chapterPath: string): Promise { + const archive = chapterPath.match(/^archives\/(\d+)$/); + let item: WpItem | undefined; + if (archive) { + const url = this.site + 'wp-json/wp/v2/posts/' + archive[1]; + const r = await fetchApi(url); + checkResponse(r, { notFoundCode: PluginError.ChapterNotFound }); + item = (await r.json()) as WpItem; + } else { + const slug = chapterPath.replace(/\/+$/, '').split('/').pop() || ''; + for (const kind of ['pages', 'posts']) { + const found = await this.api( + kind + '?slug=' + encodeURIComponent(slug), + ); + if (found.length === 1) { + item = found[0]; + break; + } + } + } + if (!item?.content?.rendered) { + throw createPluginError(PluginError.ChapterNotFound, { + url: this.site + chapterPath, + }); + } + return item.content.rendered; + } + + resolveUrl = (path: string): string => this.site + path; +} + +export default new TheSunIsColdPlugin(); diff --git a/public/static/src/en/thesuniscold/icon.png b/public/static/src/en/thesuniscold/icon.png new file mode 100644 index 0000000000..920e22b385 Binary files /dev/null and b/public/static/src/en/thesuniscold/icon.png differ