Refactor tweet stats

This commit is contained in:
Zed 2019-07-01 23:48:25 +02:00
parent c60280415e
commit 3cedcf29ea
4 changed files with 18 additions and 17 deletions

View file

@ -63,11 +63,11 @@ proc parseTweet*(node: XmlNode): Tweet =
time: getTimestamp(tweet), time: getTimestamp(tweet),
shortTime: getShortTime(tweet), shortTime: getShortTime(tweet),
profile: parseTweetProfile(tweet), profile: parseTweetProfile(tweet),
stats: parseTweetStats(tweet),
pinned: "pinned" in tweet.attr("class"), pinned: "pinned" in tweet.attr("class"),
available: true available: true
) )
result.getTweetStats(tweet)
result.getTweetMedia(tweet) result.getTweetMedia(tweet)
result.getTweetCards(tweet) result.getTweetCards(tweet)

View file

@ -111,16 +111,14 @@ proc getIntentStats*(profile: var Profile; node: XmlNode) =
of "followers": profile.followers = text of "followers": profile.followers = text
of "following": profile.following = text of "following": profile.following = text
proc getTweetStats*(tweet: Tweet; node: XmlNode) = proc parseTweetStats*(node: XmlNode): TweetStats =
tweet.replies = "0" result = TweetStats(replies: "0", retweets: "0", likes: "0")
tweet.retweets = "0"
tweet.likes = "0"
for action in node.selectAll(".ProfileTweet-actionCountForAria"): for action in node.selectAll(".ProfileTweet-actionCountForAria"):
let text = action.innerText.split() let text = action.innerText.split()
case text[1][0 .. 2] case text[1][0 .. 2]
of "ret": tweet.retweets = text[0] of "ret": result.retweets = text[0]
of "rep": tweet.replies = text[0] of "rep": result.replies = text[0]
of "lik": tweet.likes = text[0] of "lik": result.likes = text[0]
proc getGif(player: XmlNode): Gif = proc getGif(player: XmlNode): Gif =
let let

View file

@ -66,23 +66,26 @@ type
by*: string by*: string
id*: string id*: string
TweetStats* = object
replies*: string
retweets*: string
likes*: string
Tweet* = ref object Tweet* = ref object
id*: string id*: string
profile*: Profile profile*: Profile
text*: string text*: string
time*: Time time*: Time
shortTime*: string shortTime*: string
replies*: string available*: bool
retweets*: string
likes*: string
pinned*: bool pinned*: bool
stats*: TweetStats
retweet*: Option[Retweet] retweet*: Option[Retweet]
quote*: Option[Quote] quote*: Option[Quote]
gif*: Option[Gif] gif*: Option[Gif]
video*: Option[Video] video*: Option[Video]
photos*: seq[string] photos*: seq[string]
poll*: Option[Poll] poll*: Option[Poll]
available*: bool
Thread* = object Thread* = object
tweets*: seq[Tweet] tweets*: seq[Tweet]

View file

@ -132,11 +132,11 @@
</div> </div>
#end proc #end proc
# #
#proc renderStats(tweet: Tweet): string = #proc renderStats(stats: TweetStats): string =
<div class="tweet-stats"> <div class="tweet-stats">
<span class="tweet-stat">💬 ${$tweet.replies}</span> <span class="tweet-stat">💬 ${$stats.replies}</span>
<span class="tweet-stat">🔄 ${$tweet.retweets}</span> <span class="tweet-stat">🔄 ${$stats.retweets}</span>
<span class="tweet-stat">👍 ${$tweet.likes}</span> <span class="tweet-stat">👍 ${$stats.likes}</span>
</div> </div>
#end proc #end proc
# #
@ -163,7 +163,7 @@
#elif tweet.poll.isSome: #elif tweet.poll.isSome:
${renderPoll(tweet.poll.get())} ${renderPoll(tweet.poll.get())}
#end if #end if
${renderStats(tweet)} ${renderStats(tweet.stats)}
</div> </div>
</div> </div>
#else: #else: