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