|
|
@ -13,10 +13,17 @@ function getRandomInt(min, max) { |
|
|
|
|
|
|
|
class SzurubooruClient { |
|
|
|
|
|
|
|
_randomSearchQueryValues = "type:image,animated,video sort:random"; |
|
|
|
_randomSearchUrlTemplate = "%s/posts/?offset=%s&limit=%d&query=%s"; |
|
|
|
url: string; |
|
|
|
baseUrl: string; |
|
|
|
username: string; |
|
|
|
token: string; |
|
|
|
|
|
|
|
constructor(url, username, token) { |
|
|
|
randomImageCount: number; |
|
|
|
|
|
|
|
_randomSearchQueryValues: string = "type:image,animated,video sort:random"; |
|
|
|
_randomSearchUrlTemplate: string = "%s/posts/?offset=%s&limit=%d&query=%s"; |
|
|
|
|
|
|
|
constructor(url: string, username: string, token: string) { |
|
|
|
this.url = url; |
|
|
|
this.baseUrl = url.replace('/api', ''); |
|
|
|
this.username = username; |
|
|
@ -24,7 +31,7 @@ class SzurubooruClient { |
|
|
|
this.updateRandomScope(); |
|
|
|
} |
|
|
|
|
|
|
|
getRandomSearchUrl(offset, limit, tags = []) { |
|
|
|
getRandomSearchUrl(offset: number, limit: number, tags: Array<string> = []) { |
|
|
|
var searchQuery = this._randomSearchQueryValues; |
|
|
|
if (tags !== null && tags.length > 0) { |
|
|
|
searchQuery = this._randomSearchQueryValues + tags.join(" "); |
|
|
@ -38,7 +45,7 @@ class SzurubooruClient { |
|
|
|
* @param {*} limit The content limit of a single page |
|
|
|
* @param {*} total The number of objects to search through |
|
|
|
*/ |
|
|
|
getValidOffsetRange(limit, total) { |
|
|
|
getValidOffsetRange(limit: number, total: number): Array<number> { |
|
|
|
let offsetOverflow = total % limit; |
|
|
|
var offsetCeiling = (Math.round(total / limit)); |
|
|
|
if (offsetOverflow === 0) { |
|
|
@ -58,12 +65,12 @@ class SzurubooruClient { |
|
|
|
* @param {*} self |
|
|
|
* @param {*} imageCount |
|
|
|
*/ |
|
|
|
getRandomSearchParameters(imageCount = null) { |
|
|
|
getRandomSearchParameters(imageCount?: number) { |
|
|
|
let resultLimit = 10; |
|
|
|
var offsetRange = [0, 1]; |
|
|
|
if (imageCount === null) { |
|
|
|
offsetRange = this.getValidOffsetRange(resultLimit, this.randomImageCount); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
offsetRange = this.getValidOffsetRange(resultLimit, imageCount); |
|
|
|
} |
|
|
|
return [resultLimit, getRandomInt(offsetRange[0], offsetRange[1])]; |
|
|
@ -73,27 +80,26 @@ class SzurubooruClient { |
|
|
|
async updateRandomScope() { |
|
|
|
let client = this; |
|
|
|
let target = this.getRandomSearchUrl(0, 1); |
|
|
|
await this.makeGetRequest(target).then((result) => { |
|
|
|
await this.makeGetRequest(target).then((result: any) => { |
|
|
|
client.randomImageCount = result.total; |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
makeGetRequest(url) { |
|
|
|
makeGetRequest(url: string) { |
|
|
|
logger.debug("Making get request to %s", url); |
|
|
|
return axios.get(url, { |
|
|
|
headers: { |
|
|
|
"Authorization": "Token " + Buffer.from(this.username + ':' + this.token).toString('base64'), |
|
|
|
"Accept": "application/json" |
|
|
|
} |
|
|
|
}).then((response) => { |
|
|
|
}).then((response: any) => { |
|
|
|
logger.debug("Recieved: %o", response.data); |
|
|
|
return response.data; |
|
|
|
}, (err) => { |
|
|
|
}, (err: any) => { |
|
|
|
logger.error("Unexpected client error: %o", err); |
|
|
|
return null; |
|
|
|
}); |
|
|
|
return self._handle_response("GET", url, None, response) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -102,7 +108,7 @@ class SzurubooruClient { |
|
|
|
* @param {*} self |
|
|
|
* @param {*} tags |
|
|
|
*/ |
|
|
|
getRandomPost(tags = []) { |
|
|
|
getRandomPost(tags: Array<string> = []) { |
|
|
|
let client = this; |
|
|
|
var resultLimit = 0; |
|
|
|
var randomSearchOffset = 0; |
|
|
@ -113,7 +119,7 @@ class SzurubooruClient { |
|
|
|
} |
|
|
|
let searchUrl = this.getRandomSearchUrl(randomSearchOffset, resultLimit, tags); |
|
|
|
return this.makeGetRequest(searchUrl) |
|
|
|
.then((data) => { |
|
|
|
.then((data: any) => { |
|
|
|
if (data !== null) { |
|
|
|
let limitSpace = Math.min(resultLimit, data.results.length - 1); |
|
|
|
let randomPost = data.results[getRandomInt(0, limitSpace)]; |
|
|
@ -121,10 +127,11 @@ class SzurubooruClient { |
|
|
|
} else { |
|
|
|
return null |
|
|
|
} |
|
|
|
}, (err) => { |
|
|
|
|
|
|
|
}, (err: any) => { |
|
|
|
logger.error("Unexpected error: %o", err); |
|
|
|
return null; |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exports.Client = SzurubooruClient; |
|
|
|
export { SzurubooruClient as Client }; |