Skip to content

Commit 86e32dc

Browse files
committed
more page items for parseLastAddedSection + getSection() restyle
1 parent 1e67be5 commit 86e32dc

File tree

3 files changed

+91
-89
lines changed

3 files changed

+91
-89
lines changed

src/generic/main.ts

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type Chapter,
77
type ChapterDetails,
88
type ChapterProviding,
9-
type ChapterUpdatesCarouselItem,
109
type DiscoverSection,
1110
type DiscoverSectionItem,
1211
type DiscoverSectionProviding,
@@ -299,72 +298,49 @@ export abstract class MangaWorldGeneric
299298
items: [],
300299
metadata: metadata,
301300
};
302-
const chapterUpdate: ChapterUpdatesCarouselItem[] = [];
303-
for (const item of json) {
304-
switch (item.kind) {
305-
case "trending":
306-
if (id == "popular_section")
307-
section = this.parser.parseTrendingChapters(
308-
metadata,
309-
this,
310-
item.data.mostViewedChapters,
311-
);
301+
const parsers: Record<
302+
string,
303+
() => Promise<{
304+
items: DiscoverSectionItem[];
305+
metadata: MangaMetadata;
306+
}>
307+
> = {
308+
updated_section: () =>
309+
this.parser.parseChapterUpdateSection(metadata, this),
310+
most_read_section: () =>
311+
this.parser.parseMostReadSection(metadata, this),
312+
new_manga_section: () =>
313+
this.parser.parseLastAddedSection(metadata, this, false),
314+
new_fav_type_section: () =>
315+
this.parser.parseLastAddedSection(metadata, this, true),
316+
genre_section: () => this.parser.parseGenreSection(this, metadata),
317+
type_section: () => this.parser.parseTypeSection(this, metadata),
318+
};
319+
if (id === "popular_section" || id === "mese_section") {
320+
for (const item of json) {
321+
if (id === "popular_section" && item.kind === "trending") {
322+
section = this.parser.parseTrendingChapters(
323+
metadata,
324+
this,
325+
item.data.mostViewedChapters,
326+
);
312327
break;
313-
case "global":
314-
if (id == "mese_section")
315-
section = this.parser.parseMonthTrending(
316-
metadata,
317-
this,
318-
item.data.globalData.topMangas,
319-
);
320-
break;
321-
case "manga":
322-
if (id == "updated_section") {
323-
const updated_section =
324-
await this.parser.parseLastAddedSection(
325-
metadata,
326-
this,
327-
item.data,
328-
);
329-
if (updated_section)
330-
chapterUpdate.push(updated_section);
331-
}
328+
}
329+
330+
if (id === "mese_section" && item.kind === "global") {
331+
section = this.parser.parseMonthTrending(
332+
metadata,
333+
this,
334+
item.data.globalData.topMangas,
335+
);
332336
break;
337+
}
333338
}
334339
}
335-
if (chapterUpdate.length > 0) {
336-
section = { items: chapterUpdate, metadata: metadata };
337-
}
338-
if (section.items.length > 1) {
339-
return section;
340-
}
341-
switch (id) {
342-
case "most_read_section": {
343-
return this.parser.parseMostReadSection(metadata, this);
344-
}
345-
case "new_manga_section": {
346-
return this.parser.parseLastMangaAddedSection(
347-
metadata,
348-
this,
349-
false,
350-
);
351-
}
352-
case "new_fav_type_section": {
353-
return this.parser.parseLastMangaAddedSection(
354-
metadata,
355-
this,
356-
true,
357-
);
358-
}
359-
case "genre_section": {
360-
return this.parser.parseGenreSection(this, metadata);
361-
}
362-
case "type_section": {
363-
return this.parser.parseTypeSection(this, metadata);
364-
}
365-
default:
366-
return { items: [], metadata: metadata };
367-
}
340+
if (section.items.length > 1) return section;
341+
const sectionParser = parsers[id];
342+
if (sectionParser) return await sectionParser();
343+
return section;
368344
}
369345

370346
async getDiscoverSectionItems(

src/generic/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface Manga {
5656
statusT: string;
5757
tramaT: string;
5858
createdAt: string;
59+
updatedAt: string;
5960
}
6061

6162
export interface MangaChapterList {

src/generic/parsers.ts

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import {
99
type SourceManga,
1010
type TagSection,
1111
} from "@paperback/types";
12-
import { filter, jsonParser, MangaWorldGeneric, tags, types } from "./main";
12+
import {
13+
cache,
14+
filter,
15+
jsonParser,
16+
MangaWorldGeneric,
17+
tags,
18+
types,
19+
} from "./main";
1320
import type {
1421
Manga,
1522
MangaChapterList,
1623
MangaMetadata,
17-
MangaPageData,
1824
TrendingManga,
1925
WindowEntry,
2026
} from "./models";
@@ -348,7 +354,7 @@ export class Parsers {
348354
* Parsing most read
349355
* @param {MangaMetadata} metadata - metadata
350356
* @param source
351-
* @return {{ items: DiscoverSectionItem[], metadata: Metadata }}
357+
* @return {{ items: DiscoverSectionItem[], metadata: MangaMetadata }}
352358
*/
353359
async parseMostReadSection(
354360
metadata: MangaMetadata,
@@ -362,7 +368,7 @@ export class Parsers {
362368
return { items: latest, metadata: { page: page } };
363369
}
364370

365-
async parseLastMangaAddedSection(
371+
async parseLastAddedSection(
366372
metadata: MangaMetadata,
367373
source: MangaWorldGeneric,
368374
favTags: boolean,
@@ -412,31 +418,50 @@ export class Parsers {
412418
* Parse new chapters
413419
* @param {MangaMetadata} metadata - manga metadata
414420
* @param source
415-
* @param manga
416-
* @return {{
417-
* items: DiscoverSectionItem[],
418-
* metadata: MangaMetadata | undefined
419-
* }}
421+
* @return {Promise<{ items: DiscoverSectionItem[]; metadata: MangaMetadata }> }
420422
*/
421423

422-
async parseLastAddedSection(
424+
async parseChapterUpdateSection(
423425
metadata: MangaMetadata,
424426
source: MangaWorldGeneric,
425-
manga: MangaPageData,
426-
): Promise<ChapterUpdatesCarouselItem | undefined> {
427-
return {
428-
chapterId: "items.manga",
429-
metadata: metadata,
430-
type: "chapterUpdatesCarouselItem",
431-
publishDate: new Date(manga.manga.createdAt),
432-
contentRating:
433-
source.defaultContentRating === ContentRating.ADULT
434-
? ContentRating.ADULT
435-
: source.defaultContentRating,
436-
imageUrl: manga.manga.imageT ?? manga.manga.image,
437-
mangaId: manga.manga.linkId + "/" + manga.manga.slug,
438-
title: manga.manga.title ?? "",
439-
subtitle: manga.chapters[0]?.name ?? "",
440-
};
427+
): Promise<{ items: DiscoverSectionItem[]; metadata: MangaMetadata }> {
428+
const page = metadata?.page ?? 1;
429+
let html = "";
430+
const updates: ChapterUpdatesCarouselItem[] = [];
431+
if (page == 1) {
432+
html = Application.arrayBufferToUTF8String(
433+
await cache.getPageCache("home", source.base_url),
434+
);
435+
} else {
436+
const data = (
437+
await Application.scheduleRequest({
438+
url: `${source.base_url}/?page=${page}`,
439+
method: "GET",
440+
})
441+
)[1];
442+
html = Application.arrayBufferToUTF8String(data);
443+
}
444+
const windowEntry = jsonParser.getWindowEntry(html);
445+
for (const { kind, data } of windowEntry) {
446+
if (kind !== "manga") continue;
447+
const { manga, chapters } = data;
448+
const firstChapter = chapters?.[0];
449+
if (firstChapter) {
450+
updates.push({
451+
chapterId: firstChapter.id ?? "",
452+
type: "chapterUpdatesCarouselItem",
453+
publishDate: new Date(firstChapter.createdAt),
454+
contentRating:
455+
source.defaultContentRating === ContentRating.ADULT
456+
? ContentRating.ADULT
457+
: source.defaultContentRating,
458+
imageUrl: manga.imageT ?? manga.image,
459+
mangaId: `${manga.linkId}/${manga.slug}`,
460+
title: manga.title ?? "",
461+
subtitle: firstChapter.name ?? "",
462+
});
463+
}
464+
}
465+
return { items: updates, metadata: { page: page + 1 } };
441466
}
442467
}

0 commit comments

Comments
 (0)