import * as cheerio from 'cheerio'
import _ from 'lodash'
export const BASE_URL = 'https://iqdb.org/'
export function parse(body) {
const $ = cheerio.load(body)
return _.map($('table'), result => {
const content = $(result).text(),
[link] = $('td.image > a', result),
[image] = $('td.image img', result)
if (!link) return
const [, similarity] = content.match(/(\d+%)\s*similarity/),
//报错点
[, resolution, level] = content.match(/(\d+×\d+)\s*\[(\w+)\]/)
return {
url: new URL(link.attribs.href, BASE_URL).toString(),
image: new URL(image.attribs.src, BASE_URL).toString(),
similarity: parseFloat(similarity),
resolution,
level: level.toLowerCase()
}
})
.filter(v => v !== undefined)
.sort((a, b) => a.similarity - b.similarity)
.reverse()
}
import fetch from 'node-fetch'
import { FormData } from 'formdata-node'
import { fileFromPath } from 'formdata-node/file-from-path'
export async function post(req) {
const { services, discolor, imagePath } = req
const form = new FormData()
form.append('file', await fileFromPath(imagePath))
if (services) services.forEach((s, index) => form.append(`service.${index}`, s.toString()))
if (discolor) form.append('forcegray', 'on')
const response = await fetch(BASE_URL, { method: 'POST', body: form }).then(res => res.text())
console.log(response)
return parse(response)
}
const response = await post({
// discolor: true,
// 为false时会报错
discolor: true,
imagePath: '../123.png',
services: [
'danbooru',
'konachan',
'yandere',
'gelbooru',
'sankaku_channel',
'e_shuushuu',
'zerochan',
'anime_pictures'
]
})
console.dir(response, { depth: null })