-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
160 additions
and
31 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface FetchResult { | ||
success: number | ||
failure: number | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import puppeteer from 'puppeteer-extra' | ||
import StealthPlugin from 'puppeteer-extra-plugin-stealth' | ||
import PQueue from "p-queue"; | ||
import type { DatabaseCode } from "@riffyh/commons/dist"; | ||
import type { Browser } from "puppeteer"; | ||
import { writeItem } from "./writeItem"; | ||
import type { FetchResult } from "../@types/FetchResult"; | ||
|
||
const fetchQueue = new PQueue({ | ||
concurrency: 8, | ||
}) | ||
|
||
export const getGalleriesViaBrowser = async (codes: (string | number)[]): Promise<FetchResult> => { | ||
const [firstGallery, ...galleries] = codes | ||
|
||
let success = 0 | ||
let failure = 0 | ||
|
||
if (codes.length === 0) | ||
return { | ||
success, | ||
failure | ||
} | ||
|
||
puppeteer.use(StealthPlugin()) | ||
const browser = await puppeteer | ||
.use(StealthPlugin()) | ||
.launch({ | ||
headless: false, | ||
defaultViewport: { | ||
width: 1190, | ||
height: 700 | ||
}, | ||
targetFilter: target => target.type() !== 'other' | ||
}) | ||
|
||
// get first by itself first | ||
await getItem(firstGallery, browser) | ||
.then(() => success++) | ||
.catch(() => failure++) | ||
await Promise.all( | ||
galleries.map(gallery => | ||
fetchQueue.add(() => | ||
getItem(gallery, browser) | ||
.then(() => success++) | ||
.catch(() => failure++) | ||
) | ||
) | ||
) | ||
|
||
await fetchQueue.onIdle() | ||
await browser.close() | ||
|
||
return { | ||
success, | ||
failure | ||
} | ||
} | ||
|
||
const getItem = async (code: string | number, browser: Browser) => { | ||
const page = await browser.newPage() | ||
await page.goto(`https://nhentai.net/api/gallery/${code}`, { | ||
waitUntil: 'networkidle0', | ||
}) | ||
|
||
// if hit cloudflare, then maybe wait faster? | ||
if ((await page.title()).toLowerCase().includes('just a moment')) | ||
await page.waitForNetworkIdle() | ||
|
||
const content = await page.$eval('pre', el => el.textContent) ?? '' | ||
await page.close() | ||
|
||
await writeItem(code, content) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import PQueue from "p-queue"; | ||
import type { DatabaseCode } from "@riffyh/commons"; | ||
import { fetchHentai } from "./fetchHentai"; | ||
import type { FetchResult } from "../@types/FetchResult"; | ||
|
||
const fetchQueue = new PQueue({ | ||
concurrency: 8, | ||
}) | ||
|
||
export const getGalleriesViaFetch = async (codes: (string | number)[]): Promise<FetchResult> => { | ||
let success = 0 | ||
let failure = 0 | ||
|
||
if (codes.length === 0) | ||
return { | ||
success, | ||
failure | ||
} | ||
|
||
await Promise.all( | ||
codes.map(code => | ||
fetchQueue.add(() => | ||
fetchHentai(code) | ||
.then(() => success++) | ||
.catch(() => failure++) | ||
) | ||
) | ||
) | ||
|
||
await fetchQueue.onIdle() | ||
|
||
return { | ||
success, | ||
failure | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import type { Hentai } from "@riffyh/commons"; | ||
import { hentaiDirectory } from "../constants/hentaiDirectory"; | ||
|
||
export const writeItem = (code: string | number, item: Hentai | string) => { | ||
const file = path.join(hentaiDirectory, `${code}.json`) | ||
return fs.promises.writeFile( | ||
file, | ||
typeof item === 'string' ? item : JSON.stringify(item) | ||
) | ||
} |
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