-
-
Save rayriffy/02f2fd93c947ff1525fc66d5138059e5 to your computer and use it in GitHub Desktop.
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
/** | |
* Functions for query data from NHentai API | |
* @param {int} id Gallery ID | |
*/ | |
const getRawData = async id => { | |
try { | |
let isCacheFound = false | |
let cacheRes | |
// Read file from cache | |
if (fs.existsSync('.tmp/crawler.json')) { | |
const reader = fs.readFileSync('.tmp/crawler.json', 'utf8') | |
const objects = JSON.parse(reader) | |
_.each(objects, object => { | |
if (object.data.id === id && object.status === 'success') { | |
isCacheFound = true | |
cacheRes = object | |
} | |
}) | |
} | |
if (isCacheFound) { | |
return cacheRes | |
} else { | |
// Using reverse proxy server to avoid CORS issue | |
const out = await axios.get(`https://nh-express-git-master.rayriffy.now.sh/api/gallery/${id}`) | |
return { | |
status: 'success', | |
data: { | |
id: id, | |
raw: out.data, | |
}, | |
} | |
} | |
} catch (err) { | |
console.log(`cannot process ${id} with code ${err.code}`) | |
return { | |
status: 'failure', | |
data: { | |
id: id, | |
}, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment