2021-12-27 01:37:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2022-01-23 06:04:50 +00:00
|
|
|
import asyncdispatch, httpclient, uri, strutils, sequtils, sugar
|
2020-06-02 14:22:44 +00:00
|
|
|
import packedjson
|
2020-06-01 00:16:24 +00:00
|
|
|
import types, query, formatters, consts, apiutils, parser
|
2022-01-26 20:05:23 +00:00
|
|
|
import experimental/parser as newParser
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-01-20 03:54:19 +00:00
|
|
|
proc getGraphUser*(username: string): Future[User] {.async.} =
|
|
|
|
if username.len == 0: return
|
|
|
|
let
|
|
|
|
variables = """{
|
|
|
|
"screen_name": "$1",
|
|
|
|
"withSafetyModeUserFields": false,
|
|
|
|
"withSuperFollowsUserFields": false
|
|
|
|
}""" % [username]
|
|
|
|
js = await fetchRaw(graphUser ? {"variables": variables}, Api.userScreenName)
|
|
|
|
result = parseGraphUser(js)
|
|
|
|
|
|
|
|
proc getGraphUserById*(id: string): Future[User] {.async.} =
|
2022-01-23 06:04:50 +00:00
|
|
|
if id.len == 0 or id.any(c => not c.isDigit): return
|
|
|
|
let
|
2023-01-20 03:54:19 +00:00
|
|
|
variables = """{"userId": "$1", "withSuperFollowsUserFields": true}""" % [id]
|
|
|
|
js = await fetchRaw(graphUserById ? {"variables": variables}, Api.userRestId)
|
2022-01-26 16:24:03 +00:00
|
|
|
result = parseGraphUser(js)
|
2022-01-23 06:04:50 +00:00
|
|
|
|
2021-10-02 08:13:56 +00:00
|
|
|
proc getGraphListBySlug*(name, list: string): Future[List] {.async.} =
|
2020-06-01 00:16:24 +00:00
|
|
|
let
|
|
|
|
variables = %*{"screenName": name, "listSlug": list, "withHighlightedLabel": false}
|
2022-01-05 21:48:45 +00:00
|
|
|
url = graphListBySlug ? {"variables": $variables}
|
|
|
|
result = parseGraphList(await fetch(url, Api.listBySlug))
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2021-10-02 08:13:56 +00:00
|
|
|
proc getGraphList*(id: string): Future[List] {.async.} =
|
2020-06-01 00:16:24 +00:00
|
|
|
let
|
|
|
|
variables = %*{"listId": id, "withHighlightedLabel": false}
|
2022-01-05 21:48:45 +00:00
|
|
|
url = graphList ? {"variables": $variables}
|
|
|
|
result = parseGraphList(await fetch(url, Api.list))
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2022-01-23 06:04:50 +00:00
|
|
|
proc getGraphListMembers*(list: List; after=""): Future[Result[User]] {.async.} =
|
|
|
|
if list.id.len == 0: return
|
2022-03-10 13:24:57 +00:00
|
|
|
var
|
2022-01-23 06:04:50 +00:00
|
|
|
variables = %*{
|
|
|
|
"listId": list.id,
|
|
|
|
"withSuperFollowsUserFields": false,
|
|
|
|
"withBirdwatchPivots": false,
|
|
|
|
"withDownvotePerspective": false,
|
|
|
|
"withReactionsMetadata": false,
|
|
|
|
"withReactionsPerspective": false,
|
|
|
|
"withSuperFollowsTweetFields": false
|
|
|
|
}
|
2022-03-10 13:24:57 +00:00
|
|
|
if after.len > 0:
|
|
|
|
variables["cursor"] = % after
|
|
|
|
let url = graphListMembers ? {"variables": $variables}
|
2022-01-26 16:57:46 +00:00
|
|
|
result = parseGraphListMembers(await fetchRaw(url, Api.listMembers), after)
|
2022-01-23 06:04:50 +00:00
|
|
|
|
2020-06-01 00:16:24 +00:00
|
|
|
proc getListTimeline*(id: string; after=""): Future[Timeline] {.async.} =
|
2021-12-29 07:03:00 +00:00
|
|
|
if id.len == 0: return
|
2020-06-01 00:16:24 +00:00
|
|
|
let
|
|
|
|
ps = genParams({"list_id": id, "ranking_mode": "reverse_chronological"}, after)
|
|
|
|
url = listTimeline ? ps
|
2022-01-05 21:48:45 +00:00
|
|
|
result = parseTimeline(await fetch(url, Api.timeline), after)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc getTimeline*(id: string; after=""; replies=false): Future[Timeline] {.async.} =
|
2022-01-23 06:04:50 +00:00
|
|
|
if id.len == 0: return
|
2020-06-01 00:16:24 +00:00
|
|
|
let
|
|
|
|
ps = genParams({"userId": id, "include_tweet_replies": $replies}, after)
|
|
|
|
url = timeline / (id & ".json") ? ps
|
2022-01-05 21:48:45 +00:00
|
|
|
result = parseTimeline(await fetch(url, Api.timeline), after)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc getMediaTimeline*(id: string; after=""): Future[Timeline] {.async.} =
|
2022-01-23 06:04:50 +00:00
|
|
|
if id.len == 0: return
|
2020-06-01 00:16:24 +00:00
|
|
|
let url = mediaTimeline / (id & ".json") ? genParams(cursor=after)
|
2022-01-05 21:48:45 +00:00
|
|
|
result = parseTimeline(await fetch(url, Api.timeline), after)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-16 22:20:34 +00:00
|
|
|
proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
|
2022-01-23 06:04:50 +00:00
|
|
|
if name.len == 0: return
|
2020-06-16 22:20:34 +00:00
|
|
|
let
|
|
|
|
ps = genParams({"screen_name": name, "trim_user": "true"},
|
|
|
|
count="18", ext=false)
|
|
|
|
url = photoRail ? ps
|
2022-01-23 06:04:50 +00:00
|
|
|
result = parsePhotoRail(await fetch(url, Api.timeline))
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc getSearch*[T](query: Query; after=""): Future[Result[T]] {.async.} =
|
2022-01-23 06:04:50 +00:00
|
|
|
when T is User:
|
2020-06-01 00:16:24 +00:00
|
|
|
const
|
|
|
|
searchMode = ("result_filter", "user")
|
|
|
|
parse = parseUsers
|
2022-01-26 19:27:11 +00:00
|
|
|
fetchFunc = fetchRaw
|
2020-06-01 00:16:24 +00:00
|
|
|
else:
|
|
|
|
const
|
|
|
|
searchMode = ("tweet_search_mode", "live")
|
|
|
|
parse = parseTimeline
|
2022-01-26 19:27:11 +00:00
|
|
|
fetchFunc = fetch
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2021-12-28 06:25:37 +00:00
|
|
|
let q = genQueryParam(query)
|
|
|
|
if q.len == 0 or q == emptyQuery:
|
|
|
|
return Result[T](beginning: true, query: query)
|
|
|
|
|
|
|
|
let url = search ? genParams(searchParams & @[("q", q), searchMode], after)
|
|
|
|
try:
|
2022-01-26 19:27:11 +00:00
|
|
|
result = parse(await fetchFunc(url, Api.search), after)
|
2021-12-28 06:25:37 +00:00
|
|
|
result.query = query
|
|
|
|
except InternalError:
|
|
|
|
return Result[T](beginning: true, query: query)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-02-02 11:31:49 +00:00
|
|
|
proc getGraphTweet(id: string; after=""): Future[Conversation] {.async.} =
|
|
|
|
if id.len == 0: return
|
|
|
|
let
|
|
|
|
cursor = if after.len > 0: "\"cursor\":\"$1\"," % after else: ""
|
|
|
|
variables = tweetVariables % [id, cursor]
|
|
|
|
params = {"variables": variables, "features": tweetFeatures}
|
|
|
|
js = await fetch(graphTweet ? params, Api.tweetDetail)
|
|
|
|
result = parseGraphConversation(js, id)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc getReplies*(id, after: string): Future[Result[Chain]] {.async.} =
|
2023-02-02 11:31:49 +00:00
|
|
|
result = (await getGraphTweet(id, after)).replies
|
2020-06-01 00:16:24 +00:00
|
|
|
result.beginning = after.len == 0
|
|
|
|
|
|
|
|
proc getTweet*(id: string; after=""): Future[Conversation] {.async.} =
|
2023-02-02 11:31:49 +00:00
|
|
|
result = await getGraphTweet(id)
|
2020-06-01 00:16:24 +00:00
|
|
|
if after.len > 0:
|
|
|
|
result.replies = await getReplies(id, after)
|
|
|
|
|
2022-01-23 06:45:01 +00:00
|
|
|
proc getStatus*(id: string): Future[Tweet] {.async.} =
|
|
|
|
let url = status / (id & ".json") ? genParams()
|
|
|
|
result = parseStatus(await fetch(url, Api.status))
|
|
|
|
|
2020-06-01 00:16:24 +00:00
|
|
|
proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} =
|
|
|
|
let client = newAsyncHttpClient(maxRedirects=0)
|
|
|
|
try:
|
2021-12-20 02:11:12 +00:00
|
|
|
let resp = await client.request(url, HttpHead)
|
2021-12-27 01:27:49 +00:00
|
|
|
result = resp.headers["location"].replaceUrls(prefs)
|
2020-06-01 00:16:24 +00:00
|
|
|
except:
|
|
|
|
discard
|
|
|
|
finally:
|
|
|
|
client.close()
|