UNPKG

booru

Version:

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

146 lines 6.02 kB
"use strict"; /** * @packageDocumentation * @module Index */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.XmlBooru = exports.Site = exports.SearchResults = exports.Post = exports.Derpibooru = exports.resolveSite = exports.sites = exports.BooruError = exports.BooruClass = void 0; exports.forSite = booruForSite; exports.search = search; exports.tagList = tagList; exports.commonfy = commonfy; const node_util_1 = require("node:util"); const Booru_1 = __importDefault(require("./boorus/Booru")); const Derpibooru_1 = __importDefault(require("./boorus/Derpibooru")); exports.Derpibooru = Derpibooru_1.default; const XmlBooru_1 = __importDefault(require("./boorus/XmlBooru")); exports.XmlBooru = XmlBooru_1.default; const Constants_1 = require("./Constants"); const Post_1 = __importDefault(require("./structures/Post")); exports.Post = Post_1.default; const SearchResults_1 = __importDefault(require("./structures/SearchResults")); exports.SearchResults = SearchResults_1.default; const Site_1 = __importDefault(require("./structures/Site")); exports.Site = Site_1.default; const Utils_1 = require("./Utils"); const BooruTypes = { derpi: Derpibooru_1.default, xml: XmlBooru_1.default, }; const booruCache = {}; /** * Create a new booru, if special type, use that booru, else use default Booru * * @param booruSite The site to use * @param credentials The credentials to use, if any * @return A new booru */ function booruFrom(booruSite, credentials) { return new (booruSite.type !== undefined && BooruTypes[booruSite.type] ? BooruTypes[booruSite.type] : Booru_1.default)(booruSite, credentials); } /** * Create a new booru to search with * * @constructor * @param {String} site The {@link Site} domain (or alias of it) to create a booru from * @param {BooruCredentials} credentials The credentials to use on this booru * @return {Booru} A booru to use */ function booruForSite(site, credentials) { const rSite = (0, Utils_1.resolveSite)(site); if (!rSite) throw new Constants_1.BooruError('Site not supported'); const booruSite = new Site_1.default(Constants_1.sites[rSite]); // If special type, use that booru, else use default Booru return booruFrom(booruSite, credentials); } exports.default = booruForSite; /** * Searches a site for images with tags and returns the results * @param {String} site The site to search * @param {String[]|String} [tags=[]] Tags to search with * @param {SearchParameters} [searchOptions={}] The options for searching * if provided (Unused) * @return {Promise<SearchResults>} A promise with the images as an array of objects * * @example * ``` * const Booru = require('booru') * // Returns a promise with the latest cute glace pic from e926 * Booru.search('e926', ['glaceon', 'cute']) * ``` */ function search(site, tags = [], { limit = 1, random = false, page = 0, credentials = {} } = {}) { const rSite = (0, Utils_1.resolveSite)(site); if (typeof limit === 'string') { limit = Number.parseInt(limit, 10); } if (rSite === null) { throw new Constants_1.BooruError('Site not supported'); } if (!Array.isArray(tags) && typeof tags !== 'string') { throw new Constants_1.BooruError('`tags` should be an array or string'); } if (typeof limit !== 'number' || Number.isNaN(limit)) { throw new Constants_1.BooruError('`limit` should be an int'); } const booruSite = new Site_1.default(Constants_1.sites[rSite]); if (!booruCache[rSite]) { booruCache[rSite] = booruFrom(booruSite, credentials); } // This is ugly and a hack, I know this booruCache[rSite].credentials = credentials; return booruCache[rSite].search(tags, { limit, random, page }); } /** * Get a list of tags from a site * @param {String} site The site to get the tags from * @param {TagListParameters} [options={}] The options for the tag list * @return {Promise<TagListResults>} A promise with the tags as an array of objects * * @example * ``` * const Booru = require('booru') * // Returns a promise with the first 100 tags from e926 * Booru.tagList('e926') * ``` */ function tagList(site, { limit = 1, page = 0, credentials = {} } = {}) { const rSite = (0, Utils_1.resolveSite)(site); if (rSite === null) { throw new Constants_1.BooruError('Site not supported'); } const booruSite = new Site_1.default(Constants_1.sites[rSite]); if (!booruCache[rSite]) { booruCache[rSite] = booruFrom(booruSite, credentials); } booruCache[rSite].credentials = credentials; return booruCache[rSite].tagList({ limit, page }); } const deprecatedCommonfy = (0, node_util_1.deprecate)(() => { }, 'Common is now deprecated, just access the properties directly'); /** * Deprecated, now a noop * <p>This will be removed *soon* please stop using it</p> * <p>Just access <code>&lt;{@link Post}&gt;.prop</code>, no need to commonfy anymore * * @deprecated Just use <code>&lt;{@link Post}&gt;.prop</code> instead * @param {Post[]} images Array of {@link Post} objects * @return {Promise<Post[]>} Array of {@link Post} objects */ function commonfy(images) { deprecatedCommonfy(); return Promise.resolve(images); } var Booru_2 = require("./boorus/Booru"); Object.defineProperty(exports, "BooruClass", { enumerable: true, get: function () { return Booru_2.Booru; } }); var Constants_2 = require("./Constants"); Object.defineProperty(exports, "BooruError", { enumerable: true, get: function () { return Constants_2.BooruError; } }); Object.defineProperty(exports, "sites", { enumerable: true, get: function () { return Constants_2.sites; } }); var Utils_2 = require("./Utils"); Object.defineProperty(exports, "resolveSite", { enumerable: true, get: function () { return Utils_2.resolveSite; } }); //# sourceMappingURL=index.js.map