2020-06-24 13:02:34 +00:00
|
|
|
import asyncdispatch, httpclient, times, sequtils, strutils, json
|
2020-11-07 20:31:03 +00:00
|
|
|
import types, agents, consts, http_pool
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-07-09 07:18:14 +00:00
|
|
|
var
|
2020-11-07 20:31:03 +00:00
|
|
|
clientPool {.threadvar.}: HttpPool
|
2020-07-09 07:18:14 +00:00
|
|
|
tokenPool {.threadvar.}: seq[Token]
|
|
|
|
lastFailed: Time
|
|
|
|
minFail = initDuration(seconds=10)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc fetchToken(): Future[Token] {.async.} =
|
2020-07-09 07:18:14 +00:00
|
|
|
if getTime() - lastFailed < minFail:
|
|
|
|
return Token()
|
|
|
|
|
2020-11-07 20:31:03 +00:00
|
|
|
let headers = newHttpHeaders({
|
|
|
|
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
|
|
|
"accept-language": "en-US,en;q=0.5",
|
|
|
|
"connection": "keep-alive",
|
|
|
|
"user-agent": getAgent(),
|
|
|
|
"authorization": auth
|
|
|
|
})
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-24 13:02:34 +00:00
|
|
|
var
|
|
|
|
resp: string
|
|
|
|
tok: string
|
2020-06-02 18:37:55 +00:00
|
|
|
|
|
|
|
try:
|
2020-11-07 20:31:03 +00:00
|
|
|
resp = clientPool.use(headers): await c.postContent(activate)
|
2020-06-24 13:02:34 +00:00
|
|
|
tok = parseJson(resp)["guest_token"].getStr
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-24 13:02:34 +00:00
|
|
|
let time = getTime()
|
|
|
|
result = Token(tok: tok, remaining: 187, reset: time + 15.minutes,
|
|
|
|
init: time, lastUse: time)
|
|
|
|
except Exception as e:
|
2020-07-09 07:18:14 +00:00
|
|
|
lastFailed = getTime()
|
2020-06-24 13:02:34 +00:00
|
|
|
echo "fetching token failed: ", e.msg
|
2020-07-09 07:18:14 +00:00
|
|
|
result = Token()
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc expired(token: Token): bool {.inline.} =
|
2020-06-19 07:45:24 +00:00
|
|
|
const
|
|
|
|
expirationTime = 2.hours
|
|
|
|
maxLastUse = 1.hours
|
|
|
|
let time = getTime()
|
|
|
|
result = token.init < time - expirationTime or
|
|
|
|
token.lastUse < time - maxLastUse
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc isLimited(token: Token): bool {.inline.} =
|
2020-06-06 02:39:22 +00:00
|
|
|
token == nil or (token.remaining <= 1 and token.reset > getTime()) or
|
2020-06-01 00:16:24 +00:00
|
|
|
token.expired
|
|
|
|
|
|
|
|
proc release*(token: Token) =
|
|
|
|
if token != nil and not token.expired:
|
2020-06-19 07:45:24 +00:00
|
|
|
token.lastUse = getTime()
|
2020-06-01 00:16:24 +00:00
|
|
|
tokenPool.insert(token)
|
|
|
|
|
|
|
|
proc getToken*(): Future[Token] {.async.} =
|
|
|
|
for i in 0 ..< tokenPool.len:
|
|
|
|
if not result.isLimited: break
|
|
|
|
result.release()
|
|
|
|
result = tokenPool.pop()
|
|
|
|
|
|
|
|
if result.isLimited:
|
|
|
|
result.release()
|
|
|
|
result = await fetchToken()
|
|
|
|
|
|
|
|
proc poolTokens*(amount: int) {.async.} =
|
|
|
|
var futs: seq[Future[Token]]
|
|
|
|
for i in 0 ..< amount:
|
|
|
|
futs.add fetchToken()
|
|
|
|
|
|
|
|
for token in futs:
|
|
|
|
release(await token)
|
|
|
|
|
|
|
|
proc initTokenPool*(cfg: Config) {.async.} =
|
2020-11-07 20:31:03 +00:00
|
|
|
clientPool = HttpPool()
|
|
|
|
|
2020-06-01 00:16:24 +00:00
|
|
|
while true:
|
2020-06-01 11:40:26 +00:00
|
|
|
if tokenPool.countIt(not it.isLimited) < cfg.minTokens:
|
2020-06-01 11:54:45 +00:00
|
|
|
await poolTokens(min(4, cfg.minTokens - tokenPool.len))
|
|
|
|
await sleepAsync(2000)
|