nitter/src/tokens.nim

154 lines
4.3 KiB
Nim
Raw Normal View History

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