api.nim 3.3 KB

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