瀏覽代碼

Use faster API to get pinned tweets

Zed 4 年之前
父節點
當前提交
27183f1a74
共有 5 個文件被更改,包括 13 次插入3 次删除
  1. 4 0
      src/api.nim
  2. 1 0
      src/consts.nim
  3. 5 0
      src/parser.nim
  4. 2 3
      src/redis_cache.nim
  5. 1 0
      src/types.nim

+ 4 - 0
src/api.nim

@@ -114,6 +114,10 @@ proc getTweet*(id: string; after=""): Future[Conversation] {.async.} =
   if after.len > 0:
     result.replies = await getReplies(id, after)
 
+proc getStatus*(id: string): Future[Tweet] {.async.} =
+  let url = status / (id & ".json") ? genParams()
+  result = parseStatus(await fetch(url, Api.status))
+
 proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} =
   let client = newAsyncHttpClient(maxRedirects=0)
   try:

+ 1 - 0
src/consts.nim

@@ -9,6 +9,7 @@ const
 
   userShow* = api / "1.1/users/show.json"
   photoRail* = api / "1.1/statuses/media_timeline.json"
+  status* = api / "1.1/statuses/show"
   search* = api / "2/search/adaptive.json"
 
   timelineApi = api / "2/timeline"

+ 5 - 0
src/parser.nim

@@ -372,6 +372,11 @@ proc parseConversation*(js: JsonNode; tweetId: string): Conversation =
     elif "cursor-bottom" in entry:
       result.replies.bottom = e.getCursor
 
+proc parseStatus*(js: JsonNode): Tweet =
+  result = parseTweet(js)
+  if not result.isNil:
+    result.user = parseUser(js{"user"})
+
 proc parseInstructions[T](res: var Result[T]; global: GlobalObjects; js: JsonNode) =
   if js.kind != JArray or js.len == 0:
     return

+ 2 - 3
src/redis_cache.nim

@@ -151,9 +151,8 @@ proc getCachedTweet*(id: int64): Future[Tweet] {.async.} =
   if tweet != redisNil:
     tweet.deserialize(Tweet)
   else:
-    let conv = await getTweet($id)
-    if not conv.isNil:
-      result = conv.tweet
+    result = await getStatus($id)
+    if result.isNil:
       await cache(result)
 
 proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =

+ 1 - 0
src/types.nim

@@ -17,6 +17,7 @@ type
     listBySlug
     listMembers
     userRestId
+    status
 
   RateLimit* = object
     remaining*: int