-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.mjs
94 lines (91 loc) · 2.89 KB
/
verify.mjs
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
import log from './log.mjs'
import { util } from './util.mjs'
import { cached } from './cache.mjs'
import { requestImage, downloadImage } from './danbooru.mjs'
import { randomTag, excludeTags } from './tags.mjs'
/**
*
* @param {String} endpoint - a valid endpoint
* @param {import('./type.mjs').rating} rating
* @param {Boolean} image - want an image?
* @param {Boolean} hd - or an HD image?
* @returns {Promise<import('./type.mjs').apiCombined>}
*/
export async function requestTag (endpoint = 'foxgirl', rating = 'g', image = true, hd = false) {
const tag = await randomTag(endpoint)
const request = await requestTagRaw(tag, rating, image, hd)
request.endpoint = endpoint
return request
}
/**
*
* @param {String} tag - a valid Danbooru tag
* @param {import('./type.mjs').rating} rating
* @param {Boolean} image - want an image?
* @param {Boolean} hd - or an HD image?
* @returns {Promise<import('./type.mjs').apiCombined>}
*/
export async function requestTagRaw (tag = 'fox_girl', rating = 'g', image = true, hd = false) {
const response = await requestImage(tag, rating)
// API has specifically failed
if (response === false) {
log.error('API request failed!')
if (cached.delay > 0) await util.sleep(cached.delay)
return await requestTagRaw(tag, rating, image, hd)
}
const excluded = await excludeTags(response.tags)
if (excluded.found === true) {
log.error(`Blacklisted tag: ${excluded.tag}`)
if (cached.delay > 0) await util.sleep(cached.delay)
return await requestTagRaw(tag, rating, image, hd)
}
if (image === false) {
const data = await newRequest(response, null, tag, rating)
return data
}
const url = hd === true ? response.urlhd : response.url
if (cached.delay > 0) await util.sleep(cached.delay)
const downloadedImage = await downloadImage(url)
if (downloadedImage === false) {
log.error('Image download failed!')
if (cached.delay > 0) await util.sleep(cached.delay)
return await requestTagRaw(tag, rating, image, hd)
}
const data = await newRequest(response, downloadedImage, tag, rating)
return data
}
/**
*
* @param {import('./type.mjs').apiRequest} response - api response
* @param {(import('./type.mjs').apiImage | null)} image - image response
* @param {String} tag - tag used
* @param {import('./type.mjs').rating} rating - content rating used
* @returns {Promise<import('./type.mjs').apiCombined>}
*/
async function newRequest (response, image, tag, rating) {
if (image == null) {
return {
raw: response.raw,
id: response.id,
tags: response.tags,
url: response.url,
urlhd: response.urlhd,
endpoint: tag,
tag,
rating
}
}
return {
raw: response.raw,
id: response.id,
tags: response.tags,
url: response.url,
urlhd: response.urlhd,
image: image.image,
mime: image.mime,
extension: image.extension,
endpoint: tag,
tag,
rating
}
}