2023-08-29 23:45:18 +02:00
|
|
|
#SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import asyncdispatch, times, json, random, strutils, tables, sets
|
2023-08-19 00:25:14 +02:00
|
|
|
import types
|
2020-06-01 02:16:24 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
# max requests at a time per account to avoid race conditions
|
2023-08-20 11:56:42 +02:00
|
|
|
const
|
2023-08-22 01:32:09 +02:00
|
|
|
maxConcurrentReqs = 2
|
2023-08-20 11:56:42 +02:00
|
|
|
dayInSeconds = 24 * 60 * 60
|
2021-01-18 07:47:51 +01:00
|
|
|
|
2020-07-09 09:18:14 +02:00
|
|
|
var
|
2023-08-19 00:25:14 +02:00
|
|
|
accountPool: seq[GuestAccount]
|
2022-06-05 21:47:25 +02:00
|
|
|
enableLogging = false
|
|
|
|
|
|
|
|
template log(str) =
|
2023-08-19 00:25:14 +02:00
|
|
|
if enableLogging: echo "[accounts] ", str
|
2021-01-13 14:32:26 +01:00
|
|
|
|
2022-01-06 00:42:18 +01:00
|
|
|
proc getPoolJson*(): JsonNode =
|
|
|
|
var
|
|
|
|
list = newJObject()
|
|
|
|
totalReqs = 0
|
|
|
|
totalPending = 0
|
2023-08-29 23:45:18 +02:00
|
|
|
limited: HashSet[string]
|
2022-01-06 00:42:18 +01:00
|
|
|
reqsPerApi: Table[string, int]
|
|
|
|
|
2023-08-20 11:56:42 +02:00
|
|
|
let now = epochTime().int
|
2023-08-19 01:13:36 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
for account in accountPool:
|
|
|
|
totalPending.inc(account.pending)
|
2023-08-29 23:45:18 +02:00
|
|
|
|
|
|
|
var includeAccount = false
|
|
|
|
let accountJson = %*{
|
2022-01-05 22:49:16 +01:00
|
|
|
"apis": newJObject(),
|
2023-08-19 00:25:14 +02:00
|
|
|
"pending": account.pending,
|
2022-01-05 22:49:16 +01:00
|
|
|
}
|
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
for api in account.apis.keys:
|
2023-08-22 03:43:18 +02:00
|
|
|
let
|
|
|
|
apiStatus = account.apis[api]
|
|
|
|
obj = %*{}
|
2023-08-20 11:56:42 +02:00
|
|
|
|
2023-08-22 03:43:18 +02:00
|
|
|
if apiStatus.reset > now.int:
|
|
|
|
obj["remaining"] = %apiStatus.remaining
|
2023-08-19 01:13:36 +02:00
|
|
|
|
2023-08-22 03:43:18 +02:00
|
|
|
if "remaining" notin obj and not apiStatus.limited:
|
2023-08-20 11:56:42 +02:00
|
|
|
continue
|
2022-01-06 00:42:18 +01:00
|
|
|
|
2023-08-29 23:45:18 +02:00
|
|
|
if apiStatus.limited:
|
|
|
|
obj["limited"] = %true
|
|
|
|
limited.incl account.id
|
|
|
|
|
|
|
|
accountJson{"apis", $api} = obj
|
|
|
|
includeAccount = true
|
2023-08-22 03:43:18 +02:00
|
|
|
|
2022-01-06 00:42:18 +01:00
|
|
|
let
|
|
|
|
maxReqs =
|
|
|
|
case api
|
2023-08-19 00:25:14 +02:00
|
|
|
of Api.search: 50
|
2023-08-25 16:28:30 +02:00
|
|
|
of Api.tweetDetail: 150
|
2023-07-22 04:06:04 +02:00
|
|
|
of Api.photoRail: 180
|
2023-08-19 00:25:14 +02:00
|
|
|
of Api.userTweets, Api.userTweetsAndReplies, Api.userMedia,
|
2023-08-22 04:45:49 +02:00
|
|
|
Api.userRestId, Api.userScreenName,
|
2023-08-25 16:28:30 +02:00
|
|
|
Api.tweetResult,
|
2023-08-19 00:25:14 +02:00
|
|
|
Api.list, Api.listTweets, Api.listMembers, Api.listBySlug: 500
|
2023-08-22 03:43:18 +02:00
|
|
|
of Api.userSearch: 900
|
|
|
|
reqs = maxReqs - apiStatus.remaining
|
2022-01-06 00:42:18 +01:00
|
|
|
|
|
|
|
reqsPerApi[$api] = reqsPerApi.getOrDefault($api, 0) + reqs
|
|
|
|
totalReqs.inc(reqs)
|
|
|
|
|
2023-08-29 23:45:18 +02:00
|
|
|
if includeAccount:
|
|
|
|
list[account.id] = accountJson
|
|
|
|
|
2022-01-06 00:42:18 +01:00
|
|
|
return %*{
|
2023-08-19 00:25:14 +02:00
|
|
|
"amount": accountPool.len,
|
2023-08-29 23:45:18 +02:00
|
|
|
"limited": limited.card,
|
2022-01-06 00:42:18 +01:00
|
|
|
"requests": totalReqs,
|
|
|
|
"pending": totalPending,
|
|
|
|
"apis": reqsPerApi,
|
2023-08-19 00:25:14 +02:00
|
|
|
"accounts": list
|
2022-01-06 00:42:18 +01:00
|
|
|
}
|
2021-01-13 14:32:26 +01:00
|
|
|
|
|
|
|
proc rateLimitError*(): ref RateLimitError =
|
2022-01-05 22:48:45 +01:00
|
|
|
newException(RateLimitError, "rate limited")
|
2020-06-01 02:16:24 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
proc isLimited(account: GuestAccount; api: Api): bool =
|
|
|
|
if account.isNil:
|
2022-01-05 22:48:45 +01:00
|
|
|
return true
|
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
if api in account.apis:
|
|
|
|
let limit = account.apis[api]
|
2023-08-20 11:56:42 +02:00
|
|
|
|
|
|
|
if limit.limited and (epochTime().int - limit.limitedAt) > dayInSeconds:
|
|
|
|
account.apis[api].limited = false
|
2023-08-21 18:12:06 +02:00
|
|
|
log "resetting limit, api: " & $api & ", id: " & $account.id
|
2023-08-20 11:56:42 +02:00
|
|
|
|
|
|
|
return limit.limited or (limit.remaining <= 10 and limit.reset > epochTime().int)
|
2022-01-05 22:48:45 +01:00
|
|
|
else:
|
|
|
|
return false
|
2020-06-01 02:16:24 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
proc isReady(account: GuestAccount; api: Api): bool =
|
|
|
|
not (account.isNil or account.pending > maxConcurrentReqs or account.isLimited(api))
|
2022-01-05 23:38:46 +01:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
proc release*(account: GuestAccount; used=false; invalid=false) =
|
|
|
|
if account.isNil: return
|
|
|
|
if invalid:
|
|
|
|
log "discarding invalid account: " & account.id
|
2022-06-05 21:47:25 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
let idx = accountPool.find(account)
|
|
|
|
if idx > -1: accountPool.delete(idx)
|
2022-01-05 23:38:46 +01:00
|
|
|
elif used:
|
2023-08-19 00:25:14 +02:00
|
|
|
dec account.pending
|
2020-06-01 02:16:24 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
proc getGuestAccount*(api: Api): Future[GuestAccount] {.async.} =
|
|
|
|
for i in 0 ..< accountPool.len:
|
2022-01-05 23:38:46 +01:00
|
|
|
if result.isReady(api): break
|
2021-01-18 07:47:51 +01:00
|
|
|
release(result)
|
2023-08-19 00:25:14 +02:00
|
|
|
result = accountPool.sample()
|
2021-01-13 14:32:26 +01:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
if not result.isNil and result.isReady(api):
|
2022-01-05 23:38:46 +01:00
|
|
|
inc result.pending
|
|
|
|
else:
|
2023-08-19 00:25:14 +02:00
|
|
|
log "no accounts available for API: " & $api
|
2021-01-13 14:32:26 +01:00
|
|
|
raise rateLimitError()
|
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
proc setRateLimit*(account: GuestAccount; api: Api; remaining, reset: int) =
|
2022-01-05 23:38:46 +01:00
|
|
|
# avoid undefined behavior in race conditions
|
2023-08-19 00:25:14 +02:00
|
|
|
if api in account.apis:
|
|
|
|
let limit = account.apis[api]
|
2022-01-05 23:38:46 +01:00
|
|
|
if limit.reset >= reset and limit.remaining < remaining:
|
|
|
|
return
|
2023-08-19 00:25:14 +02:00
|
|
|
if limit.reset == reset and limit.remaining >= remaining:
|
|
|
|
account.apis[api].remaining = remaining
|
|
|
|
return
|
2022-01-05 23:38:46 +01:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
account.apis[api] = RateLimit(remaining: remaining, reset: reset)
|
2020-06-01 02:16:24 +02:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
proc initAccountPool*(cfg: Config; accounts: JsonNode) =
|
2022-06-05 21:47:25 +02:00
|
|
|
enableLogging = cfg.enableDebug
|
2020-11-07 21:31:03 +01:00
|
|
|
|
2023-08-19 00:25:14 +02:00
|
|
|
for account in accounts:
|
|
|
|
accountPool.add GuestAccount(
|
|
|
|
id: account{"user", "id_str"}.getStr,
|
|
|
|
oauthToken: account{"oauth_token"}.getStr,
|
|
|
|
oauthSecret: account{"oauth_token_secret"}.getStr,
|
|
|
|
)
|