mirror of
https://github.com/zedeus/nitter.git
synced 2024-12-14 12:06:29 +00:00
Retry rate limited requests with different account
This commit is contained in:
parent
4250245263
commit
fcd74e8048
1 changed files with 28 additions and 18 deletions
|
@ -120,28 +120,38 @@ template fetchImpl(result, fetchBody) {.dirty.} =
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise e
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
echo "error: ", e.name, ", msg: ", e.msg, ", accountId: ", account.id, ", url: ", url
|
let id = if account.isNil: "null" else: account.id
|
||||||
|
echo "error: ", e.name, ", msg: ", e.msg, ", accountId: ", id, ", url: ", url
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
finally:
|
finally:
|
||||||
release(account)
|
release(account)
|
||||||
|
|
||||||
proc fetch*(url: Uri; api: Api): Future[JsonNode] {.async.} =
|
template retry(bod) =
|
||||||
var body: string
|
try:
|
||||||
fetchImpl body:
|
bod
|
||||||
if body.startsWith('{') or body.startsWith('['):
|
except RateLimitError:
|
||||||
result = parseJson(body)
|
echo "[accounts] Rate limited, retrying ", api, " request..."
|
||||||
else:
|
bod
|
||||||
echo resp.status, ": ", body, " --- url: ", url
|
|
||||||
result = newJNull()
|
|
||||||
|
|
||||||
let error = result.getError
|
proc fetch*(url: Uri; api: Api): Future[JsonNode] {.async.} =
|
||||||
if error in {expiredToken, badToken}:
|
retry:
|
||||||
echo "fetchBody error: ", error
|
var body: string
|
||||||
invalidate(account)
|
fetchImpl body:
|
||||||
raise rateLimitError()
|
if body.startsWith('{') or body.startsWith('['):
|
||||||
|
result = parseJson(body)
|
||||||
|
else:
|
||||||
|
echo resp.status, ": ", body, " --- url: ", url
|
||||||
|
result = newJNull()
|
||||||
|
|
||||||
|
let error = result.getError
|
||||||
|
if error in {expiredToken, badToken}:
|
||||||
|
echo "fetchBody error: ", error
|
||||||
|
invalidate(account)
|
||||||
|
raise rateLimitError()
|
||||||
|
|
||||||
proc fetchRaw*(url: Uri; api: Api): Future[string] {.async.} =
|
proc fetchRaw*(url: Uri; api: Api): Future[string] {.async.} =
|
||||||
fetchImpl result:
|
retry:
|
||||||
if not (result.startsWith('{') or result.startsWith('[')):
|
fetchImpl result:
|
||||||
echo resp.status, ": ", result, " --- url: ", url
|
if not (result.startsWith('{') or result.startsWith('[')):
|
||||||
result.setLen(0)
|
echo resp.status, ": ", result, " --- url: ", url
|
||||||
|
result.setLen(0)
|
||||||
|
|
Loading…
Reference in a new issue