Initial search refactoring

This commit is contained in:
Zed 2019-08-23 02:15:25 +02:00
parent c7339d2126
commit f7c1c28368
5 changed files with 26 additions and 24 deletions

View file

@ -13,10 +13,10 @@ const
apiBase = parseUri("https://api.twitter.com/1.1/") apiBase = parseUri("https://api.twitter.com/1.1/")
timelineUrl = "i/profiles/show/$1/timeline/tweets" timelineUrl = "i/profiles/show/$1/timeline/tweets"
timelineSearchUrl = "i/search/timeline"
timelineMediaUrl = "i/profiles/show/$1/media_timeline" timelineMediaUrl = "i/profiles/show/$1/media_timeline"
profilePopupUrl = "i/profiles/popup" profilePopupUrl = "i/profiles/popup"
profileIntentUrl = "intent/user" profileIntentUrl = "intent/user"
searchUrl = "i/search/timeline"
tweetUrl = "status" tweetUrl = "status"
videoUrl = "videos/tweet/config/$1.json" videoUrl = "videos/tweet/config/$1.json"
tokenUrl = "guest/activate.json" tokenUrl = "guest/activate.json"
@ -40,7 +40,7 @@ macro genMediaGet(media: untyped; token=false) =
quote do: quote do:
proc `multi`(thread: Thread | Timeline; agent: string; token="") {.async.} = proc `multi`(thread: Thread | Timeline; agent: string; token="") {.async.} =
if thread == nil: return if thread == nil: return
var `media` = thread.tweets.filterIt(it.`media`.isSome) var `media` = thread.content.filterIt(it.`media`.isSome)
when `token`: when `token`:
var gToken = token var gToken = token
if gToken.len == 0: gToken = await getGuestToken(agent) if gToken.len == 0: gToken = await getGuestToken(agent)
@ -299,7 +299,7 @@ proc finishTimeline(json: JsonNode; query: Option[Query]; after, agent: string):
cardFut = getCards(thread, agent) cardFut = getCards(thread, agent)
await all(vidsFut, pollFut, cardFut) await all(vidsFut, pollFut, cardFut)
result.tweets = thread.tweets result.content = thread.content
proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} = proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} =
let headers = newHttpHeaders({ let headers = newHttpHeaders({
@ -348,7 +348,7 @@ proc getTimelineSearch*(query: Query; after, agent: string): Future[Timeline] {.
"reset_error_state": "false" "reset_error_state": "false"
} }
let json = await fetchJson(base / timelineSearchUrl ? params, headers) let json = await fetchJson(base / searchUrl ? params, headers)
result = await finishTimeline(json, some(query), after, agent) result = await finishTimeline(json, some(query), after, agent)
proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} = proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} =

View file

@ -121,11 +121,11 @@ proc parseThread*(nodes: XmlNode): Thread =
for n in nodes.filterIt(it.kind != xnText): for n in nodes.filterIt(it.kind != xnText):
let class = n.attr("class").toLower() let class = n.attr("class").toLower()
if "tombstone" in class or "unavailable" in class or "withheld" in class: if "tombstone" in class or "unavailable" in class or "withheld" in class:
result.tweets.add Tweet() result.content.add Tweet()
elif "morereplies" in class: elif "morereplies" in class:
result.more = getMoreReplies(n) result.more = getMoreReplies(n)
else: else:
result.tweets.add parseTweet(n) result.content.add parseTweet(n)
proc parseConversation*(node: XmlNode): Conversation = proc parseConversation*(node: XmlNode): Conversation =
result = Conversation( result = Conversation(
@ -150,7 +150,7 @@ proc parseConversation*(node: XmlNode): Conversation =
proc parseTimeline*(node: XmlNode; after: string): Timeline = proc parseTimeline*(node: XmlNode; after: string): Timeline =
if node == nil: return if node == nil: return
result = Timeline( result = Timeline(
tweets: parseThread(node.select(".stream > .stream-items")).tweets, content: parseThread(node.select(".stream > .stream-items")).content,
minId: node.attr("data-min-position"), minId: node.attr("data-min-position"),
maxId: node.attr("data-max-position"), maxId: node.attr("data-max-position"),
hasMore: node.select(".has-more-items") != nil, hasMore: node.select(".has-more-items") != nil,

View file

@ -75,6 +75,14 @@ type
fromUser*: seq[string] fromUser*: seq[string]
sep*: string sep*: string
Result*[T] = ref object
content*: seq[T]
minId*: string
maxId*: string
hasMore*: bool
beginning*: bool
query*: Option[Query]
Gif* = object Gif* = object
url*: string url*: string
thumb*: string thumb*: string
@ -151,7 +159,7 @@ type
poll*: Option[Poll] poll*: Option[Poll]
Thread* = ref object Thread* = ref object
tweets*: seq[Tweet] content*: seq[Tweet]
more*: int more*: int
Conversation* = ref object Conversation* = ref object
@ -160,13 +168,7 @@ type
after*: Thread after*: Thread
replies*: seq[Thread] replies*: seq[Thread]
Timeline* = ref object Timeline* = Result[Tweet]
tweets*: seq[Tweet]
minId*: string
maxId*: string
hasMore*: bool
beginning*: bool
query*: Option[Query]
Config* = ref object Config* = ref object
address*: string address*: string
@ -178,4 +180,4 @@ type
profileCacheTime*: int profileCacheTime*: int
proc contains*(thread: Thread; tweet: Tweet): bool = proc contains*(thread: Thread; tweet: Tweet): bool =
thread.tweets.anyIt(it.id == tweet.id) thread.content.anyIt(it.id == tweet.id)

View file

@ -13,8 +13,8 @@ proc renderMoreReplies(thread: Thread): VNode =
proc renderReplyThread(thread: Thread; prefs: Prefs): VNode = proc renderReplyThread(thread: Thread; prefs: Prefs): VNode =
buildHtml(tdiv(class="reply thread thread-line")): buildHtml(tdiv(class="reply thread thread-line")):
for i, tweet in thread.tweets: for i, tweet in thread.content:
let last = (i == thread.tweets.high and thread.more == 0) let last = (i == thread.content.high and thread.more == 0)
renderTweet(tweet, prefs, index=i, last=last) renderTweet(tweet, prefs, index=i, last=last)
if thread.more != 0: if thread.more != 0:
@ -26,7 +26,7 @@ proc renderConversation*(conversation: Conversation; prefs: Prefs): VNode =
tdiv(class="main-thread"): tdiv(class="main-thread"):
if conversation.before != nil: if conversation.before != nil:
tdiv(class="before-tweet thread-line"): tdiv(class="before-tweet thread-line"):
for i, tweet in conversation.before.tweets: for i, tweet in conversation.before.content:
renderTweet(tweet, prefs, index=i) renderTweet(tweet, prefs, index=i)
tdiv(class="main-tweet"): tdiv(class="main-tweet"):
@ -35,9 +35,9 @@ proc renderConversation*(conversation: Conversation; prefs: Prefs): VNode =
if hasAfter: if hasAfter:
tdiv(class="after-tweet thread-line"): tdiv(class="after-tweet thread-line"):
let total = conversation.after.tweets.high let total = conversation.after.content.high
let more = conversation.after.more let more = conversation.after.more
for i, tweet in conversation.after.tweets: for i, tweet in conversation.after.content:
renderTweet(tweet, prefs, index=i, last=(i == total and more == 0)) renderTweet(tweet, prefs, index=i, last=(i == total and more == 0))
if more != 0: if more != 0:

View file

@ -65,9 +65,9 @@ proc threadFilter(it: Tweet; tweetThread: string): bool =
proc renderTweets(timeline: Timeline; prefs: Prefs): VNode = proc renderTweets(timeline: Timeline; prefs: Prefs): VNode =
buildHtml(tdiv(id="posts")): buildHtml(tdiv(id="posts")):
var threads: seq[string] var threads: seq[string]
for tweet in timeline.tweets: for tweet in timeline.content:
if tweet.threadId in threads: continue if tweet.threadId in threads: continue
let thread = timeline.tweets.filterIt(threadFilter(it, tweet.threadId)) let thread = timeline.content.filterIt(threadFilter(it, tweet.threadId))
if thread.len < 2: if thread.len < 2:
renderTweet(tweet, prefs, class="timeline-tweet") renderTweet(tweet, prefs, class="timeline-tweet")
else: else:
@ -88,7 +88,7 @@ proc renderTimeline*(timeline: Timeline; username: string; protected: bool;
if protected: if protected:
renderProtected(username) renderProtected(username)
elif timeline.tweets.len == 0: elif timeline.content.len == 0:
renderNoneFound() renderNoneFound()
else: else:
renderTweets(timeline, prefs) renderTweets(timeline, prefs)