paheal
Version:
Simple and user-friendly Wrapper of Rule 34 Paheal
68 lines (59 loc) • 2.55 kB
JavaScript
const pahealService = require('./utils/pahealService')
const utils = new pahealService()
const fetch = require('node-fetch')
const searchService = require('./utils/searchService')
const searchChoise = {
name: 'https://rule34.paheal.net/post/list/user=',
random: 'https://rule34.paheal.net/post/list/',
text: 'https://rule34.paheal.net/post/list/',
id: 'https://rule34.paheal.net/post/view/'
}
class paheal {
async #returner(obj) {
if (obj.type == 'id' && typeof obj.value !== 'number' && typeof obj.value !== 'string') return { status: 400, message: 'You entered an invalid request'}
let answer, key = obj.type == 'id' ? Math.abs(obj.value) : obj.value, url = obj.type == 'random' ? searchChoise[obj.type] + '/1' : searchChoise[obj.type] + key + '/1', html = await fetch(url).then(res => res.text())
if (obj.type == 'id') {
let answer = utils.parse(html)
if (answer.status != 302) return answer
return answer.obj
}
answer = utils.parseSearch(html)
if (answer.status == 302) return answer.obj
else if (answer.status) return answer
return new searchService(answer.results, url, answer.lastPage)
}
/**
* @param {String} text Tags for put the input of search results
*
* **Details of return:**
* * Class with methods for Continue Searching
* * Objects in array with Doujins
* @returns {Promise<searchService> | Promise<{author: String, tags: String[], src: String, href: String}>} Class with Methods and Results
*/
search(text) {
let obj = { type: "text", value: text }
if (!text) obj.type = 'random'
return this.#returner(obj)
}
/**
* Give your specific media from current identificator
*
* @param {String | Number} id identificator of specific post
* @returns {Promise<{author: String, tags: String[], src: String, href: String}>} Object with information of media
*/
getMedia(id) {
return this.#returner({ type: "id", value: id })
}
/**
* @param {String} text Tags for put the input of search results
*
* **Details of return:**
* * Class with methods for Continue Searching
* * Objects in array with Doujins
* @returns {Promise<searchService>} Class with Methods and Results
*/
byAuthor(name) {
return this.#returner({ type: "name", value: name })
}
}
module.exports = paheal