-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from koikiss-dev/refactoring/nhentai-scrapper…
…-api Refactoring/nhentai scrapper api
- Loading branch information
Showing
4 changed files
with
139 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
); | ||
}); | ||
}); |