-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.mjs
77 lines (73 loc) · 2.8 KB
/
queue.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
import { config } from './config.mjs'
import log from './log.mjs'
import { util } from './util.mjs'
import { tag, randomEndpoint } from './tags.mjs'
import { cached } from './cache.mjs'
import { requestTag } from './verify.mjs'
/**
* add a random safe endpoint to the queue
* @param {Number} [attempts] - number of attempts to queue a new tag
*/
export async function randomQueueEndpoint (attempts = 0) {
if (tag.queue.length > 0 || tag.busy === true) return // we may get a request while running this function
const random = await randomEndpoint()
if (cached.g[random] == null) cached.g[random] = []
const limit = tag.limit.g[random] ?? tag.limit.g.default
if (cached.g[random].length <= limit) {
await queueTag(random, 'g')
} else if (attempts <= tag.limit.g.default) {
attempts++
await randomQueueEndpoint(attempts)
}
}
/**
* add specified endpoint with specified rating to the queue
* @param {String} endpoint
* @param {import('./type.mjs').rating} rating
*/
export async function queueTag (endpoint = 'foxgirl', rating = 'g') {
if (cached[rating][endpoint] == null) cached[rating][endpoint] = []
if (cached[rating][endpoint].length === 0) await addTag(endpoint, rating, true, false)
tag.queue.push([endpoint, rating])
log.debug(`Added to queue: ${endpoint}:${rating} #${cached[rating][endpoint].length}`)
if (tag.busy === false) requeueTag()
}
/**
* this function should loop until the queue is empty
*/
async function requeueTag () {
if (tag.queue.length === 0) {
tag.busy = false
return
}
if (tag.busy === true) return
tag.busy = true
if (cached.ratelimit === true) await util.sleep(config.delays.ratelimit)
const [endpoint, rating] = tag.queue.shift()
await addTag(endpoint, rating, true, false)
if (cached.delay > 0) {
await util.sleep(cached.delay)
cached.delay = Math.max(1000, cached.delay - 250)
} else if (cached.delay < 0) cached.delay = 0
tag.busy = false
const limit = tag.limit[rating][endpoint] ?? tag.limit[rating].default
if (tag.queue.length > 0) return await requeueTag()
else if (cached[rating][endpoint].length <= limit) return await queueTag(endpoint, rating)
else await randomQueueEndpoint()
}
/**
* request data with endpoint and rating
* @param {String} endpoint
* @param {import('./type.mjs').rating} rating - content rating
* @param {Boolean} toCache - add data to cache
* @param {Boolean} hd - get an HD image!
*/
export async function addTag (endpoint = 'foxgirl', rating = 'g', toCache = true, hd = false) {
if (cached.ratelimit === true) return null
log.debug(`Adding to cache: ${endpoint}:${rating}`)
const request = await requestTag(endpoint, rating, true, hd)
if (request == null) return null
log.debug(`Added! ${endpoint}:${rating}`)
if (toCache === true) cached[rating][endpoint].push(request)
return request
}