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
|
2023-07-10 09:25:34 +00:00
|
|
|
variables = """{"screen_name": "$1"}""" % username
|
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
2023-04-21 12:41:30 +00:00
|
|
|
js = await fetchRaw(graphUser ? params, Api.userScreenName)
|
2023-01-20 03:54:19 +00:00
|
|
|
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-07-10 09:25:34 +00:00
|
|
|
variables = """{"rest_id": "$1"}""" % id
|
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
2023-04-21 12:41:30 +00:00
|
|
|
js = await fetchRaw(graphUserById ? params, Api.userRestId)
|
2022-01-26 16:24:03 +00:00
|
|
|
result = parseGraphUser(js)
|
2022-01-23 06:04:50 +00:00
|
|
|
|
2023-07-10 09:25:34 +00:00
|
|
|
proc getGraphUserTweets*(id: string; kind: TimelineKind; after=""): Future[Profile] {.async.} =
|
2023-04-21 12:41:30 +00:00
|
|
|
if id.len == 0: return
|
|
|
|
let
|
|
|
|
cursor = if after.len > 0: "\"cursor\":\"$1\"," % after else: ""
|
|
|
|
variables = userTweetsVariables % [id, cursor]
|
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
|
|
|
(url, apiId) = case kind
|
|
|
|
of TimelineKind.tweets: (graphUserTweets, Api.userTweets)
|
|
|
|
of TimelineKind.replies: (graphUserTweetsAndReplies, Api.userTweetsAndReplies)
|
|
|
|
of TimelineKind.media: (graphUserMedia, Api.userMedia)
|
|
|
|
js = await fetch(url ? params, apiId)
|
|
|
|
result = parseGraphTimeline(js, "user", after)
|
|
|
|
|
2023-07-22 01:03:45 +00:00
|
|
|
# proc getTimeline*(id: string; after=""; replies=false): Future[Profile] {.async.} =
|
|
|
|
# if id.len == 0: return
|
|
|
|
# let
|
|
|
|
# ps = genParams({"userId": id, "include_tweet_replies": $replies}, after)
|
|
|
|
# url = oldUserTweets / (id & ".json") ? ps
|
|
|
|
# result = parseTimeline(await fetch(url, Api.timeline), after)
|
2023-07-21 16:56:39 +00:00
|
|
|
|
2023-08-08 00:09:56 +00:00
|
|
|
proc getUserTimeline*(id: string; after=""): Future[Profile] {.async.} =
|
|
|
|
var ps = genParams({"id": id})
|
|
|
|
if after.len > 0:
|
|
|
|
ps.add ("down_cursor", after)
|
|
|
|
|
|
|
|
let
|
|
|
|
url = legacyUserTweets ? ps
|
|
|
|
js = await fetch(url, Api.userTimeline)
|
|
|
|
result = parseUserTimeline(js, after)
|
|
|
|
|
2023-04-21 12:41:30 +00:00
|
|
|
proc getGraphListTweets*(id: string; after=""): Future[Timeline] {.async.} =
|
|
|
|
if id.len == 0: return
|
|
|
|
let
|
|
|
|
cursor = if after.len > 0: "\"cursor\":\"$1\"," % after else: ""
|
|
|
|
variables = listTweetsVariables % [id, cursor]
|
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
|
|
|
js = await fetch(graphListTweets ? params, Api.listTweets)
|
2023-07-10 09:25:34 +00:00
|
|
|
result = parseGraphTimeline(js, "list", after).tweets
|
2023-04-21 12:41:30 +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
|
2023-04-21 12:41:30 +00:00
|
|
|
variables = %*{"screenName": name, "listSlug": list}
|
|
|
|
params = {"variables": $variables, "features": gqlFeatures}
|
|
|
|
result = parseGraphList(await fetch(graphListBySlug ? params, 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
|
2023-07-10 09:25:34 +00:00
|
|
|
variables = """{"listId": "$1"}""" % id
|
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
2023-04-21 12:41:30 +00:00
|
|
|
result = parseGraphList(await fetch(graphListById ? params, 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,
|
|
|
|
"withBirdwatchPivots": false,
|
|
|
|
"withDownvotePerspective": false,
|
|
|
|
"withReactionsMetadata": false,
|
2023-04-21 12:41:30 +00:00
|
|
|
"withReactionsPerspective": false
|
2022-01-23 06:04:50 +00:00
|
|
|
}
|
2022-03-10 13:24:57 +00:00
|
|
|
if after.len > 0:
|
|
|
|
variables["cursor"] = % after
|
2023-04-21 12:41:30 +00:00
|
|
|
let url = graphListMembers ? {"variables": $variables, "features": gqlFeatures}
|
2022-01-26 16:57:46 +00:00
|
|
|
result = parseGraphListMembers(await fetchRaw(url, Api.listMembers), after)
|
2022-01-23 06:04:50 +00:00
|
|
|
|
2023-04-21 12:41:30 +00:00
|
|
|
proc getGraphTweetResult*(id: string): Future[Tweet] {.async.} =
|
2021-12-29 07:03:00 +00:00
|
|
|
if id.len == 0: return
|
2020-06-01 00:16:24 +00:00
|
|
|
let
|
2023-07-10 09:25:34 +00:00
|
|
|
variables = """{"rest_id": "$1"}""" % id
|
2023-04-21 12:41:30 +00:00
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
|
|
|
js = await fetch(graphTweetResult ? params, Api.tweetResult)
|
|
|
|
result = parseGraphTweetResult(js)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-02-24 00:01:22 +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]
|
2023-04-21 12:41:30 +00:00
|
|
|
params = {"variables": variables, "features": gqlFeatures}
|
2023-02-24 00:01:22 +00:00
|
|
|
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-24 00:01:22 +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-24 00:01:22 +00:00
|
|
|
result = await getGraphTweet(id)
|
2020-06-01 00:16:24 +00:00
|
|
|
if after.len > 0:
|
|
|
|
result.replies = await getReplies(id, after)
|
|
|
|
|
2023-07-10 09:25:34 +00:00
|
|
|
proc getGraphSearch*(query: Query; after=""): Future[Profile] {.async.} =
|
2023-04-21 12:41:30 +00:00
|
|
|
let q = genQueryParam(query)
|
|
|
|
if q.len == 0 or q == emptyQuery:
|
2023-07-10 09:25:34 +00:00
|
|
|
return Profile(tweets: Timeline(query: query, beginning: true))
|
2023-04-21 12:41:30 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
variables = %*{
|
|
|
|
"rawQuery": q,
|
|
|
|
"count": 20,
|
|
|
|
"product": "Latest",
|
|
|
|
"withDownvotePerspective": false,
|
|
|
|
"withReactionsMetadata": false,
|
|
|
|
"withReactionsPerspective": false
|
|
|
|
}
|
|
|
|
if after.len > 0:
|
|
|
|
variables["cursor"] = % after
|
|
|
|
let url = graphSearchTimeline ? {"variables": $variables, "features": gqlFeatures}
|
2023-07-10 09:25:34 +00:00
|
|
|
result = Profile(tweets: parseGraphSearch(await fetch(url, Api.search), after))
|
|
|
|
result.tweets.query = query
|
2023-04-21 12:41:30 +00:00
|
|
|
|
2023-07-12 01:37:44 +00:00
|
|
|
proc getTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} =
|
2023-07-22 01:03:45 +00:00
|
|
|
var q = genQueryParam(query)
|
|
|
|
|
2023-07-12 01:37:44 +00:00
|
|
|
if q.len == 0 or q == emptyQuery:
|
|
|
|
return Timeline(query: query, beginning: true)
|
|
|
|
|
2023-07-22 01:03:45 +00:00
|
|
|
if after.len > 0:
|
|
|
|
q &= " max_id:" & after
|
|
|
|
|
2023-07-12 01:37:44 +00:00
|
|
|
let url = tweetSearch ? genParams({
|
2023-07-22 01:03:45 +00:00
|
|
|
"q": q ,
|
|
|
|
"modules": "status",
|
|
|
|
"result_type": "recent",
|
2023-07-12 01:37:44 +00:00
|
|
|
})
|
|
|
|
|
2023-07-22 01:03:45 +00:00
|
|
|
result = parseTweetSearch(await fetch(url, Api.search), after)
|
2023-07-12 01:37:44 +00:00
|
|
|
result.query = query
|
|
|
|
|
2023-04-21 12:41:30 +00:00
|
|
|
proc getUserSearch*(query: Query; page="1"): Future[Result[User]] {.async.} =
|
|
|
|
if query.text.len == 0:
|
|
|
|
return Result[User](query: query, beginning: true)
|
|
|
|
|
|
|
|
var url = userSearch ? {
|
|
|
|
"q": query.text,
|
|
|
|
"skip_status": "1",
|
|
|
|
"count": "20",
|
|
|
|
"page": page
|
|
|
|
}
|
|
|
|
|
|
|
|
result = parseUsers(await fetchRaw(url, Api.userSearch))
|
|
|
|
result.query = query
|
|
|
|
if page.len == 0:
|
|
|
|
result.bottom = "2"
|
|
|
|
elif page.allCharsInSet(Digits):
|
|
|
|
result.bottom = $(parseInt(page) + 1)
|
|
|
|
|
|
|
|
proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
|
|
|
|
if name.len == 0: return
|
|
|
|
let
|
|
|
|
ps = genParams({"screen_name": name, "trim_user": "true"},
|
|
|
|
count="18", ext=false)
|
|
|
|
url = photoRail ? ps
|
2023-07-21 16:56:39 +00:00
|
|
|
result = parsePhotoRail(await fetch(url, Api.photoRail))
|
2022-01-23 06:45:01 +00:00
|
|
|
|
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()
|