2021-12-27 01:37:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2023-08-20 09:56:42 +00:00
|
|
|
import httpclient, asyncdispatch, options, strutils, uri, times, math, tables
|
2023-08-18 22:25:14 +00:00
|
|
|
import jsony, packedjson, zippy, oauth1
|
2020-11-07 20:31:03 +00:00
|
|
|
import types, tokens, consts, parserutils, http_pool
|
2022-01-16 17:28:40 +00:00
|
|
|
import experimental/types/common
|
2020-06-15 14:40:27 +00:00
|
|
|
|
2022-01-05 21:48:45 +00:00
|
|
|
const
|
|
|
|
rlRemaining = "x-rate-limit-remaining"
|
|
|
|
rlReset = "x-rate-limit-reset"
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2022-01-02 10:21:03 +00:00
|
|
|
var pool: HttpPool
|
2020-11-07 20:31:03 +00:00
|
|
|
|
2022-01-06 02:57:14 +00:00
|
|
|
proc genParams*(pars: openArray[(string, string)] = @[]; cursor="";
|
2020-06-16 22:20:34 +00:00
|
|
|
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:
|
2023-04-21 12:41:30 +00:00
|
|
|
result &= ("include_ext_alt_text", "1")
|
2023-08-08 00:09:56 +00:00
|
|
|
result &= ("include_ext_media_stats", "1")
|
2023-04-21 12:41:30 +00:00
|
|
|
result &= ("include_ext_media_availability", "1")
|
2020-06-16 22:20:34 +00:00
|
|
|
if count.len > 0:
|
|
|
|
result &= ("count", count)
|
2021-12-28 07:07:15 +00:00
|
|
|
if cursor.len > 0:
|
|
|
|
# The raw cursor often has plus signs, which sometimes get turned into spaces,
|
2022-12-19 09:07:24 +00:00
|
|
|
# so we need to turn them back into a plus
|
2021-12-28 07:07:15 +00:00
|
|
|
if " " in cursor:
|
|
|
|
result &= ("cursor", cursor.replace(" ", "+"))
|
|
|
|
else:
|
|
|
|
result &= ("cursor", cursor)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc getOauthHeader(url, oauthToken, oauthTokenSecret: string): string =
|
|
|
|
let
|
|
|
|
encodedUrl = url.replace(",", "%2C").replace("+", "%20")
|
|
|
|
params = OAuth1Parameters(
|
|
|
|
consumerKey: consumerKey,
|
|
|
|
signatureMethod: "HMAC-SHA1",
|
|
|
|
timestamp: $int(round(epochTime())),
|
|
|
|
nonce: "0",
|
|
|
|
isIncludeVersionToHeader: true,
|
|
|
|
token: oauthToken
|
|
|
|
)
|
|
|
|
signature = getSignature(HttpGet, encodedUrl, "", params, consumerSecret, oauthTokenSecret)
|
|
|
|
|
|
|
|
params.signature = percentEncode(signature)
|
|
|
|
|
|
|
|
return getOauth1RequestHeader(params)["authorization"]
|
|
|
|
|
|
|
|
proc genHeaders*(url, oauthToken, oauthTokenSecret: string): HttpHeaders =
|
|
|
|
let header = getOauthHeader(url, oauthToken, oauthTokenSecret)
|
|
|
|
|
2020-06-01 00:16:24 +00:00
|
|
|
result = newHttpHeaders({
|
2020-11-07 20:31:03 +00:00
|
|
|
"connection": "keep-alive",
|
2023-08-18 22:25:14 +00:00
|
|
|
"authorization": header,
|
2020-06-01 00:16:24 +00:00
|
|
|
"content-type": "application/json",
|
|
|
|
"x-twitter-active-user": "yes",
|
|
|
|
"authority": "api.twitter.com",
|
2021-12-26 05:49:27 +00:00
|
|
|
"accept-encoding": "gzip",
|
2020-06-01 00:16:24 +00:00
|
|
|
"accept-language": "en-US,en;q=0.9",
|
|
|
|
"accept": "*/*",
|
2020-06-15 14:40:27 +00:00
|
|
|
"DNT": "1"
|
2020-06-01 00:16:24 +00:00
|
|
|
})
|
|
|
|
|
2022-01-16 05:00:11 +00:00
|
|
|
template fetchImpl(result, fetchBody) {.dirty.} =
|
2020-11-07 20:31:03 +00:00
|
|
|
once:
|
|
|
|
pool = HttpPool()
|
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
var account = await getGuestAccount(api)
|
|
|
|
if account.oauthToken.len == 0:
|
2021-01-07 21:31:29 +00:00
|
|
|
raise rateLimitError()
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
try:
|
2022-01-23 01:29:03 +00:00
|
|
|
var resp: AsyncResponse
|
2023-08-18 22:25:14 +00:00
|
|
|
pool.use(genHeaders($url, account.oauthToken, account.oauthSecret)):
|
2022-09-21 03:47:16 +00:00
|
|
|
template getContent =
|
|
|
|
resp = await c.get($url)
|
|
|
|
result = await resp.body
|
2022-01-21 08:17:18 +00:00
|
|
|
|
2022-09-21 03:47:16 +00:00
|
|
|
getContent()
|
|
|
|
|
2023-04-21 12:41:30 +00:00
|
|
|
if resp.status == $Http503:
|
|
|
|
badClient = true
|
|
|
|
raise newException(BadClientError, "Bad client")
|
2021-12-30 00:39:00 +00:00
|
|
|
|
2023-08-30 01:04:22 +00:00
|
|
|
if resp.headers.hasKey(rlRemaining):
|
|
|
|
let
|
|
|
|
remaining = parseInt(resp.headers[rlRemaining])
|
|
|
|
reset = parseInt(resp.headers[rlReset])
|
|
|
|
account.setRateLimit(api, remaining, reset)
|
|
|
|
|
2022-01-16 05:00:11 +00:00
|
|
|
if result.len > 0:
|
2021-12-30 00:39:00 +00:00
|
|
|
if resp.headers.getOrDefault("content-encoding") == "gzip":
|
2022-01-16 05:00:11 +00:00
|
|
|
result = uncompress(result, dfGzip)
|
2020-06-15 14:40:27 +00:00
|
|
|
|
2023-08-30 01:04:22 +00:00
|
|
|
if result.startsWith("{\"errors"):
|
|
|
|
let errors = result.fromJson(Errors)
|
|
|
|
if errors in {expiredToken, badToken}:
|
|
|
|
echo "fetch error: ", errors
|
|
|
|
invalidate(account)
|
|
|
|
raise rateLimitError()
|
|
|
|
elif errors in {rateLimited}:
|
|
|
|
# rate limit hit, resets after 24 hours
|
|
|
|
setLimited(account, api)
|
|
|
|
raise rateLimitError()
|
|
|
|
elif result.startsWith("429 Too Many Requests"):
|
2023-08-30 23:29:54 +00:00
|
|
|
echo "[accounts] 429 error, API: ", api, ", account: ", account.id
|
2023-08-30 01:04:22 +00:00
|
|
|
account.apis[api].remaining = 0
|
|
|
|
# rate limit hit, resets after the 15 minute window
|
|
|
|
raise rateLimitError()
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-30 01:04:22 +00:00
|
|
|
fetchBody
|
2021-12-28 04:41:41 +00:00
|
|
|
|
|
|
|
if resp.status == $Http400:
|
|
|
|
raise newException(InternalError, $url)
|
|
|
|
except InternalError as e:
|
|
|
|
raise e
|
2023-04-21 12:41:30 +00:00
|
|
|
except BadClientError as e:
|
2023-08-30 01:04:22 +00:00
|
|
|
raise e
|
|
|
|
except OSError as e:
|
2023-04-21 12:41:30 +00:00
|
|
|
raise e
|
2021-01-18 06:47:51 +00:00
|
|
|
except Exception as e:
|
2023-08-18 22:25:14 +00:00
|
|
|
echo "error: ", e.name, ", msg: ", e.msg, ", accountId: ", account.id, ", url: ", url
|
2021-01-07 21:31:29 +00:00
|
|
|
raise rateLimitError()
|
2023-08-30 01:04:22 +00:00
|
|
|
finally:
|
|
|
|
release(account)
|
2022-01-16 05:00:11 +00:00
|
|
|
|
|
|
|
proc fetch*(url: Uri; api: Api): Future[JsonNode] {.async.} =
|
|
|
|
var body: string
|
|
|
|
fetchImpl body:
|
|
|
|
if body.startsWith('{') or body.startsWith('['):
|
|
|
|
result = parseJson(body)
|
|
|
|
else:
|
2022-01-23 07:38:38 +00:00
|
|
|
echo resp.status, ": ", body, " --- url: ", url
|
2022-01-16 05:00:11 +00:00
|
|
|
result = newJNull()
|
|
|
|
|
|
|
|
let error = result.getError
|
2023-08-30 01:04:22 +00:00
|
|
|
if error in {expiredToken, badToken}:
|
|
|
|
echo "fetchBody error: ", error
|
|
|
|
invalidate(account)
|
2022-01-16 05:00:11 +00:00
|
|
|
raise rateLimitError()
|
|
|
|
|
|
|
|
proc fetchRaw*(url: Uri; api: Api): Future[string] {.async.} =
|
|
|
|
fetchImpl result:
|
|
|
|
if not (result.startsWith('{') or result.startsWith('[')):
|
2022-01-23 07:38:38 +00:00
|
|
|
echo resp.status, ": ", result, " --- url: ", url
|
2022-01-16 05:00:11 +00:00
|
|
|
result.setLen(0)
|