UNPKG

paheal

Version:

Simple and user-friendly Wrapper of Rule 34 Paheal

50 lines (40 loc) 2.25 kB
const cheerio = require('cheerio') const cheerioTableparser = require('cheerio-tableparser'); const blacklist = require('./blacklist') class paheal { parseSearch(html) { let results = [] const $ = cheerio.load(html) if (html.match(/<b>Message:<\/b>/)) return { status: 404, message: 'Can\'t find media of this user'} if ($('#Errormain')[0]) return { status: 403, message: 'No images were found to match the search criteria'} if (!$('.shm-image-list').length) return this.parse(html) $('.shm-image-list').children().each((i, e) => { if (e.attribs['data-tags'].split(/ +/).some(tag => blacklist.includes(tag.toLowerCase()))) return; let obj = { id: 0, tags: [], href: '' } obj.id = e.attribs['data-post-id'] obj.tags = e.attribs['data-tags'].split(/ +/) obj.href = e.children[0].children[0].attribs.src return results.push(obj) }) let lastPage = html.match(/<a href="(.+)">Last<\/a>/) ? html.match(/<a href="(.+)">Last<\/a>/)[1].split(/ +/) : false lastPage = lastPage ? lastPage[lastPage.length - 1].match(/\d+/)[0] : 1 return { results, lastPage } } parse(html) { let obj = { author: '', tags: [], src: '', href: '' } let tags = [] const $ = cheerio.load(html) cheerioTableparser($); if ($('#Errormain')[0]) return { status: 403, message: 'No post in the database'} if (html.match(/<b>Message:<\/b>/)) return { status: 404, message: 'No media on this path'} $('.tag_name').each((i, e) => e.children.forEach(el => tags.push(el.data))) if (tags.some(tag => blacklist.includes(tag.toLowerCase()))) return { status: 403, message: 'We cannot display this material. And you better not look for it.'} obj.tags = tags obj.href = $('#main_image').attr().src obj.author = $('.username')[0].children[0].data; let source = $('.image_info.form').parsetable()[1][2].match(/href="(.+)"/) obj.src = source ? source[1] : undefined return { status: 302, obj: obj } } } module.exports = paheal