Skip to content

Commit

Permalink
[nettruyen] update baseurl, optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
zcrossoverz committed Jun 24, 2023
1 parent ec18589 commit 6169b85
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 33 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ Here's an example of how to use manga-lib in your code:
const { Manga, MangaType } = require("manga-lib");

// Create a new instance of the manga site, MangaType.NETTRUYEN is currently support for https://www.nettruyenplus.com/
const manga = new Manga().build(MangaType.NETTRUYEN);
const manga = new Manga().build(MangaType.TOONILY);
// Create a new instance with custom url
const manga = new Manga().build(MangaType.TOONILY, {
baseUrl: "https://domain.com/",
});

// Get list latest manga
const latest = await manga.getListLatestUpdate();
const latest_page_2 = await manga.getListLatestUpdate(2);

// Retrieve the manga details
const detail_manga = await manga.getDetailManga(
"https://www.nettruyenplus.com/truyen-tranh/the-eminence-in-shadowto-be-a-power-in-the-shadows-side-story-86175"
"https://toonily.com/webtoon/nano-machine/"
);

// Get data chapter
const data_chapter = await manga.getDataChapter(
"https://www.nettruyenplus.com/truyen-tranh/the-eminence-in-shadowto-be-a-power-in-the-shadows-side-story/chap-1/1004177"
"https://toonily.com/webtoon/nano-machine/chapter-159/"
);

// Search manga
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "manga-lib",
"author": "Nhan Nguyen",
"version": "1.0.45",
"version": "1.0.46",
"license": "MIT",
"description": "A library for scraping manga from various websites.",
"repository": "https://github.com/zcrossoverz/manga-lib",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/manga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Manga {
return new Nettruyen(
params !== undefined && params.baseUrl !== undefined
? params.baseUrl
: "https://www.nettruyenplus.com"
: "https://www.nettruyenmax.com/"
);
}

Expand Down
26 changes: 26 additions & 0 deletions src/lib/nettruyen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import puppeteer, { Browser } from "puppeteer";
import {
Expand Down Expand Up @@ -30,6 +31,11 @@ export class Nettruyen implements AbstractMangaFactory {

async search(keyword: string, page = 1): Promise<responseListManga> {
const _page = await (await this.browser).newPage();
await _page.setRequestInterception(true);
_page.on("request", (req) => {
if (req.resourceType() !== "document") req.abort();
else req.continue();
});
await _page.goto(
`${this.baseUrl}/tim-truyen?keyword=${keyword}${
page > 1 ? `&page=${page}` : ``
Expand Down Expand Up @@ -124,6 +130,11 @@ export class Nettruyen implements AbstractMangaFactory {
} else if (page !== undefined) {
path += `?page=${page}`;
}
await _page.setRequestInterception(true);
_page.on("request", (req) => {
if (req.resourceType() !== "document") req.abort();
else req.continue();
});
await _page.goto(`${this.baseUrl}${path}`);
const element = await _page.$$(
"#ctl00_divCenter > div.Module.Module-170 > div > div.items > div > div.item > figure"
Expand Down Expand Up @@ -197,6 +208,11 @@ export class Nettruyen implements AbstractMangaFactory {
path = path !== undefined ? path : "";

const _page = await (await this.browser).newPage();
await _page.setRequestInterception(true);
_page.on("request", (req) => {
if (req.resourceType() !== "document") req.abort();
else req.continue();
});
await _page.goto(url_chapter);
const content = await _page.$(
"#ctl00_divCenter > div > div.reading-detail.box_doc"
Expand Down Expand Up @@ -285,6 +301,11 @@ export class Nettruyen implements AbstractMangaFactory {

async getDetailManga(url: string): Promise<responseDetailManga> {
const _page = await (await this.browser).newPage();
await _page.setRequestInterception(true);
_page.on("request", (req) => {
if (req.resourceType() !== "document") req.abort();
else req.continue();
});
await _page.goto(url);
const content = await _page.$("#ctl00_divCenter");
const title = await content!.$eval("article > h1", (el) => el.textContent);
Expand Down Expand Up @@ -398,6 +419,11 @@ export class Nettruyen implements AbstractMangaFactory {

async getListLatestUpdate(page = 1): Promise<responseListManga> {
const _page = await (await this.browser).newPage();
await _page.setRequestInterception(true);
_page.on("request", (req) => {
if (req.resourceType() !== "document") req.abort();
else req.continue();
});
await _page.goto(`${this.baseUrl}${page > 1 ? `/?page=${page}` : ``}`);

const element = await _page.$$(
Expand Down
13 changes: 1 addition & 12 deletions src/lib/toonily.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import axios from "axios";
import {
NETTRUYEN_STATUS_FILTER,
NETTRUYEN_SORT_FILTER,
} from "../constants/filter";
import * as cheerio from "cheerio";
import {
AbstractMangaFactory,
Expand Down Expand Up @@ -219,14 +215,7 @@ export class Toonily implements AbstractMangaFactory {
next_chapter: next_chapter !== undefined ? null : next_chapter_data,
};
}
getListByGenre(
genre: genre,
page?: number | undefined,
status?: NETTRUYEN_STATUS_FILTER | undefined,
sort?: NETTRUYEN_SORT_FILTER | undefined
): Promise<responseListManga> {
throw new Error("Method not implemented.");
}

async search(
keyword: string,
page?: number | undefined
Expand Down
21 changes: 7 additions & 14 deletions src/types/type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
// import { MangaType } from "..";

import { Browser } from "puppeteer";
import {
NETTRUYEN_SORT_FILTER,
NETTRUYEN_STATUS_FILTER,
} from "../constants/filter";

/* eslint-disable @typescript-eslint/no-empty-function */
import { Browser } from "puppeteer";

export type responseListManga = {
totalData: number;
Expand Down Expand Up @@ -88,12 +81,12 @@ export interface AbstractMangaFactory {
next_chapter?: chapter
): Promise<responseChapter>;

getListByGenre(
genre: genre,
page?: number,
status?: NETTRUYEN_STATUS_FILTER,
sort?: NETTRUYEN_SORT_FILTER
): Promise<responseListManga>;
// getListByGenre(
// genre: genre,
// page?: number,
// status?: NETTRUYEN_STATUS_FILTER,
// sort?: NETTRUYEN_SORT_FILTER
// ): Promise<responseListManga>;

search(keyword: string, page?: number): Promise<responseListManga>;
}
Expand Down
8 changes: 6 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ import { Manga, MangaType } from "./src";
// };
// });

const t = new Manga().build(MangaType.TOONILY);
const t = new Manga().build(MangaType.MANGAREADER);

void (async () => {
console.log(await t.search("h", 19));
console.log(
await t.getDataChapter(
"https://mangareader.to/read/afterschool-war-activities-700/en/chapter-51"
)
);
})();

0 comments on commit 6169b85

Please sign in to comment.