Deduplicate GraphQL timeline endpoints

This commit is contained in:
Zed 2023-03-22 19:18:42 +01:00
parent a4966168de
commit 1f10c1fdff
4 changed files with 19 additions and 29 deletions

View File

@ -8,38 +8,31 @@ proc getGraphUser*(username: string): Future[User] {.async.} =
if username.len == 0: return
let
variables = %*{"screen_name": username}
params = {"variables": $variables, "features": userFeatures}
params = {"variables": $variables, "features": gqlFeatures}
js = await fetchRaw(graphUser ? params, Api.userScreenName)
result = parseGraphUser(js)
proc getGraphUserById*(id: string): Future[User] {.async.} =
if id.len == 0 or id.any(c => not c.isDigit): return
let
variables = %*{"userId": id, "withSuperFollowsUserFields": true}
params = {"variables": $variables, "features": tweetFeatures}
variables = %*{"userId": id}
params = {"variables": $variables, "features": gqlFeatures}
js = await fetchRaw(graphUserById ? params, Api.userRestId)
result = parseGraphUser(js)
proc getGraphUserTweets*(id: string; after=""; replies=false): Future[Timeline] {.async.} =
proc getGraphUserTweets*(id: string; kind: TimelineKind; after=""): Future[Timeline] {.async.} =
if id.len == 0: return
let
cursor = if after.len > 0: "\"cursor\":\"$1\"," % after else: ""
variables = userTweetsVariables % [id, cursor]
params = {"variables": variables, "features": tweetFeatures}
(url, apiId) = if replies: (graphUserTweetsAndReplies, Api.userTweetsAndReplies)
else: (graphUserTweets, Api.userTweets)
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, after)
proc getGraphUserMedia*(id: string; after=""): Future[Timeline] {.async.} =
if id.len == 0: return
let
cursor = if after.len > 0: "\"cursor\":\"$1\"," % after else: ""
variables = userTweetsVariables % [id, cursor]
params = {"variables": variables, "features": tweetFeatures}
js = await fetch(graphUserMedia ? params, Api.userMedia)
result = parseGraphTimeline(js, after)
proc getGraphListBySlug*(name, list: string): Future[List] {.async.} =
let
variables = %*{"screenName": name, "listSlug": list, "withHighlightedLabel": false}
@ -112,7 +105,7 @@ proc getGraphTweet(id: string; after=""): Future[Conversation] {.async.} =
let
cursor = if after.len > 0: "\"cursor\":\"$1\"," % after else: ""
variables = tweetVariables % [id, cursor]
params = {"variables": variables, "features": tweetFeatures}
params = {"variables": variables, "features": gqlFeatures}
js = await fetch(graphTweet ? params, Api.tweetDetail)
result = parseGraphConversation(js, id)

View File

@ -58,15 +58,7 @@ const
## photos: "result_filter: photos"
## videos: "result_filter: videos"
userFeatures* = """{
"responsive_web_twitter_blue_verified_badge_is_enabled": true,
"responsive_web_graphql_exclude_directive_enabled": true,
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": true,
"responsive_web_graphql_timeline_navigation_enabled": false,
"verified_phone_label_enabled": false
}"""
tweetFeatures* = """{
gqlFeatures* = """{
"longform_notetweets_consumption_enabled": true,
"longform_notetweets_richtext_consumption_enabled": true,
"responsive_web_twitter_blue_verified_badge_is_enabled": true,

View File

@ -47,9 +47,9 @@ proc fetchProfile*(after: string; query: Query; skipRail=false;
let
timeline =
case query.kind
of posts: getGraphUserTweets(userId, after)
of replies: getGraphUserTweets(userId, after, replies=true)
of media: getGraphUserMedia(userId, after)
of posts: getGraphUserTweets(userId, TimelineKind.tweets, after)
of replies: getGraphUserTweets(userId, TimelineKind.replies, after)
of media: getGraphUserTweets(userId, TimelineKind.media, after)
else: getSearch[Tweet](query, after)
rail =

View File

@ -8,6 +8,11 @@ type
RateLimitError* = object of CatchableError
InternalError* = object of CatchableError
TimelineKind* {.pure.} = enum
tweets
replies
media
Api* {.pure.} = enum
tweetDetail
timeline