From bb83b6b6d3b1b2a0c13e06e716de9c69720e14d5 Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 2 Oct 2019 22:28:53 +0200 Subject: [PATCH] More API refactoring --- src/api/list.nim | 9 ++++----- src/api/media.nim | 3 +-- src/api/timeline.nim | 27 ++++++++++++--------------- src/api/tweet.nim | 9 +++------ tests/test_thread.py | 2 +- 5 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/api/list.nim b/src/api/list.nim index e28ea8f..dc3d4c1 100644 --- a/src/api/list.nim +++ b/src/api/list.nim @@ -1,4 +1,4 @@ -import httpclient, asyncdispatch, htmlparser, strformat +import httpclient, asyncdispatch, htmlparser import sequtils, strutils, json, uri import ".."/[types, parser, parserutils, query] @@ -29,8 +29,7 @@ proc getListTimeline*(username, list, agent, after: string): Future[Timeline] {. proc getListMembers*(username, list, agent: string): Future[Result[Profile]] {.async.} = let url = base / (listMembersUrl % [username, list]) - referer = base / &"{username}/lists/{list}/members" - html = await fetchHtml(url, genHeaders(agent, referer)) + html = await fetchHtml(url, genHeaders(agent, url)) result = Result[Profile]( minId: html.selectAttr(".stream-container", "data-min-position"), @@ -42,8 +41,8 @@ proc getListMembers*(username, list, agent: string): Future[Result[Profile]] {.a proc getListMembersSearch*(username, list, agent, after: string): Future[Result[Profile]] {.async.} = let - url = base / ((listMembersUrl & "/timeline") % [username, list]) - referer = base / &"{username}/lists/{list}/members" + referer = base / (listMembersUrl % [username, list]) + url = referer / "timeline" headers = genHeaders({"x-push-with": "XMLHttpRequest"}, agent, referer, xml=true) var params = toSeq({ diff --git a/src/api/media.nim b/src/api/media.nim index c230dde..351029b 100644 --- a/src/api/media.nim +++ b/src/api/media.nim @@ -53,8 +53,7 @@ proc getGuestToken(agent: string; force=false): Future[string] {.async.} = let headers = genHeaders({"authorization": auth}, agent, base, lang=false) - url = apiBase / tokenUrl - json = await fetchJson(url, headers) + json = await fetchJson(apiBase / tokenUrl, headers) if json != nil: result = json["guest_token"].to(string) diff --git a/src/api/timeline.nim b/src/api/timeline.nim index 3f2f68d..9f167d0 100644 --- a/src/api/timeline.nim +++ b/src/api/timeline.nim @@ -1,9 +1,14 @@ import httpclient, asyncdispatch, htmlparser, strformat -import sequtils, strutils, json, xmltree, uri +import sequtils, strutils, json, uri import ".."/[types, parser, parserutils, formatters, query] import utils, consts, media, search +proc getMedia(thread: Thread | Timeline; agent: string) {.async.} = + await all(getVideos(thread, agent), + getCards(thread, agent), + getPolls(thread, agent)) + proc finishTimeline*(json: JsonNode; query: Query; after, agent: string): Future[Timeline] {.async.} = result = getResult[Tweet](json, query, after) if json == nil: return @@ -11,19 +16,13 @@ proc finishTimeline*(json: JsonNode; query: Query; after, agent: string): Future if json["new_latent_count"].to(int) == 0: return if not json.hasKey("items_html"): return - let - html = parseHtml(json["items_html"].to(string)) - thread = parseThread(html) - vidsFut = getVideos(thread, agent) - pollFut = getPolls(thread, agent) - cardFut = getCards(thread, agent) + let html = parseHtml(json["items_html"].to(string)) + let thread = parseThread(html) - await all(vidsFut, pollFut, cardFut) + await getMedia(thread, agent) result.content = thread.content proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} = - let headers = genHeaders(agent, base / username, xml=true) - var params = toSeq({ "include_available_features": "1", "include_entities": "1", @@ -34,7 +33,9 @@ proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} = if after.len > 0: params.add {"max_position": after} + let headers = genHeaders(agent, base / username, xml=true) let json = await fetchJson(base / (timelineUrl % username) ? params, headers) + result = await finishTimeline(json, Query(), after, agent) proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} = @@ -48,9 +49,5 @@ proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Ti timeline = parseTimeline(html.select("#timeline > .stream-container"), after) profile = parseTimelineProfile(html) - vidsFut = getVideos(timeline, agent) - pollFut = getPolls(timeline, agent) - cardFut = getCards(timeline, agent) - - await all(vidsFut, pollFut, cardFut) + await getMedia(timeline, agent) result = (profile, timeline) diff --git a/src/api/tweet.nim b/src/api/tweet.nim index 2939100..4a58c61 100644 --- a/src/api/tweet.nim +++ b/src/api/tweet.nim @@ -17,9 +17,6 @@ proc getTweet*(username, id, after, agent: string): Future[Conversation] {.async result = parseConversation(html, after) - let - vidsFut = getConversationVideos(result, agent) - pollFut = getConversationPolls(result, agent) - cardFut = getConversationCards(result, agent) - - await all(vidsFut, pollFut, cardFut) + await all(getConversationVideos(result, agent), + getConversationCards(result, agent), + getConversationPolls(result, agent)) diff --git a/tests/test_thread.py b/tests/test_thread.py index 3064273..020b7f7 100644 --- a/tests/test_thread.py +++ b/tests/test_thread.py @@ -18,7 +18,7 @@ thread = [ ['d0m96/status/1141811379407425537', [], 'I\'m', ['The', 'The', 'Today', 'Some', 'If', 'There', 'Above'], - [['Thank', 'Also,', 'Awesome.', 'Thank']]], + [['Awesome.']]], ['gmpreussner/status/999766552546299904', [], 'A', [], [['I'], ['I']]]