Merge remote-tracking branch 'upstream/guest_accounts'

This commit is contained in:
PrivacyDev 2023-09-15 16:32:53 -04:00
commit e4e7fe5f00
3 changed files with 32 additions and 19 deletions

View file

@ -134,11 +134,19 @@ template fetchImpl(result, additional_headers, 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)
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.} = proc fetch*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders()): Future[JsonNode] {.async.} =
if len(cfg.cookieHeader) != 0: if len(cfg.cookieHeader) != 0:
@ -146,22 +154,24 @@ proc fetch*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders
if len(cfg.xCsrfToken) != 0: if len(cfg.xCsrfToken) != 0:
additional_headers.add("x-csrf-token", cfg.xCsrfToken) additional_headers.add("x-csrf-token", cfg.xCsrfToken)
var body: string retry:
fetchImpl(body, additional_headers): var body: string
if body.startsWith('{') or body.startsWith('['): fetchImpl(body, additional_headers):
result = parseJson(body) if body.startsWith('{') or body.startsWith('['):
else: result = parseJson(body)
echo resp.status, ": ", body, " --- url: ", url else:
result = newJNull() echo resp.status, ": ", body, " --- url: ", url
result = newJNull()
let error = result.getError let error = result.getError
if error in {expiredToken, badToken}: if error in {expiredToken, badToken}:
echo "fetchBody error: ", error echo "fetchBody error: ", error
invalidate(account) invalidate(account)
raise rateLimitError() raise rateLimitError()
proc fetchRaw*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders()): Future[string] {.async.} = proc fetchRaw*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders()): Future[string] {.async.} =
fetchImpl(result, additional_headers): retry:
if not (result.startsWith('{') or result.startsWith('[')): fetchImpl(result, additional_headers):
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)

View file

@ -443,6 +443,9 @@ proc parseGraphTweet(js: JsonNode; isLegacy=false): Tweet =
of "TweetWithVisibilityResults": of "TweetWithVisibilityResults":
return parseGraphTweet(js{"tweet"}, isLegacy) return parseGraphTweet(js{"tweet"}, isLegacy)
if not js.hasKey("legacy"):
return Tweet()
var jsCard = copy(js{if isLegacy: "card" else: "tweet_card", "legacy"}) var jsCard = copy(js{if isLegacy: "card" else: "tweet_card", "legacy"})
if jsCard.kind != JNull: if jsCard.kind != JNull:
var values = newJObject() var values = newJObject()

View file

@ -37,7 +37,7 @@ proc proxyMedia*(req: jester.Request; url: string): Future[HttpCode] {.async.} =
try: try:
let res = await client.get(url) let res = await client.get(url)
if res.status != "200 OK": 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 return Http404
let hashed = $hash(url) let hashed = $hash(url)
@ -66,7 +66,7 @@ proc proxyMedia*(req: jester.Request; url: string): Future[HttpCode] {.async.} =
await request.client.send(data) await request.client.send(data)
data.setLen 0 data.setLen 0
except HttpRequestError, ProtocolError, OSError: 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 result = Http404
finally: finally:
client.close() client.close()