api.nim 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 getGraphList*(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 getGraphListById*(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. let
  22. ps = genParams({"list_id": id, "ranking_mode": "reverse_chronological"}, after)
  23. url = listTimeline ? ps
  24. result = parseTimeline(await fetch(url), after)
  25. proc getListMembers*(list: List; after=""): Future[Result[Profile]] {.async.} =
  26. if list.id.len == 0: return
  27. let
  28. ps = genParams({"list_id": list.id}, after)
  29. url = listMembers ? ps
  30. result = parseListMembers(await fetch(url, oldApi=true), after)
  31. proc getProfile*(username: string): Future[Profile] {.async.} =
  32. let
  33. ps = genParams({"screen_name": username})
  34. url = userShow ? ps
  35. result = parseUserShow(await fetch(url, oldApi=true), username)
  36. proc getTimeline*(id: string; after=""; replies=false): Future[Timeline] {.async.} =
  37. let
  38. ps = genParams({"userId": id, "include_tweet_replies": $replies}, after)
  39. url = timeline / (id & ".json") ? ps
  40. result = parseTimeline(await fetch(url), after)
  41. proc getMediaTimeline*(id: string; after=""): Future[Timeline] {.async.} =
  42. let url = mediaTimeline / (id & ".json") ? genParams(cursor=after)
  43. result = parseTimeline(await fetch(url), after)
  44. proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
  45. let
  46. ps = genParams({"screen_name": name, "trim_user": "true"},
  47. count="18", ext=false)
  48. url = photoRail ? ps
  49. result = parsePhotoRail(await fetch(url, oldApi=true))
  50. proc getSearch*[T](query: Query; after=""): Future[Result[T]] {.async.} =
  51. when T is Profile:
  52. const
  53. searchMode = ("result_filter", "user")
  54. parse = parseUsers
  55. else:
  56. const
  57. searchMode = ("tweet_search_mode", "live")
  58. parse = parseTimeline
  59. let
  60. q = genQueryParam(query)
  61. url = search ? genParams(searchParams & @[("q", q), searchMode], after)
  62. result = parse(await fetch(url), after)
  63. result.query = query
  64. proc getTweetImpl(id: string; after=""): Future[Conversation] {.async.} =
  65. let url = tweet / (id & ".json") ? genParams(cursor=after)
  66. result = parseConversation(await fetch(url), id)
  67. proc getReplies*(id, after: string): Future[Result[Chain]] {.async.} =
  68. result = (await getTweetImpl(id, after)).replies
  69. result.beginning = after.len == 0
  70. proc getTweet*(id: string; after=""): Future[Conversation] {.async.} =
  71. result = await getTweetImpl(id)
  72. if after.len > 0:
  73. result.replies = await getReplies(id, after)
  74. proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} =
  75. let client = newAsyncHttpClient(maxRedirects=0)
  76. try:
  77. let resp = await client.request(url, HttpHead)
  78. result = resp.headers["location"].replaceUrls(prefs)
  79. except:
  80. discard
  81. finally:
  82. client.close()