nitter/src/apiutils.nim

67 lines
1.8 KiB
Nim
Raw Normal View History

import httpclient, asyncdispatch, options, times, strutils, uri
import packedjson
import types, tokens, consts, parserutils, http_pool
const rl = "x-rate-limit-"
2020-06-01 00:16:24 +00:00
var pool {.threadvar.}: HttpPool
2020-06-16 22:20:34 +00:00
proc genParams*(pars: openarray[(string, string)] = @[]; cursor="";
count="20"; ext=true): seq[(string, string)] =
2020-06-01 00:16:24 +00:00
result = timelineParams
for p in pars:
result &= p
2020-06-16 22:20:34 +00:00
if ext:
result &= ("ext", "mediaStats")
2020-06-01 00:16:24 +00:00
if cursor.len > 0:
result &= ("cursor", cursor)
2020-06-16 22:20:34 +00:00
if count.len > 0:
result &= ("count", count)
2020-06-01 00:16:24 +00:00
proc genHeaders*(token: Token = nil): HttpHeaders =
2020-06-01 00:16:24 +00:00
result = newHttpHeaders({
"connection": "keep-alive",
2020-06-01 00:16:24 +00:00
"authorization": auth,
"content-type": "application/json",
"x-guest-token": if token == nil: "" else: token.tok,
"x-twitter-active-user": "yes",
"authority": "api.twitter.com",
"accept-language": "en-US,en;q=0.9",
"accept": "*/*",
"DNT": "1"
2020-06-01 00:16:24 +00:00
})
proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
once:
pool = HttpPool()
2020-07-09 07:18:14 +00:00
var token = await getToken()
if token.tok.len == 0:
2021-01-07 21:31:29 +00:00
raise rateLimitError()
2020-06-01 00:16:24 +00:00
let headers = genHeaders(token)
2020-06-01 00:16:24 +00:00
try:
let
resp = pool.use(headers): await c.get($url)
2020-06-01 00:16:24 +00:00
body = await resp.body
2020-06-16 22:20:34 +00:00
if body.startsWith('{') or body.startsWith('['):
result = parseJson(body)
else:
echo resp.status, ": ", body
result = newJNull()
if not oldApi and resp.headers.hasKey(rl & "reset"):
let time = fromUnix(parseInt(resp.headers[rl & "reset"]))
if token.reset != time:
token.remaining = parseInt(resp.headers[rl & "limit"])
token.reset = time
2020-06-01 00:16:24 +00:00
if result.getError notin {invalidToken, forbidden, badToken}:
token.lastUse = getTime()
else:
echo "fetch error: ", result.getError
except Exception:
2020-06-01 00:16:24 +00:00
echo "error: ", url
2021-01-07 21:31:29 +00:00
raise rateLimitError()