2022-12-22 00:03:22 +09:00
import p from "phin" ;
import Keyv from "keyv" ;
2023-02-11 02:17:45 +09:00
import { CookieJar } from "tough-cookie" ;
import { HttpsCookieAgent } from "http-cookie-agent/http" ;
2022-12-22 00:03:22 +09:00
const keyv = new Keyv ( process . env . REDIS_URL ) ;
2023-02-11 02:17:45 +09:00
const strategy = process . env . NHENTAI_IP_ORIGIN || "true" ;
2022-12-22 00:03:22 +09:00
keyv . on ( "error" , err = > console . log ( "Connection Error" , err ) ) ;
2022-12-25 04:45:19 +09:00
const ttl = 1000 * 60 * 60 * Number ( process . env . EXPIRE_CACHE ) ;
2022-12-22 00:03:22 +09:00
2023-02-11 02:17:45 +09:00
const jar = new CookieJar ( ) ;
jar . setCookie ( process . env . COOKIE || "" , "https://nhentai.net/" ) ;
2022-12-22 00:03:22 +09:00
class JandaPress {
url : string ;
2023-02-11 02:17:45 +09:00
useragent : string ;
2022-12-22 00:03:22 +09:00
constructor ( ) {
this . url = "" ;
2023-02-11 02:17:45 +09:00
this . useragent = "jandapress/1.0.5 Node.js/16.9.1" ;
}
async simulateCookie ( target : string , parseJson = false ) : Promise < p.IResponse | unknown > {
if ( ! parseJson ) {
const res = await p ( {
url : target ,
followRedirects : true ,
core : {
agent : new HttpsCookieAgent ( { cookies : { jar , } , } ) ,
} ,
headers : {
"User-Agent" : process . env . USER_AGENT || "" ,
} ,
} ) ;
return res ;
} else {
const res = await p ( {
url : target ,
parse : "json" ,
core : {
agent : new HttpsCookieAgent ( { cookies : { jar , } , } ) ,
} ,
headers : {
"User-Agent" : process . env . USER_AGENT || "" ,
} ,
} ) ;
return res . body ;
}
}
/ * *
* Simulating nhentai request if origin api is not available
* You ' ll need [ tough - cookie ] ( https : //www.npmjs.com/package/tough-cookie) and [http-cookie-agent](https://www.npmjs.com/package/http-cookie-agent) to make this work
* @param target url to fetch
* @returns Promise < unknown >
* @throws Error
* /
async simulateNhentaiRequest ( target : string ) : Promise < unknown > {
if ( strategy === "true" ) {
const res = await p ( {
url : target ,
parse : "json"
} ) ;
return res . body ;
} else {
try {
const res = await this . simulateCookie ( target , true ) ;
return res ;
} catch ( err ) {
const e = err as Error ;
throw new Error ( e . message ) ;
}
}
2022-12-22 00:03:22 +09:00
}
/ * *
* Fetch body from url and check if it ' s cached
* @param url url to fetch
* @returns Buffer
* /
async fetchBody ( url : string ) : Promise < Buffer > {
const cached = await keyv . get ( url ) ;
if ( cached ) {
console . log ( "Fetching from cache" ) ;
return cached ;
} else if ( url . includes ( "/random" ) ) {
console . log ( "Random should not be cached" ) ;
const res = await p ( { url : url , followRedirects : true } ) ;
return res . body ;
} else {
console . log ( "Fetching from source" ) ;
const res = await p ( { url : url , followRedirects : true } ) ;
await keyv . set ( url , res . body , ttl ) ;
return res . body ;
}
}
/ * *
* Fetch json from url and check if it ' s cached
* @param url url to fetch
* @returns Buffer
* /
2023-02-11 02:17:45 +09:00
async fetchJson ( url : string ) : Promise < unknown > {
2022-12-22 00:03:22 +09:00
const cached = await keyv . get ( url ) ;
if ( cached ) {
console . log ( "Fetching from cache" ) ;
return cached ;
} else {
console . log ( "Fetching from source" ) ;
2023-02-11 02:17:45 +09:00
const res = await this . simulateNhentaiRequest ( url ) ;
await keyv . set ( url , res , ttl ) ;
return res ;
2022-12-22 00:03:22 +09:00
}
}
currentProccess() {
const arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 9 , 7 , 8 , 9 , 10 ] ;
arr . reverse ( ) ;
const rss = process . memoryUsage ( ) . rss / 1024 / 1024 ;
const heap = process . memoryUsage ( ) . heapUsed / 1024 / 1024 ;
const heaptotal = process . memoryUsage ( ) . heapTotal / 1024 / 1024 ;
return {
rss : ` ${ Math . round ( rss * 100 ) / 100 } MB ` ,
heap : ` ${ Math . round ( heap * 100 ) / 100 } / ${ Math . round ( heaptotal * 100 ) / 100 } MB `
} ;
}
}
export default JandaPress ;