2.0.0 • Published 2 years ago

iqdb-client v2.0.0

Weekly downloads
53
License
MIT
Repository
github
Last release
2 years ago

iqdb-client

jsdelivr npm vulnerabilities workflowdependents

iqdb.org api client for Node.js.

English | 中文文档

亮点

  • 自带类型定义
  • 支持“忽略颜色”(ignore color)选项.
  • 支持对单一库的搜索.
  • 同时支持iqdb2d与iqdb3d.
  • 支持上传文件搜索(通过Buffer或Stream)和根据图片地址搜索
  • 基于Fetch

破坏性更改

2.0.0,对similarity一词的误拼已被修复,你若修改了IQDBClientConfig中对应的字段,那你可能需要做出更改。如果你使用的是默认的IQDBClientConfig,那这一更改对你不会有什么影响。 IQDBSearchResultItemsimilarity字段的类型由number | string变为number | null,意味着解析失败时不再返回文本。

安装

npm install iqdb-client
# 或者使用yarn
yarn add iqdb-client

用法

const searchPic = require('iqdb-client')
const result = (await searchPic('https://pixiv.cat/84035784-3.jpg', { lib: 'www' }))
/** 也支持ES风格的导入*/
//更多示例请查看./src/api.test.ts
if(result.ok){
    console.log(result.data)
}

参数

searchPic(pic: string | Buffer | Readable, 
{ lib, forcegray, libs,fileName }: IQDB_SEARCH_OPTIONS_ALL)
  • lib: string, 必须提供 想要使用多库搜索时,提供'www'(for iqdb2d)或'3d'(for 3diqdb), 使用在h.tsIQDB_SEARCH_LIBRARY_2D中定义的其他值时将执行单库搜索。
  • forcegray: boolean
    是否忽略颜色
  • fileName: string
    表示表单中的‘filename’应为何值。仅在根据文件查找时有效。若没有提供,会自动生成一个随机文件名代替。
  • service: Array<number> 执行多库搜索时,决定在哪些库上搜索

iqdb2d (lib='www')时可用的值:

export enum IQDBLibs_2D {
    danbooru = 1,
    konachan = 2,
    'yande.re' = 3,
    gelbooru = 4,
    'sankaku channel' = 5,
    'e-shuushuu' = 6,
    zerochan = 11,
    'anime-picture' = 13
}

3diqdb (lib='3d')时可用的值:

export enum IQDBLibs_3D {
    '3dbooru' = 7,
    'idol' = 9
}

返回值

当成功完成请求, 函数会返回一个包含{ok:boolean}的对象, 如返回结果示例所展示的那样。如果相似度检查通过,ok字段会被设为true。 若在执行中碰到错误, 函数会返回如下一个对象:

{
    ok:false,
    /*错误信息*/
    err:'HTTP 400'
}

由于缺乏实例,错误处理还不是特别完善。欢迎就你碰到的问题提出issue。 返回结果示例

{
    "ok": true,
    "data": [{
        "head": "Your image",
        "img": "/thu/thu_114514.jpg",
        "name": "84035784_p2.jpg",
        "similarity": null,
        "size": {
            "width": 1703,
            "height": 2459
        },
        "type": null
    }, {
        "head": "Best match",
        "sourceUrl": "//danbooru.donmai.us/posts/4076714",
        "similarity": 0.96,
        "img": "/danbooru/1/f/8/1f8ff3c560a0689e795938138dac7b1f.jpg",
        "size": {
            "width": 1703,
            "height": 2459
        },
        "type": "Safe",
        "source": ["Danbooru", "Gelbooru"]
    }, {
        "head": "Additional match",
        "sourceUrl": "https://yande.re/post/show/678391",
        "similarity": 0.92,
        "img": "/moe.imouto/8/0/1/801df5f665e61e6f87eb85431f2ca2a1.jpg",
        "size": {
            "width": 1703,
            "height": 2459
        },
        "type": "Safe",//是否nsfw, 可能是'Safe' | 'Ero' | 'Explicit'中的一个值
        "source": ["yande.re"]
    }],
    "service": [1, 2, 3, 4, 5, 6, 11, 13] //在本次搜索中用到了的库
}

进阶用法

interface IQDBClientConfig {
    baseDomain: string,
    similarityPass: number
    userAgent: string,
    fetchOptions?: import('node-fetch').RequestInit
}
const { makeSearchFunc } = require('iqdb-client')
const searchPic = await makeSearchFunc({
            baseDomain: `127.0.0.1`,
            similarityPass: 0.6,
            userAgent: 'testa',
        })

使用makeSearchFunc()来自定义代理使用,相似度判定等选项。makeSearchFunc()会返回一个新的searchPic() 默认导出的searchPic()使用defaultConfig

export const defaultConfig: IQDBClientConfig = {
    baseDomain: 'iqdb.org',
    similarityPass: 0.6,
    userAgent: 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
}

支持

  • 支持iqdb.org
  • 遇到问题时,提交issue或PR

许可协议

本仓库的代码在MIT许可下授权。 更多信息请查看 LICENSE