forked from AtoraSuunva/booru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (81 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//@ts-check
// Declare the dependencies
const snekfetch = require('snekfetch')
const Constants = require('./src/Constants.js')
const { BooruError } = Constants
const Utils = require('./src/Utils.js')
const Booru = require('./src/boorus/Booru.js')
const Post = require('./src/structures/Post.js')
const SearchResults = require('./src/structures/SearchResults.js')
const BooruTypes = {
'xml': require('./src/boorus/XmlBooru.js'),
'derpi': require('./src/boorus/Derpibooru.js'),
}
/**
* Create a new booru to search with
* @private
* @constructor
* @param {String} site The {@link Site} (or alias of it) to create a booru from
* @param {*} credentials The credentials to use on this booru
* @return {Booru} A booru to use
*/
function _Booru(site, credentials = null) {
const rSite = Utils.resolveSite(site)
if (rSite === null) {
throw new BooruError('Site not supported')
}
const booruSite = Constants.sites[rSite]
// If special type, use that booru, else use default Booru
return new (BooruTypes[booruSite.type] || Booru)(booruSite, credentials)
}
const booruCache = {}
/**
* Searches a site for images with tags and returns the results
* @param {String} site The site to search
* @param {String[]} [tags=[]] Tags to search with
* @param {Object} [searchOptions={}] The options for searching
* @param {Number|String} [searchOptions.limit=1] The limit of images to return
* @param {Boolean} [searchOptions.random=false] If it should grab randomly sorted results
* @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, credentials = false} = {}) {
const rSite = Utils.resolveSite(site)
limit = parseInt(limit)
if (rSite === null) {
throw new BooruError('Site not supported')
}
if (!Array.isArray(tags) && typeof tags !== 'string') {
throw new BooruError('`tags` should be an array or string')
}
if (typeof limit !== 'number' || Number.isNaN(limit)) {
throw new BooruError('`limit` should be an int')
}
// Too lazy to remove validation (for now)
const booruSite = Constants.sites[rSite]
if (!booruCache[rSite]) {
booruCache[rSite] = new (BooruTypes[booruSite.type] || Booru)(booruSite)
}
return booruCache[rSite].search(tags, {limit, random, credentials})
}
/**
* Deprecated, now a noop
* Just access <{@link BooruImage}>.common and it'll be auto-generated as you get it
*
* @deprecated Just use <{@link BooruImage}>.common instead
* @param {*} images Array of {@link BooruImage} objects
* @return {*} Array of {@link BooruLink} objects
*/
function commonfy(images) {
return Promise.resolve(images)
}
module.exports = _Booru
module.exports.search = search
module.exports.commonfy = commonfy // do nothing
module.exports.sites = Constants.sites // Sites in case you want to see what it supports
module.exports.resolveSite = Utils.resolveSite // might as well /shrug
// coding is fun :)