11# SPDX-License-Identifier: AGPL-3.0-only
2- import asyncdispatch, httpclient, uri, strutils, sequtils, sugar, tables
2+ import asyncdispatch, httpclient, strutils, sequtils, sugar
33import packedjson
44import types, query, formatters, consts, apiutils, parser
55import experimental/ parser as newParser
@@ -11,95 +11,91 @@ proc genParams(variables: string; fieldToggles = ""): seq[(string, string)] =
1111 if fieldToggles.len > 0 :
1212 result .add (" fieldToggles" , fieldToggles)
1313
14- proc mediaUrl (id: string ; cursor: string ): SessionAwareUrl =
15- let
16- cookieVars = userMediaVars % [id, cursor]
17- oauthVars = restIdVars % [id, cursor]
18- result = SessionAwareUrl (
19- cookieUrl: graphUserMedia ? genParams (cookieVars),
20- oauthUrl: graphUserMediaV2 ? genParams (oauthVars)
14+ proc apiUrl (endpoint, variables: string ; fieldToggles = " " ): ApiUrl =
15+ return ApiUrl (endpoint: endpoint, params: genParams (variables, fieldToggles))
16+
17+ proc apiReq (endpoint, variables: string ; fieldToggles = " " ): ApiReq =
18+ let url = apiUrl (endpoint, variables, fieldToggles)
19+ return ApiReq (cookie: url, oauth: url)
20+
21+ proc mediaUrl (id: string ; cursor: string ): ApiReq =
22+ result = ApiReq (
23+ cookie: apiUrl (graphUserMedia, userMediaVars % [id, cursor]),
24+ oauth: apiUrl (graphUserMediaV2, restIdVars % [id, cursor])
2125 )
2226
23- proc userTweetsUrl (id: string ; cursor: string ): SessionAwareUrl =
24- let
25- cookieVars = userTweetsVars % [id, cursor]
26- oauthVars = restIdVars % [id, cursor]
27- result = SessionAwareUrl (
28- # cookieUrl: graphUserTweets ? genParams(cookieVars, userTweetsFieldToggles),
29- oauthUrl: graphUserTweetsV2 ? genParams (oauthVars)
27+ proc userTweetsUrl (id: string ; cursor: string ): ApiReq =
28+ result = ApiReq (
29+ # cookie: apiUrl(graphUserTweets, userTweetsVars % [id, cursor], userTweetsFieldToggles),
30+ oauth: apiUrl (graphUserTweetsV2, restIdVars % [id, cursor])
3031 )
3132 # might change this in the future pending testing
32- result .cookieUrl = result .oauthUrl
33+ result .cookie = result .oauth
3334
34- proc userTweetsAndRepliesUrl (id: string ; cursor: string ): SessionAwareUrl =
35- let
36- cookieVars = userTweetsAndRepliesVars % [id, cursor]
37- oauthVars = restIdVars % [id, cursor]
38- result = SessionAwareUrl (
39- cookieUrl: graphUserTweetsAndReplies ? genParams (cookieVars, userTweetsFieldToggles),
40- oauthUrl: graphUserTweetsAndRepliesV2 ? genParams (oauthVars)
35+ proc userTweetsAndRepliesUrl (id: string ; cursor: string ): ApiReq =
36+ let cookieVars = userTweetsAndRepliesVars % [id, cursor]
37+ result = ApiReq (
38+ cookie: apiUrl (graphUserTweetsAndReplies, cookieVars, userTweetsFieldToggles),
39+ oauth: apiUrl (graphUserTweetsAndRepliesV2, restIdVars % [id, cursor])
4140 )
4241
43- proc tweetDetailUrl (id: string ; cursor: string ): SessionAwareUrl =
44- let
45- cookieVars = tweetDetailVars % [id, cursor]
46- oauthVars = tweetVars % [id, cursor]
47- result = SessionAwareUrl (
48- cookieUrl: graphTweetDetail ? genParams (cookieVars, tweetDetailFieldToggles),
49- oauthUrl: graphTweet ? genParams (oauthVars)
42+ proc tweetDetailUrl (id: string ; cursor: string ): ApiReq =
43+ let cookieVars = tweetDetailVars % [id, cursor]
44+ result = ApiReq (
45+ cookie: apiUrl (graphTweetDetail, cookieVars, tweetDetailFieldToggles),
46+ oauth: apiUrl (graphTweet, tweetVars % [id, cursor])
5047 )
5148
52- proc userUrl (username: string ): SessionAwareUrl =
53- let
54- cookieVars = """ {"screen_name":"$1","withGrokTranslatedBio":false} """ % username
55- oauthVars = """ {"screen_name": "$1"} """ % username
56- result = SessionAwareUrl (
57- cookieUrl: graphUser ? genParams (cookieVars, tweetDetailFieldToggles),
58- oauthUrl: graphUserV2 ? genParams (oauthVars)
49+ proc userUrl (username: string ): ApiReq =
50+ let cookieVars = """ {"screen_name":"$1","withGrokTranslatedBio":false} """ % username
51+ result = ApiReq (
52+ cookie: apiUrl (graphUser, cookieVars, tweetDetailFieldToggles),
53+ oauth: apiUrl (graphUserV2, """ {"screen_name": "$1"} """ % username)
5954 )
6055
6156proc getGraphUser * (username: string ): Future [User ] {.async .} =
6257 if username.len == 0 : return
63- let js = await fetchRaw (userUrl (username), Api .userScreenName )
58+ let js = await fetchRaw (userUrl (username))
6459 result = parseGraphUser (js)
6560
6661proc getGraphUserById * (id: string ): Future [User ] {.async .} =
6762 if id.len == 0 or id.any (c => not c.isDigit): return
6863 let
69- url = graphUserById ? genParams ( """ {"rest_id": "$1"} """ % id)
70- js = await fetchRaw (url, Api .userRestId )
64+ url = apiReq ( graphUserById, """ {"rest_id": "$1"} """ % id)
65+ js = await fetchRaw (url)
7166 result = parseGraphUser (js)
7267
7368proc getGraphUserTweets * (id: string ; kind: TimelineKind ; after= " " ): Future [Profile ] {.async .} =
7469 if id.len == 0 : return
7570 let
7671 cursor = if after.len > 0 : " \" cursor\" :\" $1\" ," % after else : " "
77- js = case kind
78- of TimelineKind .tweets:
79- await fetch (userTweetsUrl (id, cursor), Api .userTweets)
80- of TimelineKind .replies:
81- await fetch (userTweetsAndRepliesUrl (id, cursor), Api .userTweetsAndReplies)
82- of TimelineKind .media:
83- await fetch (mediaUrl (id, cursor), Api .userMedia)
72+ url = case kind
73+ of TimelineKind .tweets: userTweetsUrl (id, cursor)
74+ of TimelineKind .replies: userTweetsAndRepliesUrl (id, cursor)
75+ of TimelineKind .media: mediaUrl (id, cursor)
76+ js = await fetch (url)
8477 result = parseGraphTimeline (js, after)
8578
8679proc getGraphListTweets * (id: string ; after= " " ): Future [Timeline ] {.async .} =
8780 if id.len == 0 : return
8881 let
8982 cursor = if after.len > 0 : " \" cursor\" :\" $1\" ," % after else : " "
90- url = graphListTweets ? genParams (restIdVars % [id, cursor])
91- result = parseGraphTimeline (await fetch (url, Api .listTweets), after).tweets
83+ url = apiReq (graphListTweets, restIdVars % [id, cursor])
84+ js = await fetch (url)
85+ result = parseGraphTimeline (js, after).tweets
9286
9387proc getGraphListBySlug * (name, list: string ): Future [List ] {.async .} =
9488 let
9589 variables = %* {" screenName" : name, " listSlug" : list}
96- url = graphListBySlug ? genParams ($ variables)
97- result = parseGraphList (await fetch (url, Api .listBySlug))
90+ url = apiReq (graphListBySlug, $ variables)
91+ js = await fetch (url)
92+ result = parseGraphList (js)
9893
9994proc getGraphList * (id: string ): Future [List ] {.async .} =
100- let
101- url = graphListById ? genParams (""" {"listId": "$1"} """ % id)
102- result = parseGraphList (await fetch (url, Api .list))
95+ let
96+ url = apiReq (graphListById, """ {"listId": "$1"} """ % id)
97+ js = await fetch (url)
98+ result = parseGraphList (js)
10399
104100proc getGraphListMembers * (list: List ; after= " " ): Future [Result [User ]] {.async .} =
105101 if list.id.len == 0 : return
@@ -113,22 +109,23 @@ proc getGraphListMembers*(list: List; after=""): Future[Result[User]] {.async.}
113109 }
114110 if after.len > 0 :
115111 variables[" cursor" ] = % after
116- let url = graphListMembers ? genParams ($ variables)
117- result = parseGraphListMembers (await fetchRaw (url, Api .listMembers), after)
112+ let
113+ url = apiReq (graphListMembers, $ variables)
114+ js = await fetchRaw (url)
115+ result = parseGraphListMembers (js, after)
118116
119117proc getGraphTweetResult * (id: string ): Future [Tweet ] {.async .} =
120118 if id.len == 0 : return
121119 let
122- variables = """ {"rest_id": "$1"} """ % id
123- params = {" variables" : variables, " features" : gqlFeatures}
124- js = await fetch (graphTweetResult ? params, Api .tweetResult)
120+ url = apiReq (graphTweetResult, """ {"rest_id": "$1"} """ % id)
121+ js = await fetch (url)
125122 result = parseGraphTweetResult (js)
126123
127124proc getGraphTweet (id: string ; after= " " ): Future [Conversation ] {.async .} =
128125 if id.len == 0 : return
129126 let
130127 cursor = if after.len > 0 : " \" cursor\" :\" $1\" ," % after else : " "
131- js = await fetch (tweetDetailUrl (id, cursor), Api .tweetDetail )
128+ js = await fetch (tweetDetailUrl (id, cursor))
132129 result = parseGraphConversation (js, id)
133130
134131proc getReplies * (id, after: string ): Future [Result [Chain ]] {.async .} =
@@ -157,8 +154,10 @@ proc getGraphTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} =
157154 }
158155 if after.len > 0 :
159156 variables[" cursor" ] = % after
160- let url = graphSearchTimeline ? genParams ($ variables)
161- result = parseGraphSearch [Tweets ](await fetch (url, Api .search), after)
157+ let
158+ url = apiReq (graphSearchTimeline, $ variables)
159+ js = await fetch (url)
160+ result = parseGraphSearch [Tweets ](js, after)
162161 result .query = query
163162
164163proc getGraphUserSearch * (query: Query ; after= " " ): Future [Result [User ]] {.async .} =
@@ -179,13 +178,15 @@ proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] {.async.}
179178 variables[" cursor" ] = % after
180179 result .beginning = false
181180
182- let url = graphSearchTimeline ? genParams ($ variables)
183- result = parseGraphSearch [User ](await fetch (url, Api .search), after)
181+ let
182+ url = apiReq (graphSearchTimeline, $ variables)
183+ js = await fetch (url)
184+ result = parseGraphSearch [User ](js, after)
184185 result .query = query
185186
186187proc getPhotoRail * (id: string ): Future [PhotoRail ] {.async .} =
187188 if id.len == 0 : return
188- let js = await fetch (mediaUrl (id, " " ), Api .userMedia )
189+ let js = await fetch (mediaUrl (id, " " ))
189190 result = parseGraphPhotoRail (js)
190191
191192proc resolve * (url: string ; prefs: Prefs ): Future [string ] {.async .} =
0 commit comments