Skip to content

Commit 368f725

Browse files
author
Phumrapee Limpianchop
committed
feat: add ability to provide database code as full url
1 parent 4172cd0 commit 368f725

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

commons/src/@types/DatabaseCode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type DatabaseCode =
2+
| string
23
| number
34
| {
45
code: number

scraper/src/commands/fetch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Kysely } from 'kysely'
1010
import { SQLDatabase } from '../@types/SQLDatabase'
1111
import { createDBConnection } from '../functions/createDBConnection'
1212
import { getRemoteHentai } from '../functions/getRemoteHentai'
13+
import {parseUrl} from "../functions/parseUrl";
1314

1415
const fetchQueue = new PQueue({
1516
concurrency: 8,
@@ -42,7 +43,7 @@ export const fetch = async (entryPoint: string) => {
4243
const chunkFile = path.join(prebuiltChunkDirectory, `chunk-${i + 1}.json`)
4344
await fs.promises.writeFile(
4445
chunkFile,
45-
JSON.stringify(chunk.map(o => (typeof o === 'number' ? o : o.code)))
46+
JSON.stringify(chunk.map(o => (typeof o === 'number' ? o : typeof o === 'string' ? parseUrl(o) : o.code)))
4647
)
4748
})
4849
)
@@ -87,7 +88,7 @@ export const fetch = async (entryPoint: string) => {
8788
const orderedHentai = codes
8889
.map(code => {
8990
try {
90-
const targetCode = typeof code === 'number' ? code : code.code
91+
const targetCode = typeof code === 'number' ? code : typeof code === 'string' ? parseUrl(code) : code.code
9192
const targetHentai: Hentai = JSON.parse(
9293
fs
9394
.readFileSync(path.join(hentaiDirectory, `${targetCode}.json`))

scraper/src/functions/parseUrl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const parseUrl = (input: string) => {
2+
const matcher = input.match(/https?:\/\/nhentai.net\/g\/(\d+)\/?/)
3+
if (matcher !== null && Number.isInteger(Number(matcher[1])))
4+
return Number(matcher[1])
5+
else
6+
throw new Error("Cannot parse " + input)
7+
}

0 commit comments

Comments
 (0)