Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@himeka/booru

asadahimeka108MIT2.7.7TypeScript support: included

Search (and do other things) on a bunch of different boorus!

booru, e621, hypnohub, danbooru, konachan, yandere, gelbooru, rule34, safebooru, lolibooru, sakugabooru, 3dbooru, tbib, xbooru, paheal, derpibooru

readme

booru

npm GitHub Typescript typings

English | 中文

A node package to search boorus

Booru 站点图片搜索 API

Forked from AtoraSuunva/booru

特性

  • 搜索 20 个不同的 booru 站点(见 sites.json
  • 将所有接收到的数据标准化为一致的 Post 对象
  • 也可以访问从 booru 接收的原始数据(从 XML 转换为 JSON,如果适用)
  • 对 boorus 的别名支持(sb -> safebooru.org
  • Promises
  • 类型(使用 Typescript)
  • 选择要获取的图像数量
  • 支持随机作品,在支持它的站点上使用 order:random,在不支持它的站点上使用自定义代码
  • 支持 Node.js 与浏览器(在不跨域的情况下)

安装

npm i @himeka/booru
# 或
yarn add @himeka/booru

使用

const Booru = require('@himeka/booru')

Booru.search('safebooru', ['glaceon'], { limit: 3, random: true }).then(
  posts => {
    for (let post of posts) console.log(post.fileUrl, post.postView)
  },
)

// or (using alias support and creating boorus)
const sb = Booru.forSite('sb')

sb.search(['cat', 'dog'], { limit: 2 })

详见 example.js


文档

点击查看: docs

浏览器支持

booru 主要是为 Node.js 构建的,浏览器支持可能有限。 可以使用 webpack(或类似工具)在 web 上使用 booru,尽管体验可能会有所不同。 某些网站没有正确的 CORS 标头,这意味着从浏览器对这些网站的 API 请求将失败,需要自行解决跨域问题。

对于不支持 CORS 的站点,有如下两种方式解决

  • 定义全局方法 BOORU_FETCH_PROXY 来处理请求 URL,注意代码要放到调用 Booru.search 之前。
// 替换成你自己的 CORS 代理
globalThis.BOORU_FETCH_PROXY = u => `https://cors.example.com/${u}`
  • 或者可以按如下代码 hack 一下,注意这段代码要放到引用 @himeka/booru 之前。

下面例子的 CORS 代理需要支持 https://cors.example.com/https://konachan.net/post.json 形式的调用。

const _fetch = window.fetch
const proxy = 'https://cors.example.com/' // 替换成你自己的 CORS 代理
window.fetch = (input, init) => {
  let url = input.toString()
  if (url.startsWith('https')) { // 自行处理条件
    url = proxy + url // 自行处理为代理所需要的格式
  }
  return _fetch(url, init)
}

FAQ

Post 有什么属性?

Post 的基本结构如下:

Post {
  _data: {/*...*/},                       // 站点 API 返回的原始数据
  fileUrl: 'https://aaaa.com/img.jpg',    // 作品的原文件(大图)链接
  id: '124125',                           // 作品 ID
  tags: ['cat', 'cute'],                  // 作品标签,以数组表示
  score: 5,                               // 作品的评分
  source: 'https://ex.com/aaa.png',       // 作品的来源链接(Pixiv、Twitter 等,如果有的话)
  rating: 's',                            // 作品的分级(R18、存疑、安全)
  createdAt: Date,                        // 作品创建日期
  postView: 'https://booru.ex/show/12345' // 作品的站点详情页面
}

s: 'Safe' q: 'Questionable' e: 'Explicit' u: 'Unrated'

Derpibooru 会有 Safe, Suggestive, Questionable, Explicit, Suggestive 会在 <Post>.rating 显示为q

Why?

Why not?

License?

It's MIT

changelog

booru Changelog

v2.7.7

  • feat: support cors proxy using global method
  • fix: update api endpoint of rule34.paheal

v2.7.6

  • feat: support atfbooru
  • feat: support aibooru
  • feat: support credentials query
  • fix: use isomorphic-unfetch for browser support
  • chore: use tsup to build dist files
  • chore: update fast-xml-parser

v2.7.1

  • fix: use native fetch at browser
  • fix: danbooru use 720*720 preview_url
  • fix: change default page number to 1
  • fix: some boorus didnt get preview_url & sample_url
  • feat: support lolibooru
  • feat: support sakugabooru
  • feat: support 3dbooru

2.5.6

  • Update endpoints for Hypohub
  • Update tag-joining logic for Derpibooru (use , to join tags instead of +)
  • Fix regression that happened when the previous regression was fixed because programming
    • Both danbooru-based boorus and non-danbooru boorus should be fine now

2.5.5

  • Fix regression where previous fix for Danbooru broke some other boorus

2.5.4

  • Handle cases where some Danbooru-based boorus (like realbooru) return "directory": null in their API response and cause invalid URLs to be generated by booru
  • Slightly prettier example.js

2.5.3

  • Use encodeURIComponent instead of encodeURI for tags to properly encode them
  • Update some project files for more consistent development

2.5.2

  • Fixed parsing of gelbooru's API response

2.5.1

  • Broken version, deprecated and removed

2.5.0

  • Update API endpoint for r34.xxx, see https://github.com/AtoraSuunva/booru/pull/75
  • Add Booru#getSearchUrl, meant for aiding in debugging
    • You can use this to get the URL that booru would use to search, to inspect the response yourself
  • Minor changes to allow VS code's Run & Debug to work, kinda
    • Still needs some setup, and some refactoring to get working nicely

2.4.2

2.4.1

  • Moved from tslint (deprecated) -> eslint
  • Moved tslint style rules -> prettier for formatting
  • Updated dependencies
  • Moved from my old username to new username

2.4.0

  • Removed furry.booru.org since they have CloudFlare browser verification enabled.
    • As far as I know, there's no (intended) way to bypass this if you're not a browser.
    • Added CloudFlare-specific error message if this happens in the future
  • Add fix for Paheal changing their API response format
  • Make example.js only specify the "cat" default tag if you don't specify a site
  • Change from tsc -> typescript for package.json scripts, since tsc is deprecated.
  • Update dependencies

2.3.3

  • Fix Paheal failing to provide a useful error message.
    • Details: Paheal was returning an HTML error page instead of a JSON response. The previous way of scraping an error message off HTML pages failed on the page Paheal returned.
  • Move from terser-folder to a custom minify.js, which handles both .js and .json minifying
    • terser-folder also hasn't been updated in 2 years
  • Update circle ci to use node 14 instead of 11
  • Update dependencies, no more security vulnerabilities!

2.3.2

  • Added Post#available, to check if a post isn't deleted/banned
  • By default, unavailable posts aren't returned in search results
    • You can use SearchParameters#showUnavailable to still get them
    • Booru.search('db', ['cat'], { showUnavailable: true })
  • Fix for danbooru occasionally having invalid fileUrl or missing IDs
    • You can use Post#available to check for this

2.3.0

  • Fix for illegal invocation errors when using booru on the web
    • Some of the APIs don't have the required CORS headers however
  • Add encodeURI to tags when searching, to avoid encoding errors from the API
  • Better example.js file, now supports command line input
  • Remove lolibooru

2.2.3

  • Various fixes for Derpibooru support

2.2.2

  • Fix default tags missing from post results
  • Fix scores/sources/createdAt post props for some sites

2.2.1

  • Added missing check for results.posts in API response

2.2.0

  • Support for e621/e926's new api
  • Fix BooruError wiping out stack trace of caught error
  • Dependency updates

2.1.0

  • Update dependencies
  • Fix typo in "hypo" for hypnohub's aliases #42
  • Workaround for JS projects trying to use new booru(site) #40
    • Will still "work" for certain projects, but now booru.forSite(site) is preferred

2.0.5

  • Last release before changelog started