Skip to content

Commit

Permalink
Mangatown: handle both chapters types (manga-download#752)
Browse files Browse the repository at this point in the history
* Mangatown: handle both chapters types

* get images urls synchroneously

* revised pages gathering logic

* Update MangaTown.ts

* Update MangaTown.ts
  • Loading branch information
MikeZeDev committed Sep 11, 2024
1 parent 7e8d067 commit 7524692
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
41 changes: 36 additions & 5 deletions web/src/engine/websites/MangaTown.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { Tags } from '../Tags';
import icon from './MangaTown.webp';
import { type Chapter, DecoratableMangaScraper, Page } from '../providers/MangaPlugin';
import { FetchCSS } from '../platform/FetchProvider';
import * as Common from './decorators/Common';
import { Fetch, FetchWindowScript } from '../platform/FetchProvider';
import type { Priority } from '../taskpool/DeferredTask';

@Common.MangaCSS(/^{origin}\/manga\//, 'div.article_content h1.title-top')
//Modded DM5 script
function ImageScript(pageIndex: number): string {
return `
new Promise(async (resolve, reject) => {
try {
const response = await fetch('chapterfun.ashx?cid='+ window.chapter_id + '&page=' + '${pageIndex.toString()}');
eval( await response.text());
resolve(new URL(d[0], window.location.origin).href);
} catch(error) {
reject(error);
}
});
`;
}

@Common.MangaCSS(/^{origin}\/manga\/[^/]+\/$/, 'div.article_content h1.title-top')
@Common.MangasMultiPageCSS('/directory/0-0-0-0-0-0/{page}.htm', 'ul.manga_pic_list li p.title a', 1, 1, 0, Common.AnchorInfoExtractor(true))
@Common.ChaptersSinglePageCSS('ul.chapter_list li a')
@Common.ImageAjaxFromHTML('img#image')
export default class extends DecoratableMangaScraper {

public constructor() {
Expand All @@ -19,7 +34,23 @@ export default class extends DecoratableMangaScraper {
}

public override async FetchPages(chapter: Chapter): Promise<Page[]> {
const data = await FetchCSS<HTMLOptionElement>(new Request(new URL(chapter.Identifier, this.URI)), 'div.manga_read_footer div.page_select select option');
return data.filter(option => !/featured\.html$/.test(option.value)).map(option => new Page(this, chapter, new URL(option.value, this.URI), { Referer: 'mangahere.com' }));
const chapterUrl = new URL(chapter.Identifier, this.URI);
const pageCount = await FetchWindowScript<number>(new Request(chapterUrl), 'window.total_pages');
return new Array(pageCount).fill(0).map((_, index) => new Page(this, chapter, chapterUrl, { pageIndex: index + 1, Referer: 'https://mangahere.com' }));
}

public override async FetchImage(page: Page, priority: Priority, signal: AbortSignal): Promise<Blob> {
const pageUrl = await FetchWindowScript<string>(new Request(page.Link), ImageScript(page.Parameters.pageIndex as number));
return this.imageTaskPool.Add(async () => {
const request = new Request(pageUrl, {
signal: signal,
headers: {
Referer: page.Parameters.Referer
}
});
const response = await Fetch(request);
return response.blob();
}, priority, signal);

}
}
32 changes: 27 additions & 5 deletions web/src/engine/websites/MangaTown_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe } from 'vitest';
import { TestFixture, type Config } from '../../../test/WebsitesFixture';

const config: Config = {
const configMultiPage: Config = {
plugin: {
id: 'mangatown',
title: 'MangaTown',
Expand All @@ -14,8 +14,6 @@ const config: Config = {
child: {
id: '/manga/goblin_slayer/c078/',
title: '78',
//timeout: 20_000,

},
entry: {
index: 0,
Expand All @@ -24,5 +22,29 @@ const config: Config = {
}
};

const fixture = new TestFixture(config);
describe(fixture.Name, async () => (await fixture.Connect()).AssertWebsite());
const fixtureMultiPage = new TestFixture(configMultiPage);
describe(fixtureMultiPage.Name, async () => (await fixtureMultiPage.Connect()).AssertWebsite());

const configSinglePage: Config = {
plugin: {
id: 'mangatown',
title: 'MangaTown',
},
container: {
url: 'https://www.mangatown.com/manga/witch_hunter/',
id: '/manga/witch_hunter/',
title: 'Witch Hunter'
},
child: {
id: '/manga/witch_hunter/v12/c234/',
title: '234',
},
entry: {
index: 0,
size: 205_356,
type: 'image/jpeg'
}
};

const fixtureSinglePage = new TestFixture(configSinglePage);
describe(fixtureSinglePage.Name, async () => (await fixtureSinglePage.Connect()).AssertWebsite());

0 comments on commit 7524692

Please sign in to comment.