Improve tweet selectors

This commit is contained in:
Zed 2019-06-26 19:59:28 +02:00
parent 02fcd7b880
commit 7d2fa15676
2 changed files with 12 additions and 7 deletions

View file

@ -83,8 +83,7 @@ proc parseTweet*(node: XmlNode): Tweet =
proc parseTweets*(node: XmlNode): Tweets = proc parseTweets*(node: XmlNode): Tweets =
if node.isNil or node.kind == xnText: return if node.isNil or node.kind == xnText: return
for n in node.selectAll(".stream-item"): node.selectAll(".stream-item").map(parseTweet)
result.add parseTweet(n)
proc parseConversation*(node: XmlNode): Conversation = proc parseConversation*(node: XmlNode): Conversation =
result = Conversation( result = Conversation(
@ -92,13 +91,14 @@ proc parseConversation*(node: XmlNode): Conversation =
before: parseTweets(node.select(".in-reply-to")) before: parseTweets(node.select(".in-reply-to"))
) )
let replies = node.select(".replies-to") let replies = node.select(".replies-to", ".stream-items")
if replies.isNil: return if replies.isNil: return
result.after = parseTweets(replies.select(".ThreadedConversation--selfThread")) for reply in replies.filterIt(it.kind != xnText):
if "selfThread" in reply.attr("class"):
for reply in replies.select(".stream-items"): result.after = parseTweets(reply.select(".stream-items"))
result.replies.add parseTweets(reply) else:
result.replies.add parseTweets(reply)
proc parseVideo*(node: JsonNode): Video = proc parseVideo*(node: JsonNode): Video =
let track = node{"track"} let track = node{"track"}

View file

@ -16,6 +16,11 @@ proc select*(node: XmlNode; selector: string): XmlNode =
let nodes = node.selectAll(selector) let nodes = node.selectAll(selector)
if nodes.len > 0: nodes[0] else: nil if nodes.len > 0: nodes[0] else: nil
proc select*(node: XmlNode; parent, child: string): XmlNode =
let parentNode = node.select(parent)
if parentNode.isNil(): return
result = parentNode.select(child)
proc getAttr*(node: XmlNode; attr: string; default=""): string = proc getAttr*(node: XmlNode; attr: string; default=""): string =
if node.isNil or node.attrs.isNil: return default if node.isNil or node.attrs.isNil: return default
return node.attrs.getOrDefault(attr) return node.attrs.getOrDefault(attr)