Skip to content

Commit

Permalink
Merge pull request #105 from koikiss-dev/refactoring/nhentai-scrapper…
Browse files Browse the repository at this point in the history
…-api

Refactoring/nhentai scrapper api
  • Loading branch information
koikiss-dev authored Oct 11, 2023
2 parents 79e86dc + 3942f2d commit c6349a1
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 146 deletions.
32 changes: 15 additions & 17 deletions src/routes/v1/manga/nhentai/NhentaiRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@ import { Nhentai } from "../../../../scraper/sites/manga/nhentai/Nhentai";

const router = Router();


router.get('/manga/nhentai/filter/:mangaName', async (request, response) => {
router.get("/manga/nhentai/filter/:mangaName", async (request, response) => {
try {
const { mangaName } = request.params;
const nhentai = await new Nhentai().filter(mangaName);
response.send(nhentai)
response.send(nhentai);
} catch (error) {
response.status(500).send(error);
}
})

router.get('/manga/nhentai/info/:mangaId', async (request, response) => {
try {
const { mangaId } = request.params;
const nhentai = await new Nhentai().getMangaInfo(mangaId);
response.send(nhentai)
} catch (error) {
response.status(500).send(error);
}
})
});

router.get("/manga/nhentai/info/:mangaId", async (request, response) => {
try {
const { mangaId } = request.params;
const nhentai = await new Nhentai().getMangaInfo(mangaId);
response.send(nhentai);
} catch (error) {
response.status(500).send(error);
}
});

router.get('/manga/nhentai/chapters/:mangaId', async (request, response) => {
router.get("/manga/nhentai/chapters/:mangaId", async (request, response) => {
try {
const { mangaId } = request.params;
const nhentai = await new Nhentai().getMangaChapters(mangaId);
response.send(nhentai)
response.send(nhentai);
} catch (error) {
response.status(500).send(error);
}
})
});

export default router;
146 changes: 67 additions & 79 deletions src/scraper/sites/manga/nhentai/Nhentai.ts
Original file line number Diff line number Diff line change
@@ -1,137 +1,125 @@
import axios from 'axios';
import { load } from 'cheerio';
import { getFilterByPages } from './assets/getFilterByPage';
import { IMangaChapter, Manga } from '../../../../types/manga';
import axios from "axios";
import { load } from "cheerio";
import { getFilterByPages } from "./assets/getFilterByPage";
import { IMangaChapter, Manga } from "../../../../types/manga";

