api.nim 3.9 KB

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