1
0
mirror of https://github.com/zedeus/nitter synced 2024-11-10 21:00:19 +01:00
nitter/src/tokens.nim

146 lines
3.9 KiB
Nim
Raw Normal View History

2023-08-29 23:45:18 +02:00
#SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, times, json, random, strutils, tables, sets
import types
2020-06-01 02:16:24 +02:00
# max requests at a time per account to avoid race conditions
const
maxConcurrentReqs = 2
dayInSeconds = 24 * 60 * 60
2020-07-09 09:18:14 +02:00
var
accountPool: seq[GuestAccount]
2022-06-05 21:47:25 +02:00
enableLogging = false
template log(str) =
if enableLogging: echo "[accounts] ", str
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]
let now = epochTime().int
for account in accountPool:
totalPending.inc(account.pending)
2023-08-29 23:45:18 +02: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-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
2022-01-06 00:42:18 +01:00
let
maxReqs =
case api
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
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,
Api.list, Api.listTweets, Api.listMembers, Api.listBySlug: 500
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 %*{
"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,
"accounts": list
2022-01-06 00:42:18 +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
proc isLimited(account: GuestAccount; api: Api): bool =
if account.isNil:
2022-01-05 22:48:45 +01: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
2023-08-21 18:12:06 +02:00
log "resetting limit, api: " & $api & ", id: " & $account.id
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
proc isReady(account: GuestAccount; api: Api): bool =
not (account.isNil or account.pending > maxConcurrentReqs or account.isLimited(api))
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
let idx = accountPool.find(account)
if idx > -1: accountPool.delete(idx)
elif used:
dec account.pending
2020-06-01 02:16:24 +02: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 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 02:16:24 +02:00
proc initAccountPool*(cfg: Config; accounts: JsonNode) =
2022-06-05 21:47:25 +02: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,
)