mirror of
https://github.com/zedeus/nitter
synced 2024-11-26 03:29:14 +01:00
Add temporary token fail safe
This commit is contained in:
parent
7b40c8ebd5
commit
514d1b3121
@ -29,10 +29,11 @@ proc genHeaders*(token: Token = nil): HttpHeaders =
|
||||
})
|
||||
|
||||
proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
|
||||
var
|
||||
token = await getToken()
|
||||
client = newAsyncHttpClient(headers=genHeaders(token))
|
||||
var token = await getToken()
|
||||
if token.tok.len == 0:
|
||||
result = newJNull()
|
||||
|
||||
var client = newAsyncHttpClient(headers=genHeaders(token))
|
||||
try:
|
||||
let
|
||||
resp = await client.get($url)
|
||||
@ -54,5 +55,4 @@ proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
|
||||
echo "error: ", url
|
||||
result = newJNull()
|
||||
finally:
|
||||
try: client.close()
|
||||
except: discard
|
||||
client.close()
|
||||
|
@ -1,9 +1,15 @@
|
||||
import asyncdispatch, httpclient, times, sequtils, strutils, json
|
||||
import types, agents, consts
|
||||
|
||||
var tokenPool {.threadvar.}: seq[Token]
|
||||
var
|
||||
tokenPool {.threadvar.}: seq[Token]
|
||||
lastFailed: Time
|
||||
minFail = initDuration(seconds=10)
|
||||
|
||||
proc fetchToken(): Future[Token] {.async.} =
|
||||
if getTime() - lastFailed < minFail:
|
||||
return Token()
|
||||
|
||||
let
|
||||
headers = newHttpHeaders({
|
||||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
||||
@ -26,8 +32,9 @@ proc fetchToken(): Future[Token] {.async.} =
|
||||
result = Token(tok: tok, remaining: 187, reset: time + 15.minutes,
|
||||
init: time, lastUse: time)
|
||||
except Exception as e:
|
||||
lastFailed = getTime()
|
||||
echo "fetching token failed: ", e.msg
|
||||
return Token()
|
||||
result = Token()
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user