diff --git a/plugins/english/dobytranslations.ts b/plugins/english/dobytranslations.ts new file mode 100644 index 0000000000..a828c6eb58 --- /dev/null +++ b/plugins/english/dobytranslations.ts @@ -0,0 +1,252 @@ +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 { Plugin } from '@/types/plugin'; +import { CheerioAPI, load as parseHTML } from 'cheerio'; + +/** + * dobytranslations.com runs the LightNovel WordPress theme, customised: a + * novel page's chapter list (`div.eplister`) holds several lists ("First + * Chapter", "Last Released Chapter", then the full list), which the + * lightnovelwp generator's parser stops reading after the first. Paid + * chapters are not listed on the site. + */ +class DobyTranslationsPlugin implements Plugin.PluginBase { + id = 'dobytranslations'; + name = 'Doby Translations'; + icon = 'src/en/dobytranslations/icon.png'; + site = 'https://dobytranslations.com/'; + version = '1.0.0'; + + filters = { + 'genre[]': { + type: FilterTypes.CheckboxGroup, + label: 'Genre', + value: [], + options: [ + { label: 'Antihero Protagonist', value: 'antihero-protagonist' }, + { label: 'Comedy', value: 'comedy' }, + { label: 'Completely Free', value: 'completely-free' }, + { label: 'Cthulhu', value: 'cthulhu' }, + { label: 'Drama', value: 'drama' }, + { label: 'Ecchi', value: 'ecchi' }, + { label: 'Fantasy', value: 'fantasy' }, + { label: 'Farming', value: 'farming' }, + { label: 'Female Protagonist', value: 'female-prota' }, + { label: 'Gender Bender', value: 'gender-bender' }, + { label: 'Harem', value: 'harem' }, + { label: 'Horror', value: 'horror' }, + { label: 'Male Protagonist', value: 'male-protagonist' }, + { label: 'Male to Female', value: 'male-to-female' }, + { label: 'No Romance', value: 'no-romance' }, + { label: 'Psychological', value: 'psychological' }, + { label: 'Revenge', value: 'revenge' }, + { label: 'Romance', value: 'romance' }, + { label: 'Slice of Life', value: 'slice-of-life' }, + { label: 'System', value: 'system' }, + { label: 'Villain Protagonist', value: 'villain-protagonist' }, + { label: 'Yuri', value: 'yuri' }, + ], + }, + 'type[]': { + type: FilterTypes.CheckboxGroup, + label: 'Type', + value: [], + options: [{ label: 'Web Novel', value: 'web-novel' }], + }, + 'status': { + type: FilterTypes.Picker, + label: 'Status', + value: '', + options: [ + { label: 'All', value: '' }, + { label: 'Ongoing', value: 'ongoing' }, + { label: 'Hiatus', value: 'hiatus' }, + { label: 'Completed', value: 'completed' }, + ], + }, + 'order': { + type: FilterTypes.Picker, + label: 'Order by', + value: 'popular', + options: [ + { label: 'Default', value: '' }, + { label: 'A-Z', value: 'title' }, + { label: 'Z-A', value: 'titlereverse' }, + { label: 'Latest Update', value: 'update' }, + { label: 'Latest Added', value: 'latest' }, + { label: 'Popular', value: 'popular' }, + { label: 'Rating', value: 'rating' }, + ], + }, + } satisfies Filters; + + private async fetchPage(path: string): Promise { + const url = this.site + path; + const r = await fetchApi(url); + checkResponse(r); + const html = await r.text(); + assertNoChallenge(html, url, r.url); + return parseHTML(html); + } + + /** A site URL as a path without leading slash, ending in one; else null. */ + private toPath(url: string | undefined): string | null { + if (!url) return null; + const host = this.site.replace(/^https?:\/\//, ''); + const u = url.replace(/^https?:\/\//, ''); + if (!u.startsWith(host)) return null; + return u.slice(host.length).replace(/(^\/+|\/+$)/g, '') + '/'; + } + + private parseList($: CheerioAPI): Plugin.NovelItem[] { + const novels: Plugin.NovelItem[] = []; + $('article.maindet, article.bs, .listupd article').each((_, el) => { + const a = $(el).find('a').first(); + const path = this.toPath(a.attr('href')); + if (!path) return; + const img = $(el).find('img').first(); + const name = ( + a.attr('title') || $(el).find('.tt, .ntitle, h2').first().text() + ).trim(); + const cover = img.attr('data-src') || img.attr('src'); + novels.push(cover ? { name, path, cover } : { name, path }); + }); + return novels; + } + + async popularNovels( + pageNo: number, + { + showLatestNovels, + filters, + }: Plugin.PopularNovelsOptions, + ): Promise { + // "Default" is the empty value: no order parameter, the site's default. + const order = showLatestNovels + ? 'latest' + : filters?.order?.value ?? 'popular'; + let query = 'series/?page=' + pageNo; + if (order) query += '&order=' + order; + for (const key of ['genre[]', 'type[]'] as const) { + for (const v of filters?.[key]?.value || []) { + query += '&' + key + '=' + encodeURIComponent(v); + } + } + if (filters?.status?.value) { + query += '&status=' + encodeURIComponent(filters.status.value); + } + return this.parseList(await this.fetchPage(query)); + } + + async searchNovels( + searchTerm: string, + pageNo: number, + ): Promise { + const path = 'page/' + pageNo + '/?s=' + encodeURIComponent(searchTerm); + const r = await fetchApi(this.site + path); + if (r.status === 404 && pageNo > 1) return []; // past the last page + checkResponse(r, { search: true }); + const html = await r.text(); + assertNoChallenge(html, this.site + path, r.url); + return this.parseList(parseHTML(html)); + } + + async parseNovel(novelPath: string): Promise { + const $ = await this.fetchPage(novelPath); + const name = $('h1.entry-title').first().text().trim(); + if (!name) { + throw createPluginError(PluginError.NovelNotFound, { + url: this.site + novelPath, + }); + } + const novel: Plugin.SourceNovel = { path: novelPath, name }; + const img = $('.sertothumb img, .thumbook img, .thumb img').first(); + const cover = img.attr('data-src') || img.attr('src'); + if (cover) novel.cover = cover; + const summary = $( + '.entry-content[itemprop="description"], .sersys, .synp .entry-content', + ) + .first() + .text() + .trim(); + if (summary) novel.summary = summary; + const genres = $('.sertogenre a, .genxed a') + .map((_, el) => $(el).text().trim()) + .get() + .filter(Boolean); + if (genres.length) novel.genres = genres.join(', '); + switch ($('.sertostat span').first().text().trim().toLowerCase()) { + case 'ongoing': + novel.status = NovelStatus.Ongoing; + break; + case 'completed': + novel.status = NovelStatus.Completed; + break; + case 'hiatus': + novel.status = NovelStatus.OnHiatus; + break; + } + + // Every list in the block, each chapter once (the "First Chapter" and + // "Last Released Chapter" entries repeat ones of the full list). + // Chapter titles repeat the novel's name, with or without a leading tag + // such as "[GL]" (the site added it to the name partway through). + const escape = (t: string) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + const bare = name.replace(/^\[[^\]]*\]\s*/, ''); + const titlePrefix = new RegExp( + '^(?:' + escape(name) + '|' + escape(bare) + ')\\s*', + ); + const seen: Record = {}; + const chapters: Plugin.ChapterItem[] = []; + $('.eplister li a').each((_, el) => { + const path = this.toPath($(el).attr('href')); + if (!path || seen[path]) return; + seen[path] = true; + const num = $(el).find('.epl-num').text().trim(); + const title = $(el).find('.epl-title').text().trim() || $(el).text(); + const rest = title.replace(titlePrefix, '').trim(); + const number = parseFloat( + (num.match(/\d+(?:\.\d+)?/) || rest.match(/\d+(?:\.\d+)?/) || [])[0] || + '', + ); + chapters.push({ + name: /^\d+(?:\.\d+)?$/.test(rest) ? 'Chapter ' + rest : rest || num, + path, + releaseTime: $(el).find('.epl-date').text().trim() || undefined, + chapterNumber: isNaN(number) ? undefined : number, + }); + }); + // Oldest first: the site lists newest first; then by number when every + // chapter has one (a stable sort, so parts of one chapter keep their + // order). + chapters.reverse(); + if (chapters.every(c => c.chapterNumber !== undefined)) { + chapters.sort((a, b) => (a.chapterNumber || 0) - (b.chapterNumber || 0)); + } + novel.chapters = chapters; + return novel; + } + + async parseChapter(chapterPath: string): Promise { + const $ = await this.fetchPage(chapterPath); + $('script, noscript, ins, .adsbygoogle').remove(); + const text = $('.epcontent, .entry-content').first(); + if (!text.length || !text.text().trim()) { + throw createPluginError(PluginError.EmptyContent, { + url: this.site + chapterPath, + }); + } + return text.html() || ''; + } + + resolveUrl = (path: string): string => this.site + path; +} + +export default new DobyTranslationsPlugin(); diff --git a/public/static/src/en/dobytranslations/icon.png b/public/static/src/en/dobytranslations/icon.png new file mode 100644 index 0000000000..cfdd66da84 Binary files /dev/null and b/public/static/src/en/dobytranslations/icon.png differ