api.nim 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import asyncdispatch, httpclient, uri, strutils, sequtils, sugar
  3. import packedjson
  4. import types, query, formatters, consts, apiutils, parser
  5. import experimental/parser/user
  6. proc getGraphUser*(id: string): Future[User] {.async.} =
  7. if id.len == 0 or id.any(c => not c.isDigit): return
  8. let
  9. variables = %*{"userId": id, "withSuperFollowsUserFields": true}
  10. js = await fetch(graphUser ? {"variables": $variables}, Api.userRestId)
  11. result = parseGraphUser(js, id)
  12. proc getGraphListBySlug*(name, list: string): Future[List] {.async.} =
  13. let
  14. variables = %*{"screenName": name, "listSlug": list, "withHighlightedLabel": false}
  15. url = graphListBySlug ? {"variables": $variables}
  16. result = parseGraphList(await fetch(url, Api.listBySlug))
  17. proc getGraphList*(id: string): Future[List] {.async.} =
  18. let
  19. variables = %*{"listId": id, "withHighlightedLabel": false}
  20. url = graphList ? {"variables": $variables}
  21. result = parseGraphList(await fetch(url, Api.list))
  22. proc getGraphListMembers*(list: List; after=""): Future[Result[User]] {.async.} =
  23. if list.id.len == 0: return
  24. let
  25. variables = %*{
  26. "listId": list.id,
  27. "cursor": after,
  28. "withSuperFollowsUserFields": false,
  29. "withBirdwatchPivots": false,
  30. "withDownvotePerspective": false,
  31. "withReactionsMetadata": false,
  32. "withReactionsPerspective": false,
  33. "withSuperFollowsTweetFields": false
  34. }
  35. url = graphListMembers ? {"variables": $variables}
  36. result = parseGraphListMembers(await fetch(url, Api.listMembers), after)
  37. proc getListTimeline*(id: string; after=""): Future[Timeline] {.async.} =
  38. if id.len == 0: return
  39. let
  40. ps = genParams({"list_id": id, "ranking_mode": "reverse_chronological"}, after)
  41. url = listTimeline ? ps
  42. result = parseTimeline(await fetch(url, Api.timeline), after)
  43. proc getUser*(username: string): Future[User] {.async.} =
  44. if username.len == 0: return
  45. let
  46. ps = genParams({"screen_name": username})
  47. json = await fetchRaw(userShow ? ps, Api.userShow)
  48. result = parseUser(json, username)
  49. proc getUserById*(userId: string): Future[User] {.async.} =
  50. if userId.len == 0: return
  51. let
  52. ps = genParams({"user_id": userId})
  53. json = await fetchRaw(userShow ? ps, Api.userShow)
  54. result = parseUser(json)
  55. proc getTimeline*(id: string; after=""; replies=false): Future[Timeline] {.async.} =
  56. if id.len == 0: return
  57. let
  58. ps = genParams({"userId": id, "include_tweet_replies": $replies}, after)
  59. url = timeline / (id & ".json") ? ps
  60. result = parseTimeline(await fetch(url, Api.timeline), after)
  61. proc getMediaTimeline*(id: string; after=""): Future[Timeline] {.async.} =
  62. if id.len == 0: return
  63. let url = mediaTimeline / (id & ".json") ? genParams(cursor=after)
  64. result = parseTimeline(await fetch(url, Api.timeline), after)
  65. proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
  66. if name.len == 0: return
  67. let
  68. ps = genParams({"screen_name": name, "trim_user": "true"},
  69. count="18", ext=false)
  70. url = photoRail ? ps
  71. result = parsePhotoRail(await fetch(url, Api.timeline))
  72. proc getSearch*[T](query: Query; after=""): Future[Result[T]] {.async.} =
  73. when T is User:
  74. const
  75. searchMode = ("result_filter", "user")
  76. parse = parseUsers
  77. else:
  78. const
  79. searchMode = ("tweet_search_mode", "live")
  80. parse = parseTimeline
  81. let q = genQueryParam(query)
  82. if q.len == 0 or q == emptyQuery:
  83. return Result[T](beginning: true, query: query)
  84. let url = search ? genParams(searchParams & @[("q", q), searchMode], after)
  85. try:
  86. result = parse(await fetch(url, Api.search), after)
  87. result.query = query
  88. except InternalError:
  89. return Result[T](beginning: true, query: query)
  90. proc getTweetImpl(id: string; after=""): Future[Conversation] {.async.} =
  91. let url = tweet / (id & ".json") ? genParams(cursor=after)
  92. result = parseConversation(await fetch(url, Api.tweet), id)
  93. proc getReplies*(id, after: string): Future[Result[Chain]] {.async.} =
  94. result = (await getTweetImpl(id, after)).replies
  95. result.beginning = after.len == 0
  96. proc getTweet*(id: string; after=""): Future[Conversation] {.async.} =
  97. result = await getTweetImpl(id)
  98. if after.len > 0:
  99. result.replies = await getReplies(id, after)
  100. proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} =
  101. let client = newAsyncHttpClient(maxRedirects=0)
  102. try:
  103. let resp = await client.request(url, HttpHead)
  104. result = resp.headers["location"].replaceUrls(prefs)
  105. except:
  106. discard
  107. finally:
  108. client.close()