export class Nhentai {

async filter(mangaName: string) {
return new NhentaiFilter().filter(mangaName);
}

async getMangaInfo(mangaId: string) {
async getMangaInfo(mangaId: string) {
return new NhentaiMangaInfo().getMangaInfoById(mangaId);
}

async getMangaChapters(mangaId: string) {
return new NhentaiGetMangaChapters().getMangaChapters(mangaId);
}

}


class NhentaiFilter {

url = "https://nhentai.to/search?q=";
url = "https://nhentai.to/search?q=";

async filter(mangaName: string) {
try {
const { data } = await axios.get(`${this.url}${mangaName}`);

let { data } = await axios.get(`${this.url}${mangaName}`);
const $ = load(data);

let $ = load(data);
let numPages = $("section.pagination a").length;

let numPages = $("section.pagination a").length;
if (numPages != 0) {
numPages = numPages - 2;
} else {
numPages = 1;
}

if (numPages != 0) {
numPages = numPages - 2;
}else {
numPages = 1;
}

let getResults = await getFilterByPages(mangaName, numPages);
const getResults = await getFilterByPages(mangaName, numPages);

return getResults;
return getResults;
} catch (error) {
throw error;
}
}


}



class NhentaiMangaInfo {


async getMangaInfoById(mangaId: string) {
try {
const { data } = await axios.get(`https://nhentai.to/g/${mangaId}`);

const $ = load(data);

try {

const manga = new Manga();

let { data } = await axios.get(`https://nhentai.to/g/${mangaId}`);
manga.characters = [];
manga.authors = [];
manga.chapters = [];

let manga = new Manga();

manga.characters = [];
manga.authors = [];
manga.chapters = [];
manga.title = $("div#info h1").text();
manga.thumbnail = {
url: $("div#cover a img").attr("src"),
};

let $ = load(data);
manga.id = mangaId;
manga.isNSFW = true;

manga.title = $("div#info h1").text();
manga.thumbnail = {
url: $("div#cover a img").attr('src')
};
const chracters = $("section#tags .tag-container").get(1);
const autors = $("section#tags .tag-container").get(3);

manga.id = mangaId;
manga.isNSFW = true;
$(autors)
.find("span a span.name")
.each((_, elementCheerio) => {
manga.authors.push($(elementCheerio).text());
});

const chracters = $("section#tags .tag-container").get(1);
const autors = $("section#tags .tag-container").get(3);
$(chracters)
.find("span a span.name")
.each((_, elementCheerio) => {
manga.characters.push($(elementCheerio).text());
});

$(autors).find("span a span.name").each((_, elementCheerio) => {
manga.authors.push($(elementCheerio).text())
})

$(chracters).find("span a span.name").each((_, elementCheerio) => {
manga.characters.push($(elementCheerio).text())
});

return manga

}catch(error) {
return error
return manga;
} catch (error) {
throw error;
}

}
}


class NhentaiGetMangaChapters {
async getMangaChapters(mangaId: string): Promise<IMangaChapter[]> {
try {
const { data } = await axios.get(`https://nhentai.to/g/${mangaId}`);

async getMangaChapters(mangaId: string) {

try {
const $ = load(data);

let { data } = await axios.get(`https://nhentai.to/g/${mangaId}`);
const mangaChapters: IMangaChapter[] = [];

let $ = load(data);
const mangaImagesPages: string[] = [];

let mangaChapters: IMangaChapter[] = [];

let mangaImagesPages: string[] = [];

$("div#thumbnail-container .thumb-container a img ").each((_, chapterImage) => {
mangaImagesPages.push(
$(chapterImage).attr("data-src")
)
})
$("div#thumbnail-container .thumb-container a img ").each(
(_, chapterImage) => {
mangaImagesPages.push(
$(chapterImage)
.attr("data-src")
.replace("cdn.dogehls.xyz", "t7.nhentai.net"),
);
},
);

mangaChapters.push({
title: "it doesn't",
number: 1,
cover: "it doesn't",
url: "/manga/nhentai/chapter/1",
id: 1,
images: mangaImagesPages
})

images: mangaImagesPages,
});

return mangaChapters;

} catch (error) {
return error
throw error;
}

}

}

45 changes: 23 additions & 22 deletions src/scraper/sites/manga/nhentai/assets/getFilterByPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@ import { IMangaResult } from "@animetypes/manga";
import axios from "axios";
import { load } from "cheerio";

export async function getFilterByPages(mangaName: string, numPage: number) {
export async function getFilterByPages(
mangaName: string,
numPage: number,
): Promise<IMangaResult[]> {
const searchResults: IMangaResult[] = [];

for (let index = 1; index <= numPage; index++) {
const { data } = await axios.get(
`https://nhentai.to/search?q=${mangaName}&page=${index}`,
);

let searchResults: IMangaResult[] = [];

for (let index = 1; index <= numPage ; index++) {

let { data } = await axios.get(`https://nhentai.to/search?q=${mangaName}&page=${index}`);

let $ = load(data);
const $ = load(data);

$(".container .gallery a").each((_, elementCheerio) => {
let id = $(elementCheerio).attr("href").split("/")[2];
let title = $(elementCheerio).find(".caption").text();
let coverImg = $(elementCheerio).find("img").attr("src");

searchResults.push({
id: id,
title: title,
url: `/manga/nhentai/title/${id}`,
thumbnail: {
url: coverImg
}
})
})
const id = $(elementCheerio).attr("href").split("/")[2];
const title = $(elementCheerio).find(".caption").text();
const coverImg = $(elementCheerio).find("img").attr("src");

searchResults.push({
id: id,
title: title,
url: `/manga/nhentai/title/${id}`,
thumbnail: {
url: coverImg,
},
});
});
}

return searchResults;

}
62 changes: 34 additions & 28 deletions src/test/Nhentai.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { Nhentai } from "../../src/scraper/sites/manga/nhentai/Nhentai";


describe('It returns a list of animes related that name filter', () => {

it('it should match that fields', async () => {
const mangasHentais = await new Nhentai().filter("Evangelion");

expect(mangasHentais[0].id).toBe("403447");
expect(mangasHentais[0].title).toBe("[Cassino (Magarikouji Lily)] Playboys (2) – Neon Genesis Evangelion dj [Eng]");

})

it('it should return a manga information from manga id', async () => {
const getMangaInfo: any = await new Nhentai().getMangaInfo("403447");

expect(getMangaInfo.title).toEqual("[Cassino (Magarikouji Lily)] Playboys (2) – Neon Genesis Evangelion dj [Eng]");
import { IMangaChapter, IMangaResult, Manga } from "../types/manga";

describe("It returns a list of animes related that name filter", () => {
it("it should match that fields", async () => {
const mangasHentai: IMangaResult[] = await new Nhentai().filter(
"Evangelion",
);

expect(mangasHentai[0].id).toBe("403447");
expect(mangasHentai[0].title).toBe(
"[Cassino (Magarikouji Lily)] Playboys (2) – Neon Genesis Evangelion dj [Eng]",
);
});
});

describe("Manga info tests", () => {
it("it should return a manga information from manga id", async () => {
const getMangaInfo: Manga = await new Nhentai().getMangaInfo("403447");

expect(getMangaInfo.title).toEqual(
"[Cassino (Magarikouji Lily)] Playboys (2) – Neon Genesis Evangelion dj [Eng]",
);
expect(getMangaInfo.id).toEqual("403447");

})

it('it should return a manga chapters from manga id', async () => {

const getMangaChapters: any = await new Nhentai().getMangaChapters("403447");
expect(getMangaChapters[0].images[0]).toEqual("https://cdn.dogehls.xyz/galleries/2223278/1t.jpg")

})


})
});
});

describe("Manga chapters", () => {
it("it should return a manga chapters from manga id", async () => {
const getMangaChapters: IMangaChapter[] =
await new Nhentai().getMangaChapters("403447");
expect(getMangaChapters[0].images[0]).toEqual(
"https://t7.nhentai.net/galleries/2223278/1t.jpg",
);
});
});

0 comments on commit c6349a1

Please sign in to comment.