Skip to content

Commit 068d71c

Browse files
authored
fix: Novel Fire Chapter Parsing (#2131)
* fix: convert page mode to switch * fix: remove unicode spacing from chapter names
1 parent e97519e commit 068d71c

1 file changed

Lines changed: 39 additions & 46 deletions

File tree

plugins/english/novelfire.ts

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { storage } from '@libs/storage';
99
class NovelFire implements Plugin.PluginBase {
1010
id = 'novelfire';
1111
name = 'Novel Fire';
12-
version = '1.3.1';
12+
version = '1.4.1';
1313
icon = 'src/en/novelfire/icon.png';
1414
site = 'https://novelfire.net/';
1515
webStorageUtilized = true;
@@ -18,15 +18,9 @@ class NovelFire implements Plugin.PluginBase {
1818

1919
pluginSettings = {
2020
pageLength: {
21-
value: '-1',
21+
value: '',
2222
label: 'Page Mode (Change if Broken)',
23-
type: 'Select',
24-
options: [
25-
{ label: '100 (Fallback Mode)', value: '100' },
26-
{ label: '200', value: '200' },
27-
{ label: '500', value: '500' },
28-
{ label: 'All', value: '-1' },
29-
],
23+
type: 'Switch',
3024
},
3125
singlePage: {
3226
value: '',
@@ -36,7 +30,7 @@ class NovelFire implements Plugin.PluginBase {
3630
},
3731
};
3832
singlePage = storage.get('singlePage');
39-
pageLength = storage.get('pageLength') ?? '-1';
33+
pageLength = storage.get('pageLength');
4034

4135
async getCheerio(url: string, search: boolean): Promise<CheerioAPI> {
4236
const r = await fetchApi(url);
@@ -139,7 +133,7 @@ class NovelFire implements Plugin.PluginBase {
139133
post_id: string,
140134
page: string,
141135
): Promise<Plugin.ChapterItem[]> {
142-
const length = parseInt(this.pageLength) || -1;
136+
const length = this.pageLength ? 100 : -1;
143137
const url = `${this.site}ajax/listChapterDataAjax`;
144138
const start = length === -1 ? 0 : (parseInt(page) - 1) * length;
145139
this.draw++;
@@ -176,38 +170,34 @@ class NovelFire implements Plugin.PluginBase {
176170
if (result.status === 429) throw new NovelFireThrottlingError();
177171

178172
const body = await result.text();
179-
180-
if (body.includes('You are being rate limited')) {
173+
if (body.includes('You are being rate limited'))
181174
throw new NovelFireThrottlingError();
182-
}
183-
184-
if (body.includes('Page Not Found 404')) {
185-
throw new NovelFireAjaxNotFound();
186-
}
187-
188-
const json = JSON.parse(body);
189-
const chapters = (json.data || [])
190-
.map((index: { title?: string; slug: string; n_sort: number }) => {
191-
const chapterName = load(index.title || index.slug).text();
192-
const chapterPath = `${novelPath}/chapter-${index.n_sort}`;
193-
const sortNumber = index.n_sort;
194-
195-
if (!chapterPath) return null;
196-
197-
return {
198-
name: chapterName,
199-
path: chapterPath,
200-
chapterNumber: Number(sortNumber),
201-
};
202-
})
203-
.filter(
204-
(chapter: Plugin.ChapterItem | null) => chapter !== null,
205-
) as Plugin.ChapterItem[];
206-
const sortedChapters = chapters.sort(function (a, b) {
207-
return (a.chapterNumber || 0) - (b.chapterNumber || 0);
208-
});
209-
210-
return sortedChapters;
175+
if (body.includes('Page Not Found 404')) throw new NovelFireAjaxNotFound();
176+
177+
return (JSON.parse(body).data || [])
178+
.flatMap(
179+
(idx: { title?: string; slug: string; n_sort: string | number }) => {
180+
const name = load(idx.title || idx.slug)
181+
.text()
182+
.replace(/[\u200B-\u200D\uFEFF]/g, '')
183+
.trim();
184+
const num = Number(idx.n_sort);
185+
186+
return name && !isNaN(num)
187+
? [
188+
{
189+
name,
190+
path: `${novelPath}/chapter-${num}`,
191+
chapterNumber: num,
192+
},
193+
]
194+
: [];
195+
},
196+
)
197+
.sort(
198+
(a: Plugin.ChapterItem, b: Plugin.ChapterItem) =>
199+
(a.chapterNumber || 0) - (b.chapterNumber || 0),
200+
);
211201
}
212202

213203
async getAllChaptersForce(
@@ -282,7 +272,7 @@ class NovelFire implements Plugin.PluginBase {
282272

283273
const post_id = $('#novel-report').attr('report-post_id');
284274
if (post_id) {
285-
storage.set(`novelfire_postid_${novelPath}`, post_id);
275+
storage.set(`${this.id}_${novelPath.split('/').pop()}`, post_id);
286276
}
287277

288278
const novel: Partial<Plugin.SourceNovel & { totalPages: number }> = {
@@ -344,9 +334,12 @@ class NovelFire implements Plugin.PluginBase {
344334
.parent()
345335
.text()
346336
.trim();
347-
const length = parseInt(this.pageLength) || -1;
337+
const length = this.pageLength ? 100 : -1;
348338
novel.totalPages =
349339
length === -1 ? 1 : Math.ceil(parseInt(totalChapters) / length) || 1;
340+
if (novel.totalPages === 1 && post_id) {
341+
novel.chapters = await this.getAllChapters(novelPath, post_id, '1');
342+
}
350343
if (length === 100 && this.singlePage) {
351344
novel.chapters = await this.getAllChaptersForce(
352345
novel.path as string,
@@ -359,7 +352,7 @@ class NovelFire implements Plugin.PluginBase {
359352
}
360353

361354
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
362-
const post_id = storage.get(`novelfire_postid_${novelPath}`);
355+
const post_id = storage.get(`${this.id}_${novelPath.split('/').pop()}`);
363356

364357
if (post_id && !isNaN(Number(post_id))) {
365358
try {
@@ -371,7 +364,7 @@ class NovelFire implements Plugin.PluginBase {
371364
}
372365

373366
// Fallback only works for multiples of 100
374-
const length = parseInt(this.pageLength) || -1;
367+
const length = this.pageLength ? 100 : -1;
375368
if (length === 100) {
376369
const url = `${this.site}${novelPath}/chapters?page=${page}`;
377370
const result = await fetchApi(url);

0 commit comments

Comments
 (0)