booru
Version:
Search (and do other things) on a bunch of different boorus!
96 lines • 2.95 kB
JavaScript
;
/**
* @packageDocumentation
* @module Constants
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultOptions = exports.USER_AGENT = exports.BooruError = exports.sites = void 0;
exports.searchURI = searchURI;
exports.tagListURI = tagListURI;
const Utils_1 = require("./Utils");
const siteJson = require('./sites.json');
const packageJson = require('../package.json');
const expandedTags = {
'rating:e': 'rating:explicit',
'rating:q': 'rating:questionable',
'rating:s': 'rating:safe',
};
/**
* A map of site url/{@link SiteInfo}
*/
exports.sites = siteJson;
/**
* Custom error type for when the boorus error or for user-side error, not my code (probably)
* <p>The name of the error is 'BooruError'
* @type {Error}
*/
class BooruError extends Error {
constructor(message) {
super(message instanceof Error ? message.message : message);
if (message instanceof Error) {
this.stack = message.stack ?? '<No Stack>';
}
else {
Error.captureStackTrace(this, BooruError);
}
this.name = 'BooruError';
}
}
exports.BooruError = BooruError;
/**
* The user-agent to use for searches
* @private
*/
exports.USER_AGENT = `booru/${packageJson.version} (+https://github.com/AtoraSuunva/booru)`;
/**
* Expands tags based on a simple map, used for gelbooru/safebooru/etc compat :(
*
* @private
* @param {String[]} tags The tags to expand
*/
function expandTags(tags) {
return tags.map((v) => expandedTags[v.toLowerCase()] ?? v);
}
/**
* Create a full uri to search with
*
* @private
* @param {string} domain The domain to search
* @param {Site} site The site to search
* @param {string[]} [tags=[]] The tags to search for
* @param {number} [limit=100] The limit for images to return
* @param {number} [page=0] The page to get
* @param {BooryCredentials} [credentials] The credentials to use for the search, appended to the querystring
*/
function searchURI(site, tags = [], limit = 100, page = 0, credentials = {}) {
const query = (0, Utils_1.querystring)({
[site.tagQuery]: expandTags(tags),
limit,
[site.paginate]: page,
...credentials,
}, {
arrayJoin: site.tagJoin,
});
return `http${site.insecure ? '' : 's'}://${site.domain}${site.api.search}${query}`;
}
function tagListURI(site, limit = 100, page = 0, credentials = {}) {
const query = (0, Utils_1.querystring)({
limit,
[site.paginate]: page,
...credentials,
});
return `http${site.insecure ? '' : 's'}://${site.domain}${site.api.tagList}${query}`;
}
/**
* The default options to use for requests
* <p>I could document this better but meh
*
* @private
*/
exports.defaultOptions = {
headers: {
Accept: 'application/json, application/xml;q=0.9, */*;q=0.8',
'User-Agent': exports.USER_AGENT,
},
};
//# sourceMappingURL=Constants.js.map