Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 3794a9d

Browse files
authored
Use GraphQL endpoint for photo rail (#10)
1 parent e62b180 commit 3794a9d

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

src/api.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] {.async.}
136136
result = parseGraphSearch[User](await fetch(url, Api.search), after)
137137
result.query = query
138138

139-
proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
140-
if name.len == 0: return
139+
proc getPhotoRail*(id: string): Future[PhotoRail] {.async.} =
140+
if id.len == 0: return
141141
let
142-
ps = genParams({"screen_name": name, "trim_user": "true"},
143-
count="18", ext=false)
144-
url = photoRail ? ps
145-
result = parsePhotoRail(await fetch(url, Api.photoRail))
142+
variables = userTweetsVariables % [id, ""]
143+
params = {"variables": variables, "features": gqlFeatures}
144+
url = graphUserMedia ? params
145+
result = parseGraphPhotoRail(await fetch(url, Api.userMedia))
146146

147147
proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} =
148148
let client = newAsyncHttpClient(maxRedirects=0)

src/auth.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const
1010
apiMaxReqs: Table[Api, int] = {
1111
Api.search: 50,
1212
Api.tweetDetail: 150,
13-
Api.photoRail: 180,
1413
Api.userTweets: 500,
1514
Api.userTweetsAndReplies: 500,
1615
Api.userMedia: 500,

src/consts.nim

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const
66
consumerSecret* = "Bcs59EFbbsdF6Sl9Ng71smgStWEGwXXKSjYvPVt7qys"
77

88
api = parseUri("https://api.x.com")
9-
activate* = $(api / "1.1/guest/activate.json")
10-
11-
photoRail* = api / "1.1/statuses/media_timeline.json"
129

1310
graphql = api / "graphql"
1411
graphUser* = graphql / "u7wQyGi6oExe8_TRWGMq4Q/UserResultByScreenNameQuery"

src/parser.nim

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,6 @@ proc parseTweet(js: JsonNode; jsCard: JsonNode = newJNull()): Tweet =
289289
result.text.removeSuffix(" Learn more.")
290290
result.available = false
291291

292-
proc parsePhotoRail*(js: JsonNode): PhotoRail =
293-
with error, js{"error"}:
294-
if error.getStr == "Not authorized.":
295-
return
296-
297-
for tweet in js:
298-
let
299-
t = parseTweet(tweet, js{"tweet_card"})
300-
url = if t.photos.len > 0: t.photos[0]
301-
elif t.video.isSome: get(t.video).thumb
302-
elif t.gif.isSome: get(t.gif).thumb
303-
elif t.card.isSome: get(t.card).image
304-
else: ""
305-
306-
if url.len == 0: continue
307-
result.add GalleryPhoto(url: url, tweetId: $t.id)
308-
309292
proc parseGraphTweet(js: JsonNode; isLegacy=false): Tweet =
310293
if js.kind == JNull:
311294
return Tweet()
@@ -445,6 +428,34 @@ proc parseGraphTimeline*(js: JsonNode; root: string; after=""): Profile =
445428
tweet.id = parseBiggestInt(entryId)
446429
result.pinned = some tweet
447430

431+
proc parseGraphPhotoRail*(js: JsonNode): PhotoRail =
432+
result = @[]
433+
434+
let instructions =
435+
? js{"data", "user_result", "result", "timeline_response", "timeline", "instructions"}
436+
437+
for i in instructions:
438+
if i{"__typename"}.getStr == "TimelineAddEntries":
439+
for e in i{"entries"}:
440+
let entryId = e{"entryId"}.getStr
441+
if entryId.startsWith("tweet"):
442+
with tweetResult, e{"content", "content", "tweetResult", "result"}:
443+
let t = parseGraphTweet(tweetResult, false)
444+
if not t.available:
445+
t.id = parseBiggestInt(entryId.getId())
446+
447+
let url =
448+
if t.photos.len > 0: t.photos[0]
449+
elif t.video.isSome: get(t.video).thumb
450+
elif t.gif.isSome: get(t.gif).thumb
451+
elif t.card.isSome: get(t.card).image
452+
else: ""
453+
454+
result.add GalleryPhoto(url: url, tweetId: $t.id)
455+
456+
if result.len == 16:
457+
break
458+
448459
proc parseGraphSearch*[T: User | Tweets](js: JsonNode; after=""): Result[T] =
449460
result = Result[T](beginning: after.len == 0)
450461

src/redis_cache.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ proc cache*(data: List) {.async.} =
8686
await setEx(data.listKey, listCacheTime, compress(toFlatty(data)))
8787

8888
proc cache*(data: PhotoRail; name: string) {.async.} =
89-
await setEx("pr:" & toLower(name), baseCacheTime * 2, compress(toFlatty(data)))
89+
await setEx("pr2:" & toLower(name), baseCacheTime * 2, compress(toFlatty(data)))
9090

9191
proc cache*(data: User) {.async.} =
9292
if data.username.len == 0: return
@@ -158,14 +158,14 @@ proc getCachedUsername*(userId: string): Future[string] {.async.} =
158158
# if not result.isNil:
159159
# await cache(result)
160160

161-
proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
162-
if name.len == 0: return
163-
let rail = await get("pr:" & toLower(name))
161+
proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} =
162+
if id.len == 0: return
163+
let rail = await get("pr2:" & toLower(id))
164164
if rail != redisNil:
165165
rail.deserialize(PhotoRail)
166166
else:
167-
result = await getPhotoRail(name)
168-
await cache(result, name)
167+
result = await getPhotoRail(id)
168+
await cache(result, id)
169169

170170
proc getCachedList*(username=""; slug=""; id=""): Future[List] {.async.} =
171171
let list = if id.len == 0: redisNil

src/routes/timeline.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ proc fetchProfile*(after: string; query: Query; skipRail=false;
4747
let
4848
rail =
4949
skipIf(skipRail or query.kind == media, @[]):
50-
getCachedPhotoRail(name)
50+
getCachedPhotoRail(userId)
5151

5252
user = getCachedUser(name)
5353

src/types.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type
1515
Api* {.pure.} = enum
1616
tweetDetail
1717
tweetResult
18-
photoRail
1918
search
2019
list
2120
listBySlug

0 commit comments

Comments
 (0)