Skip to content

Commit

Permalink
Use old user endpoint to avoid graphql rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jun 2, 2020
1 parent 3986370 commit 74534e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ proc getListMembers*(list: List; after=""): Future[Result[Profile]] {.async.} =
url = listMembers ? ps
result = parseListMembers(await fetch(url, oldApi=true), after)

proc getProfile*(username: string): Future[Profile] {.async.} =
let
ps = genParams({"screen_name": username})
url = userLookup ? ps
result = parseUserShow(await fetch(url, oldApi=true), username)

proc getTimeline*(id: string; after=""; replies=false): Future[Timeline] {.async.} =
let
ps = genParams({"userId": id, "include_tweet_replies": $replies}, after)
Expand Down
1 change: 1 addition & 0 deletions src/consts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const
mediaTimeline* = timelineApi / "media"
listTimeline* = timelineApi / "list.json"
listMembers* = api / "1.1/lists/members.json"
userLookup* = api / "1.1/users/show.json"
tweet* = timelineApi / "conversation"
search* = api / "2/search/adaptive.json"

Expand Down
13 changes: 13 additions & 0 deletions src/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ proc parseProfile(js: JsonNode; id=""): Profile =

result.expandProfileEntities(js)

proc parseUserShow*(js: JsonNode; username: string): Profile =
if js == nil: return
with error, js{"errors"}:
result = Profile(username: username)
if parseError(error) == suspended:
result.suspended = true
return

result = parseProfile(js)

proc parseGraphProfile*(js: JsonNode; username: string): Profile =
if js == nil: return
with error, js{"errors"}:
Expand Down Expand Up @@ -301,6 +311,9 @@ proc parseConversation*(js: JsonNode; tweetId: string): Conversation =
let global = parseGlobalObjects(? js)

let instructions = ? js{"timeline", "instructions"}
if instructions.len == 0:
return

for e in instructions[0]{"addEntries", "entries"}:
let entry = e{"entryId"}.getStr
if "tweet" in entry:
Expand Down
2 changes: 1 addition & 1 deletion src/redis_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.}
if prof != redisNil:
result = prof.to(Profile)
else:
result = await getGraphProfile(username)
result = await getProfile(username)
if result.id.len > 0:
await cache(result)

Expand Down

0 comments on commit 74534e8

Please sign in to comment.