Skip to content

Commit

Permalink
feat(image-search): adding iqdb search (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-fish committed Mar 6, 2021
1 parent 9a37afe commit 9cd9abb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/plugin-image-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"axios": "^0.21.1",
"cheerio": "^1.0.0-rc.5",
"iqdb-client": "^1.0.3",
"nhentai-api": "^3.0.2"
}
}
3 changes: 3 additions & 0 deletions packages/plugin-image-search/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context, Session, Command, makeArray, segment } from 'koishi-core'
import ascii2d from './ascii2d'
import saucenao from './saucenao'
import iqdb from './iqdb'

export interface Config extends saucenao.Config {
saucenaoApiKey?: string | string[]
Expand Down Expand Up @@ -39,6 +40,8 @@ export function apply(ctx: Context, config: Config = {}) {
.action(search(saucenao))
.subcommand('ascii2d [image]', '使用 ascii2d 搜图')
.action(search(ascii2d))
.subcommand('iqdb [image]', '使用 iqdb 搜图')
.action(search(iqdb))

const pendings = new Set<string>()

Expand Down
33 changes: 33 additions & 0 deletions packages/plugin-image-search/src/iqdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { searchPic } from 'iqdb-client'
import { segment } from 'koishi-utils'
import { Session } from 'koishi-core'

async function makeSearch(url: string): Promise<string> {
const res: any = await searchPic(url, { lib: 'www' })
if (res.ok || (res.data && res.data.length > 1)) {
let data: any = res.data[1]
let { head, sourceUrl, img, type, source } = data

return [
segment('image', { url: 'https://iqdb.org' + img }),
'准度:' + head.toLowerCase(),
'来源:' + sourceUrl,
'色图:' + (type.toLowerCase() === 'safe' ? '否' : '是⚠️'),
'源站:' + source.join(', '),
].join('\n')
} else if (res.err) {
return '搜图时遇到问题:' + res.err
} else {
return '搜图时遇到未知问题。'
}
}

export default async function(url: string, session: Session) {
let result: string = 'iqdb.org 搜图\n'
try {
result += await makeSearch(url)
} catch (err) {
result += '搜图时遇到问题:' + err
}
return session.send(result)
}

0 comments on commit 9cd9abb

Please sign in to comment.