Skip to content

Commit

Permalink
fixed token issue that broke all pages besides the favorites / likes …
Browse files Browse the repository at this point in the history
…timeline
  • Loading branch information
PrivacyDevel committed Apr 5, 2023
1 parent 7d2a558 commit a6dd229
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ proc getFavorites*(id: string; cfg: Config; after=""): Future[Timeline] {.async.
let
ps = genParams({"userId": id}, after)
url = consts.favorites / (id & ".json") ? ps
headers = genHeaders()
headers.add("Cookie", cfg.cookieHeader)
headers.add("x-csrf-token", cfg.xCsrfToken)
headers = newHttpHeaders({
"Cookie": cfg.cookieHeader,
"x-csrf-token": cfg.xCsrfToken
})
result = parseTimeline(await fetch(url, Api.favorites, headers), after)

proc getMediaTimeline*(id: string; after=""): Future[Timeline] {.async.} =
Expand Down
13 changes: 8 additions & 5 deletions src/apiutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ template updateToken() =
reset = parseInt(resp.headers[rlReset])
token.setRateLimit(api, remaining, reset)

template fetchImpl(result, headers, fetchBody) {.dirty.} =
template fetchImpl(result, additional_headers, fetchBody) {.dirty.} =
once:
pool = HttpPool()

Expand All @@ -60,6 +60,9 @@ template fetchImpl(result, headers, fetchBody) {.dirty.} =

try:
var resp: AsyncResponse
var headers = genHeaders(token)
for key, value in additional_headers.pairs():
headers.add(key, value)
pool.use(headers):
template getContent =
resp = await c.get($url)
Expand Down Expand Up @@ -96,9 +99,9 @@ template fetchImpl(result, headers, fetchBody) {.dirty.} =
release(token, invalid=true)
raise rateLimitError()

proc fetch*(url: Uri; api: Api; headers: HttpHeaders = genHeaders()): Future[JsonNode] {.async.} =
proc fetch*(url: Uri; api: Api; additional_headers: HttpHeaders = newHttpHeaders()): Future[JsonNode] {.async.} =
var body: string
fetchImpl(body, headers):
fetchImpl(body, additional_headers):
if body.startsWith('{') or body.startsWith('['):
result = parseJson(body)
else:
Expand All @@ -113,8 +116,8 @@ proc fetch*(url: Uri; api: Api; headers: HttpHeaders = genHeaders()): Future[Jso
release(token, invalid=true)
raise rateLimitError()

proc fetchRaw*(url: Uri; api: Api; headers: HttpHeaders = genHeaders()): Future[string] {.async.} =
fetchImpl(result, headers):
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)
Expand Down

0 comments on commit a6dd229

Please sign in to comment.