Skip to content

Commit 0050b0f

Browse files
authored
feat(english): add Taekan source (#2875)
1 parent 1315c17 commit 0050b0f

2 files changed

Lines changed: 223 additions & 0 deletions

File tree

‎plugins/english/taekan.ts‎

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
import { fetchApi } from '@libs/fetch';
2+
import { Plugin } from '@/types/plugin';
3+
import { Filters } from '@libs/filterInputs';
4+
import { NovelStatus } from '@libs/novelStatus';
5+
import { load as loadCheerio } from 'cheerio';
6+
import { defaultCover } from '@libs/defaultCover';
7+
8+
type NovelEntry = { name: string; path: string; status: string };
9+
10+
// Menu pages that are not light-novel series.
11+
const NON_NOVEL_PATHS = new Set([
12+
'/dropped-series',
13+
'/commission-a-translation',
14+
'/megane-no-kanojo-chapters-3-to-8',
15+
]);
16+
// Illustration galleries are image-only pages, and the manga links on the
17+
// Hundred page are image chapters, not text.
18+
const NON_CHAPTER_PATH = /ill+ustration/i;
19+
const NON_CHAPTER_NAME = /^Read Chapter/i;
20+
const NON_SUMMARY_TEXT =
21+
/^(\*|Volume|Description|Synopsis:?$|\(Extracted|Read online|I.ve dropped|I.m afraid|As of|These two links)/i;
22+
const PAGE_SIZE = 20;
23+
24+
class Taekan implements Plugin.PluginBase {
25+
id = 'taekan';
26+
name = 'Taekan';
27+
icon = 'src/en/taekan/icon.png';
28+
site = 'https://taekan.wordpress.com';
29+
version = '1.0.0';
30+
filters: Filters | undefined = undefined;
31+
32+
private async loadPage(url: string) {
33+
const response = await fetchApi(url);
34+
if (!response.ok) {
35+
throw new Error(`Taekan: HTTP ${response.status}.`);
36+
}
37+
return loadCheerio(await response.text());
38+
}
39+
40+
private pathOf(href: string): string | undefined {
41+
try {
42+
const url = new URL(href, this.site);
43+
if (url.hostname !== 'taekan.wordpress.com') return undefined;
44+
return url.pathname.replace(/\/+$/, '') || undefined;
45+
} catch {
46+
return undefined;
47+
}
48+
}
49+
50+
/** The site's series are the pages linked from its top menu. */
51+
private async novelList(): Promise<NovelEntry[]> {
52+
const $ = await this.loadPage(this.site);
53+
const novels: NovelEntry[] = [];
54+
const seen = new Set<string>();
55+
$('#primary-menu a').each((_, el) => {
56+
const anchor = $(el);
57+
const path = this.pathOf(anchor.attr('href') || '');
58+
// Series pages live at a single-segment address, e.g. /dragon-crisis.
59+
if (!path || path.split('/').length !== 2) return;
60+
if (NON_NOVEL_PATHS.has(path) || seen.has(path)) return;
61+
const name = anchor.text().replace(/\s+/g, ' ').trim();
62+
if (!name) return;
63+
seen.add(path);
64+
// The menu nests finished series under "Completed / Dropped Series"
65+
// and abandoned ones under "Dropped Series"; anything outside that
66+
// group is still being translated.
67+
const parents = anchor
68+
.parents('li')
69+
.toArray()
70+
.map(li => this.pathOf($(li).children('a').first().attr('href') || ''));
71+
const dropped = parents.includes('/dropped-series');
72+
const grouped = anchor.parents('ul.sub-menu').length > 0;
73+
novels.push({
74+
name,
75+
path,
76+
status: dropped
77+
? NovelStatus.Cancelled
78+
: grouped
79+
? NovelStatus.Completed
80+
: NovelStatus.Ongoing,
81+
});
82+
});
83+
// Series still being translated come first.
84+
const ongoing = novels.filter(n => n.status === NovelStatus.Ongoing);
85+
return ongoing.concat(novels.filter(n => n.status !== NovelStatus.Ongoing));
86+
}
87+
88+
private coverOf($: ReturnType<typeof loadCheerio>): string {
89+
const src = $('.entry-content img').first().attr('src') || '';
90+
return src.startsWith('http') ? src : defaultCover;
91+
}
92+
93+
private async toNovelItems(
94+
novels: NovelEntry[],
95+
): Promise<Plugin.NovelItem[]> {
96+
return Promise.all(
97+
novels.map(async novel => {
98+
const $ = await this.loadPage(this.resolveUrl(novel.path)).catch(
99+
() => undefined,
100+
);
101+
return {
102+
name: novel.name,
103+
path: novel.path,
104+
cover: $ ? this.coverOf($) : defaultCover,
105+
};
106+
}),
107+
);
108+
}
109+
110+
async popularNovels(pageNo: number): Promise<Plugin.NovelItem[]> {
111+
const novels = (await this.novelList()).slice(
112+
(pageNo - 1) * PAGE_SIZE,
113+
pageNo * PAGE_SIZE,
114+
);
115+
return this.toNovelItems(novels);
116+
}
117+
118+
async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
119+
const [$, entries] = await Promise.all([
120+
this.loadPage(this.resolveUrl(novelPath)),
121+
this.novelList(),
122+
]);
123+
const entry = entries.find(item => item.path === novelPath);
124+
const content = $('.entry-content').first();
125+
content.find('script,style,noscript,.sharedaddy,#jp-post-flair').remove();
126+
127+
// Chapters are single-segment pages linked from the series page, in
128+
// reading order. Links to other hosts (Google Drive, Baka-Tsuki, ...)
129+
// and download-only entries are skipped.
130+
const chapters: Plugin.ChapterItem[] = [];
131+
const seen = new Set<string>();
132+
content.find('a').each((_, el) => {
133+
const anchor = $(el);
134+
const path = this.pathOf(anchor.attr('href') || '');
135+
const name = anchor.text().replace(/\s+/g, ' ').trim();
136+
if (!path || !name || seen.has(path)) return;
137+
if (!/^\/[^/]+$/.test(path) || path === novelPath) return;
138+
if (NON_CHAPTER_PATH.test(path) || NON_CHAPTER_NAME.test(name)) return;
139+
if (NON_NOVEL_PATHS.has(path)) return;
140+
if (entries.some(item => item.path === path)) return;
141+
seen.add(path);
142+
chapters.push({ name, path });
143+
});
144+
145+
const novel: Plugin.SourceNovel = {
146+
path: novelPath,
147+
name:
148+
entry?.name ||
149+
$('.entry-title').first().text().replace(/\s+/g, ' ').trim(),
150+
cover: this.coverOf($),
151+
chapters,
152+
status: entry?.status || NovelStatus.Unknown,
153+
};
154+
155+
// The synopsis is the run of plain paragraphs before the first chapter
156+
// link.
157+
const summary: string[] = [];
158+
let reachedChapters = false;
159+
content.find('p').each((_, el) => {
160+
const paragraph = $(el);
161+
if (reachedChapters) return;
162+
const linksChapter = paragraph
163+
.find('a[href]')
164+
.toArray()
165+
.some(a => seen.has(this.pathOf($(a).attr('href') || '') || ''));
166+
if (linksChapter) {
167+
reachedChapters = true;
168+
return;
169+
}
170+
if (paragraph.find('a[href]').length) return;
171+
const text = paragraph.text().replace(/\s+/g, ' ').trim();
172+
if (text.length < 40 || NON_SUMMARY_TEXT.test(text)) return;
173+
summary.push(text);
174+
});
175+
novel.summary = summary.join('\n\n') || undefined;
176+
177+
return novel;
178+
}
179+
180+
async parseChapter(chapterPath: string): Promise<string> {
181+
const $ = await this.loadPage(this.resolveUrl(chapterPath));
182+
const content = $('.entry-content').first();
183+
content
184+
.find(
185+
'script,style,iframe,noscript,form,.sharedaddy,#jp-post-flair,[id^="more-"]',
186+
)
187+
.remove();
188+
content.find('p').each((_, el) => {
189+
const paragraph = $(el);
190+
if (!paragraph.text().trim() && !paragraph.find('img').length)
191+
paragraph.remove();
192+
});
193+
const chapter = content.html() || '';
194+
if (!content.find('img').length && content.text().trim().length < 200)
195+
throw new Error('Taekan: no readable chapter text found.');
196+
return chapter;
197+
}
198+
199+
async searchNovels(
200+
searchTerm: string,
201+
pageNo: number,
202+
): Promise<Plugin.NovelItem[]> {
203+
const terms = searchTerm.toLowerCase().split(/\s+/).filter(Boolean);
204+
if (!terms.length) return [];
205+
// The site's own search returns chapter posts, so match series titles
206+
// against the menu instead.
207+
const novels = (await this.novelList()).filter(novel =>
208+
terms.every(term => novel.name.toLowerCase().includes(term)),
209+
);
210+
return this.toNovelItems(
211+
novels.slice((pageNo - 1) * PAGE_SIZE, pageNo * PAGE_SIZE),
212+
);
213+
}
214+
215+
resolveUrl(path: string): string {
216+
const url = new URL(path, this.site);
217+
if (url.hostname !== 'taekan.wordpress.com')
218+
throw new Error('Taekan: invalid page address.');
219+
return `${this.site}${url.pathname}${url.search}`;
220+
}
221+
}
222+
223+
export default new Taekan();
18.6 KB
Loading

0 commit comments

Comments
 (0)