Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DisasterScans: website changes #893

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 43 additions & 55 deletions web/src/engine/websites/DisasterScans.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
import { Tags } from '../Tags';
import icon from './DisasterScans.webp';
import { Chapter, DecoratableMangaScraper, Manga, type MangaPlugin } from '../providers/MangaPlugin';
import { Chapter, DecoratableMangaScraper, type Manga, type MangaScraper } from '../providers/MangaPlugin';
import * as Common from './decorators/Common';
import { FetchJSON, FetchWindowScript } from '../platform/FetchProvider';
import { FetchCSS } from '../platform/FetchProvider';

type NEXTDATA = {
buildId: string
type JSONChapter = {
ChapterName: string,
ChapterNumber: string,
chapterID: number
}

type JSONManga = {
pageProps: {
comic: {
id: string,
ComicTitle: string
}
chapters: {
chapterID: number,
ChapterName: string,
chapterNumber: string
}[]
}
function MangaExtractor(element: HTMLMetaElement) {
return element.content.split('- Disaster Scans').at(0).trim();
}

type JSONMangas = {
pageProps: {
comics: {
id: string,
ComicTitle: string
}[]
}
function MangaInfoExtractor(anchor: HTMLAnchorElement) {
return {
id: anchor.pathname,
title: anchor.querySelector<HTMLHeadingElement>('div div h1').textContent.trim()
};
}

const pageScript = `
new Promise( resolve => {
resolve(JSON.parse(__NEXT_DATA__.props.pageProps.chapter.pages).map( page => 'https://f005.backblazeb2.com/b2api/v1/b2_download_file_by_id?fileId='+page));
});
`;
function PageLinkExtractor(this: MangaScraper, image: HTMLImageElement): string {
return new URL(image.getAttribute('src'), this.URI).searchParams.get('url') ?? image.getAttribute('src');
}

@Common.PagesSinglePageJS(pageScript)
@Common.MangaCSS(/^{origin}\/comics\/[^/]+$/, 'meta[property="og:title"]', MangaExtractor)
@Common.MangasSinglePageCSS('/comics', 'div.grid > a[href*="/comics/"]', MangaInfoExtractor)
@Common.PagesSinglePageCSS('section.container div img', PageLinkExtractor)
@Common.ImageAjax()
export default class extends DecoratableMangaScraper {

Expand All @@ -51,34 +41,32 @@ export default class extends DecoratableMangaScraper {
return icon;
}

public override async Initialize(): Promise<void> {
const data = await FetchWindowScript<NEXTDATA>(new Request(this.URI), `__NEXT_DATA__`);
this.nextBuild = data.buildId;
}

public override ValidateMangaURL(url: string): boolean {
return new RegExpSafe(`^${this.URI.origin}/comics/[^/]+`).test(url);
}

public override async FetchManga(provider: MangaPlugin, _url: string): Promise<Manga> {
const slug = _url.split('/').at(-1);
const url = new URL(`/_next/data/${this.nextBuild}/comics/${slug}.json?slug=${slug}`, this.URI);
const data = await FetchJSON<JSONManga>(new Request(url));
return new Manga(this, provider, data.pageProps.comic.id, data.pageProps.comic.ComicTitle.trim());
public override async FetchChapters(manga: Manga): Promise<Chapter[]> {
const scripts = await FetchCSS<HTMLScriptElement>(new Request(new URL(`${manga.Identifier}`, this.URI)), 'script:not([src])');
const chapters = this.ExtractData<JSONChapter[]>(scripts, 'ChapterNumber', 'chapters');
return chapters.map(chapter => {
let title = `Chapter ${chapter.ChapterNumber}`;
title += chapter.ChapterName ? ` - ${chapter.ChapterName}` : '';
return new Chapter(this, manga, `${manga.Identifier}/${chapter.chapterID}-chapter-${chapter.ChapterNumber}`, title);
});
}

public override async FetchMangas(provider: MangaPlugin): Promise<Manga[]> {
const data = await FetchWindowScript<JSONMangas>(new Request(new URL('/comics', this.URI)), `__NEXT_DATA__.props`);
return data.pageProps.comics.map(element => new Manga(this, provider, element.id, element.ComicTitle.trim()));
}
private ExtractData<T>(scripts: HTMLScriptElement[], scriptMatcher: string, keyName: string): T {
const script = scripts.map(script => script.text).find(text => text.includes(scriptMatcher) && text.includes(keyName));
const content = JSON.parse(script.substring(script.indexOf(',"') + 1, script.length - 2)) as string;
let record = JSON.parse(content.substring(content.indexOf(':') + 1)) as JSONObject;
MikeZeDev marked this conversation as resolved.
Show resolved Hide resolved

public override async FetchChapters(manga: Manga): Promise<Chapter[]> {
const slug = `${manga.Identifier}-${manga.Title.toLowerCase().split(' ').join('-')}`;
const url = new URL(`/_next/data/${this.nextBuild}/comics/${slug}.json?slug=${slug}`, this.URI);
const data = await FetchJSON<JSONManga>(new Request(url));
return data.pageProps.chapters.map(chap => {
const title = `Chapter ${chap.chapterNumber}${chap.ChapterName.length != 0 ? ' - ' + chap.ChapterName : ''}`;
return new Chapter(this, manga, `/comics/${slug}/${chap.chapterID}-chapter-${chap.chapterNumber}`, title);
});
return (function FindValueForKeyName(parent: JSONElement): JSONElement {
MikeZeDev marked this conversation as resolved.
Show resolved Hide resolved
if (parent[keyName]) {
return parent[keyName];
}
for (const child of (Object.values(parent) as JSONElement[]).filter(value => value && typeof value === 'object')) {
const result = FindValueForKeyName(child);
if (result) {
return result;
}
}
return undefined;
})(record) as T;
}
}
2 changes: 1 addition & 1 deletion web/src/engine/websites/DisasterScans_e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = {
},
container: {
url: 'https://disasterscans.com/comics/17993-yuan-zun',
id: '17993',
id: '/comics/17993-yuan-zun',
title: 'Yuan Zun'
},
child: {
Expand Down