1.0.2 • Published 2 years ago
@media-fetch-project/mangadex-source v1.0.2
MediaFetchProject Mangadex-Source
Mangadex-Source is a manga/manhwa/manhua fetcher based on the MediaFetchProject.
The objective is to use the unique structure of the DevLib to ensure an easy implementation with other fetchers (ex: to make an app that looks on many different websites for your result).
Usage Example
import { AbstractImage } from '@media-fetch-project/dev-lib/dist/Media';
import Mangadex from '@media-fetch-project/mangadex-source';
import Chapter from '@media-fetch-project/mangadex-source/dist/Chapter';
import Manga from '@media-fetch-project/mangadex-source/dist/Manga';
import { createWriteStream, existsSync, mkdirSync } from 'fs';
import path from 'path';
import { Readable } from 'stream';
const folderPath = path.normalize('./dl/Mangadex/');
const mangaToSearch = 'Kaguya-sama';
if (!existsSync(folderPath)) mkdirSync(folderPath, { recursive: true });
async function downloadPage(
destination: string,
page: AbstractImage,
pageNumber: number
): Promise<void> {
if (!existsSync(destination)) mkdirSync(destination, { recursive: true });
console.log(`\t\t\tdownloading page ${pageNumber}`);
const writableStream = createWriteStream(
path.normalize(
path.join(destination, `${pageNumber}.${await page.getType()}`)
),
{ autoClose: true }
);
Readable.from(await page.fetch())
.pipe(writableStream)
.once('finish', () =>
console.log(`\t\t\tpage ${pageNumber} downloaded ✔️`)
);
return new Promise((resolve, reject) => {
writableStream.once('finish', resolve);
writableStream.once('error', reject);
});
}
async function downloadChapter(
destination: string,
chapter: Chapter
): Promise<void> {
const chapterTitle = `[${await chapter.getTranslatedLanguage()}] ${await chapter.getNumber()} - ${await chapter.getTitle()}`;
console.log(`\t\t[${chapterTitle}] fetching pages...`);
const pages = await chapter.getMedia();
console.log(`\t\t[${chapterTitle}] {${pages.length}} pages found`);
await pages.reduce(
(previous, page, pageIndex) =>
previous.then(async () => {
await downloadPage(
path.normalize(path.join(destination, chapterTitle)),
page,
pageIndex + 1
);
}),
Promise.resolve()
);
}
async function downloadManga(destination: string, manga: Manga): Promise<void> {
const mangaTitle = await manga.getTitle();
console.log(`\t[${mangaTitle}] fetching chapters...`);
const chapters = await manga.getChapters();
console.log(`\t[${mangaTitle}] {${chapters.length}} chapters found`);
await chapters.reduce(
(previous, chapter) =>
previous.then(async () => {
await downloadChapter(
path.normalize(path.join(destination, mangaTitle)),
chapter
);
}),
Promise.resolve()
);
}
new Mangadex()
.search({
text: mangaToSearch,
filters: { availableTranslatedLanguage: ['fr'] },
})
.then(async (mangas) => {
console.log(mangas.length, 'mangas found');
await mangas.reduce(
(previous, manga) =>
previous.then(async () => {
console.log(`[${await manga.getTitle()}]`);
await downloadManga(folderPath, manga);
}),
Promise.resolve()
);
})
.catch(console.error);
Want to add your repository to the list of official media fetchers? Create an issue with the following label on the DevLib repository.
Licence
This project is under the CC-CY-4.0 licence.
Please respect it when using, modifing or distributing this project.
For more information, please go on the official Creative Commons website