Coder Social home page Coder Social logo

iqdb-api's Introduction

L2 Studio - IQDB API

A library for iqdb.org image search api

Install

npm install --save @l2studio/iqdb-api
# or
pnpm i @l2studio/iqdb-api

API

import * as iqdb from '@l2studio/iqdb-api'
# or
import { search } from '@l2studio/iqdb-api'

.search

iqdb.search(input: SearchInput, options?: SearchOptions): Promise<SearchResponse>

SearchInput

A type alias, equivalent to String | Buffer | Readable. Can be a stringify image url, image buffer or readable stream.

export type SearchInput = string | Buffer | Readable

E.g.:

  • String - 'http://example.com/yourimage.jpg'
  • Buffer - fs.readFileSync('/path/yourimage.jpg')
  • Readable - fs.createReadStream('/path/yourimage.jpg')

SearchOptions

Website origin and services see also: /src/type.ts

export interface SearchOptions {
  /*
   * Websites and Image Services Used.
   * If services is not defined, all services of the current origin are used.
   */
  website?: ({
    origin: Website,
    services?: RootWebsiteService[]
  } | {
    origin: Website,
    services?: ThreeDWebsiteService[]
  })
  /**
   * Custom filename when the input is a image buffer or readable stream.
   * Use random string when is not defined.
   */
  filename?: string
  // Whether to ignore the color of the input image. (Force Gray)
  ignoreColors?: boolean
  // Whether to pick up more other results.
  pickOtherResults?: boolean
  // Got request options (See: https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
  requestOptions?: OptionsOfTextResponseBody
}

SearchResult

export interface SearchResult {
  // Result match
  match: 'best' | 'additional' | 'possible' | 'other'
  // Thumbnail
  thumbnail: {
    // Raw src: /danbooru/foo/bar.jpg
    src: string
    // Fixed src: https://iqdb.org/danbooru/foo/bar.jpg
    fixedSrc: string
    // Image rating: q, s, e...
    rating?: string
    // Image score
    score?: number
    // Image tags
    tags?: string[]
  }
  // All source services
  sources: Array<{
    // Service: Danbooru, Gelbooru, etc...
    service: Service
    // Raw href: //danbooru.donmai.us/posts/foo
    href: string
    // Fixed href: http://danbooru.donmai.us/posts/foo
    // Fixed always use http!
    fixedHref: string
  }>
  // Result width
  width: number
  // Result height
  height: number
  // Result type
  type: 'safe' | 'ero' | 'explicit' | 'unrated'
  // Result similarity (0 - 100)
  similarity: number
  // Similarity percentage (0.00 - 1.00)
  similarityPercentage: number
}

SearchResponse

export interface SearchResponse {
  // Searched images
  searched: number
  // Timecost (Seconds) e.g.: 14.054
  timeSeconds: number
  // Timecost (Milliseconds) e.g.: 14054
  timeMilliseconds: number
  // IQDB Thumbnail src. e.g.: https://iqdb.org/thu/thu_24432b8d.jpg
  thumbnailSrc: string
  // Other search sites for thumbnail src.
  otherSearchHrefs: {
    saucenao: string
    ascii2d: string
    google: string
    tineye: string
  }
  // Search results
  results: SearchResult[]
}

License

Apache-2.0

iqdb-api's People

Contributors

lgou2w avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

iqdb-api's Issues

TypeError if a possible result isn't in the format [Image Dimensions] [Type]

Good morning/afternoon/evening,

I'd like to report what is likely a bug due to IQDB (or rather, probably Gelbooru) rarely returning an [Unrated] without any image dimensions specified, which in turn makes line 186 in api.js fail with a TypeError, as dimensionTexts[1] ends up being undefined.

function parseResultsPage($, $pageEl) {
  // ...
  const $dimensionEl = $($rows[$matchEl.length + 2]).find('td');
  const dimensionTexts = $dimensionEl.text().trim().split(' ');
  const dimension = dimensionTexts[0].replace(/[^\d]/g, '-').split('-');
  const width = parseInt(dimension[0]);
  const height = parseInt(dimension[1]);
  const type = dimensionTexts[1].substring(1, dimensionTexts[1].length - 1).toLowerCase(); // error occurs here
  const $similarityEl = $($rows[$matchEl.length + 3]).find('td');
  const similarityTexts = $similarityEl.text().trim().split(' ');
  // ...

Edit: I just cross-checked with the code in the repository, it corresponds to line 214 in api.ts.

// dimensionTexts[1] is undefined, therefore .substring() causes a TypeError
const type = dimensionTexts[1].substring(1, dimensionTexts[1].length - 1).toLowerCase() 

As seen here, the dimensionTexts aren't in the format WidthXHeight [Type], but just [Type]:
error
while the code assumes that the result should look like this:
expected

Amazing library otherwise! Cheers.

search returning empty response

Hi, I came across this library and would love to use it. However, I'm running into issues when calling the search function:

import * as iqdb from '@l2studio/iqdb-api'
import * as fs from 'fs';
import * as process from 'process';

try {
	var Response = iqdb.search(fs.readFileSync('Check/test.jpg'));
}
catch(e){
	console.error(e.message);
}

console.log(`${Response.searched}`);
const newData = { Response }
console.log(JSON.stringify(newData));

returns

undefined
{"Response":{}}

Can you spot any issues with my code that makes the function call not work? I'm fairly new to scripting languages so I'm not sure what's going wrong.

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.