diff --git a/src/apiutils.nim b/src/apiutils.nim index 815a918..ab483dc 100644 --- a/src/apiutils.nim +++ b/src/apiutils.nim @@ -134,11 +134,19 @@ template fetchImpl(result, additional_headers, fetchBody) {.dirty.} = except OSError as e: raise 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() finally: release(account) +template retry(bod) = + try: + bod + except RateLimitError: + echo "[accounts] Rate limited, retrying ", api, " request..." + bod + proc fetch*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders()): Future[JsonNode] {.async.} = if len(cfg.cookieHeader) != 0: @@ -146,22 +154,24 @@ proc fetch*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders if len(cfg.xCsrfToken) != 0: additional_headers.add("x-csrf-token", cfg.xCsrfToken) - var body: string - fetchImpl(body, additional_headers): - if body.startsWith('{') or body.startsWith('['): - result = parseJson(body) - else: - echo resp.status, ": ", body, " --- url: ", url - result = newJNull() + retry: + var body: string + fetchImpl(body, additional_headers): + 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() + let error = result.getError + if error in {expiredToken, badToken}: + echo "fetchBody error: ", error + invalidate(account) + raise rateLimitError() proc fetchRaw*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders()): Future[string] {.async.} = - fetchImpl(result, additional_headers): - if not (result.startsWith('{') or result.startsWith('[')): - echo resp.status, ": ", result, " --- url: ", url - result.setLen(0) + retry: + fetchImpl(result, additional_headers): + if not (result.startsWith('{') or result.startsWith('[')): + echo resp.status, ": ", result, " --- url: ", url + result.setLen(0) diff --git a/src/parser.nim b/src/parser.nim index c77ee38..68ee078 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -443,6 +443,9 @@ proc parseGraphTweet(js: JsonNode; isLegacy=false): Tweet = of "TweetWithVisibilityResults": return parseGraphTweet(js{"tweet"}, isLegacy) + if not js.hasKey("legacy"): + return Tweet() + var jsCard = copy(js{if isLegacy: "card" else: "tweet_card", "legacy"}) if jsCard.kind != JNull: var values = newJObject() diff --git a/src/routes/media.nim b/src/routes/media.nim index d335c97..eacd1f8 100644 --- a/src/routes/media.nim +++ b/src/routes/media.nim @@ -37,7 +37,7 @@ proc proxyMedia*(req: jester.Request; url: string): Future[HttpCode] {.async.} = try: let res = await client.get(url) if res.status != "200 OK": - echo "[media] Proxying media failed, status: $1, url: $2, body: $3" % [res.status, url, await res.body] + echo "[media] Proxying failed, status: $1, url: $2" % [res.status, url] return Http404 let hashed = $hash(url) @@ -66,7 +66,7 @@ proc proxyMedia*(req: jester.Request; url: string): Future[HttpCode] {.async.} = await request.client.send(data) data.setLen 0 except HttpRequestError, ProtocolError, OSError: - echo "[media] Proxying media exception, error: $1, url: $2" % [getCurrentExceptionMsg(), url] + echo "[media] Proxying exception, error: $1, url: $2" % [getCurrentExceptionMsg(), url] result = Http404 finally: client.close()