Small cleanup

This commit is contained in:
Zed 2019-06-21 02:30:57 +02:00
parent 89b6cfdf98
commit 3bd54f5df2
2 changed files with 24 additions and 29 deletions

View File

@ -54,7 +54,8 @@ routes:
client = newAsyncHttpClient() client = newAsyncHttpClient()
pic = await client.getContent(url) pic = await client.getContent(url)
defer: client.close() client.close()
resp pic, mimetype(url) resp pic, mimetype(url)
get "/video/@sig/@url": get "/video/@sig/@url":

View File

@ -66,35 +66,28 @@ proc parseTweetProfile*(profile: XmlNode): Profile =
) )
proc parseTweet*(tweet: XmlNode): Tweet = proc parseTweet*(tweet: XmlNode): Tweet =
result.id = tweet.getAttr("data-item-id")
result.link = tweet.getAttr("data-permalink-path")
result.text = tweet.selectText(".tweet-text").stripTwitterUrls()
result.pinned = "pinned" in tweet.getAttr("class")
result.profile = parseTweetProfile(tweet)
let time = tweet.querySelector(".js-short-timestamp") let time = tweet.querySelector(".js-short-timestamp")
result.time = fromUnix(parseInt(time.getAttr("data-time", "0"))) result = Tweet(
result.shortTime = time.innerText() id: tweet.getAttr("data-item-id"),
link: tweet.getAttr("data-permalink-path"),
result.replies = "0" text: tweet.selectText(".tweet-text").stripTwitterUrls(),
result.likes = "0" pinned: "pinned" in tweet.getAttr("class"),
result.retweets = "0" profile: parseTweetProfile(tweet),
time: fromUnix(parseInt(time.getAttr("data-time", "0"))),
shortTime: time.innerText(),
replies: "0",
likes: "0",
retweets: "0"
)
for action in tweet.querySelectorAll(".ProfileTweet-actionCountForAria"): for action in tweet.querySelectorAll(".ProfileTweet-actionCountForAria"):
let let text = action.innerText.split()
text = action.innerText.split()
num = text[0]
case text[1] case text[1]
of "replies": result.replies = num of "replies": result.replies = text[0]
of "likes": result.likes = num of "likes": result.likes = text[0]
of "retweets": result.retweets = num of "retweets": result.retweets = text[0]
else: discard else: discard
let by = tweet.selectText(".js-retweet-text > a > b")
if by.len > 0:
result.retweetBy = some(by)
for photo in tweet.querySelectorAll(".AdaptiveMedia-photoContainer"): for photo in tweet.querySelectorAll(".AdaptiveMedia-photoContainer"):
result.photos.add photo.attrs["data-image-url"] result.photos.add photo.attrs["data-image-url"]
@ -106,21 +99,22 @@ proc parseTweet*(tweet: XmlNode): Tweet =
else: else:
result.videoThumb = some(thumb) result.videoThumb = some(thumb)
let by = tweet.selectText(".js-retweet-text > a > b")
if by.len > 0:
result.retweetBy = some(by)
proc parseTweets*(node: XmlNode): Tweets = proc parseTweets*(node: XmlNode): Tweets =
if node.isNil: return if node.isNil: return
node.querySelectorAll(".tweet").map(parseTweet) node.querySelectorAll(".tweet").map(parseTweet)
template selectTweets*(node: XmlNode; class: string): untyped =
parseTweets(node.querySelector(class))
proc parseConversation*(node: XmlNode): Conversation = proc parseConversation*(node: XmlNode): Conversation =
result.tweet = parseTweet(node.querySelector(".permalink-tweet-container > .tweet")) result.tweet = parseTweet(node.querySelector(".permalink-tweet-container > .tweet"))
result.before = node.selectTweets(".in-reply-to") result.before = parseTweets(node.querySelector(".in-reply-to"))
let replies = node.querySelector(".replies-to") let replies = node.querySelector(".replies-to")
if replies.isNil: return if replies.isNil: return
result.after = replies.selectTweets(".ThreadedConversation--selfThread") result.after = parseTweets(replies.querySelector(".ThreadedConversation--selfThread"))
for reply in replies.querySelectorAll("li > .stream-items"): for reply in replies.querySelectorAll("li > .stream-items"):
let thread = parseTweets(reply) let thread = parseTweets(reply)