Selaa lähdekoodia

Use old user endpoint to avoid graphql rate limits

Zed 6 vuotta sitten
vanhempi
commit
74534e8fef
4 muutettua tiedostoa jossa 21 lisäystä ja 1 poistoa
  1. 6 0
      src/api.nim
  2. 1 0
      src/consts.nim
  3. 13 0
      src/parser.nim
  4. 1 1
      src/redis_cache.nim

+ 6 - 0
src/api.nim

@@ -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)

+ 1 - 0
src/consts.nim

@@ -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"
 

+ 13 - 0
src/parser.nim

@@ -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"}:
@@ -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:

+ 1 - 1
src/redis_cache.nim

@@ -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)