Skip to content

Commit

Permalink
Misc. fixes and refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jun 2, 2020
1 parent c736a98 commit 5eec0bd
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/parserutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ proc getIntentStats*(profile: var Profile; node: XmlNode) =
of "following": profile.following = text

proc parseTweetStats*(node: XmlNode): TweetStats =
result = TweetStats(replies: "0", retweets: "0", likes: "0")
result = TweetStats()
for action in node.selectAll(".ProfileTweet-actionCountForAria"):
let text = action.innerText.split()
case text[1][0 .. 2]
of "ret": result.retweets = text[0]
of "rep": result.replies = text[0]
of "lik": result.likes = text[0]
of "ret": result.retweets = text[0].parseInt
of "rep": result.replies = text[0].parseInt
of "lik": result.likes = text[0].parseInt

proc parseTweetReply*(node: XmlNode): seq[string] =
let reply = node.select(".ReplyingToContextBelowAuthor")
Expand Down
1 change: 1 addition & 0 deletions src/routes/embed.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jester
import router_utils
import ".."/[api, types, agents]
import ../views/[embed]
export getVideo

export embed

Expand Down
1 change: 1 addition & 0 deletions src/routes/list.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jester
import router_utils
import ".."/[query, types, api, agents]
import ../views/[general, timeline, list]
export getListTimeline, getListMembers

template respList*(list, timeline: typed) =
if list.minId.len == 0:
Expand Down
3 changes: 3 additions & 0 deletions src/routes/media.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const m3u8Regex* = re"""url="(.+.m3u8)""""

proc createMediaRouter*(cfg: Config) =
router media:
get "/pic/?":
resp Http404

get "/pic/@url":
cond "http" in @"url"
cond "twimg" in @"url"
Expand Down
1 change: 1 addition & 0 deletions src/routes/resolver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jester
import router_utils
import ".."/[query, types, api, agents]
import ../views/general
export resolve

template respResolved*(url, kind: string): untyped =
let u = url
Expand Down
3 changes: 2 additions & 1 deletion src/routes/timeline.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ proc createTimelineRouter*(cfg: Config) =
setProfileCacheTime(cfg.profileCacheTime)

router timeline:
get "/@name/?@tab?":
get "/@name/?@tab?/?":
cond '.' notin @"name"
cond @"name" notin ["pic", "gif", "video"]
cond @"tab" in ["with_replies", "media", "search", ""]
let
prefs = cookiePrefs()
Expand Down
8 changes: 5 additions & 3 deletions src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type
tweetId*: string
color*: string

PhotoRail* = seq[GalleryPhoto]

Poll* = object
options*: seq[string]
values*: seq[int]
Expand Down Expand Up @@ -131,9 +133,9 @@ type
id*: int64

TweetStats* = object
replies*: string
retweets*: string
likes*: string
replies*: int
retweets*: int
likes*: int

Tweet* = ref object
id*: int64
Expand Down
6 changes: 3 additions & 3 deletions src/views/profile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ proc renderStat(num, class: string; text=""): VNode =
buildHtml(li(class=class)):
span(class="profile-stat-header"): text capitalizeAscii(t)
span(class="profile-stat-num"):
text if num.len == 0: "?" else: num
text if num.len == 0: "?" else: insertSep(num, ',')

proc renderProfileCard*(profile: Profile; prefs: Prefs): VNode =
buildHtml(tdiv(class="profile-card")):
Expand Down Expand Up @@ -57,7 +57,7 @@ proc renderProfileCard*(profile: Profile; prefs: Prefs): VNode =
renderStat(profile.followers, "followers")
renderStat(profile.likes, "likes")

proc renderPhotoRail(profile: Profile; photoRail: seq[GalleryPhoto]): VNode =
proc renderPhotoRail(profile: Profile; photoRail: PhotoRail): VNode =
buildHtml(tdiv(class="photo-rail-card")):
tdiv(class="photo-rail-header"):
a(href=(&"/{profile.username}/media")):
Expand Down Expand Up @@ -90,7 +90,7 @@ proc renderProtected(username: string): VNode =
p: text &"Only confirmed followers have access to @{username}'s tweets."

proc renderProfile*(profile: Profile; timeline: Timeline;
photoRail: seq[GalleryPhoto]; prefs: Prefs; path: string): VNode =
photoRail: PhotoRail; prefs: Prefs; path: string): VNode =
timeline.query.fromUser = @[profile.username]
buildHtml(tdiv(class="profile-tabs")):
if not prefs.hideBanner:
Expand Down
31 changes: 17 additions & 14 deletions src/views/tweet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ proc renderVideo*(video: Video; prefs: Prefs; path: string): VNode =

proc renderGif(gif: Gif; prefs: Prefs): VNode =
buildHtml(tdiv(class="attachments media-gif")):
tdiv(class="gallery-gif", style=style(maxHeight, "unset")):
tdiv(class="gallery-gif", style={maxHeight: "unset"}):
tdiv(class="attachment"):
let thumb = getPicUrl(gif.thumb)
let url = getGifUrl(gif.url)
Expand All @@ -124,14 +124,16 @@ proc renderGif(gif: Gif; prefs: Prefs): VNode =
proc renderPoll(poll: Poll): VNode =
buildHtml(tdiv(class="poll")):
for i in 0 ..< poll.options.len:
let leader = if poll.leader == i: " leader" else: ""
let perc = $poll.values[i] & "%"
let
leader = if poll.leader == i: " leader" else: ""
perc = poll.values[i] / poll.votes * 100
percStr = (&"{perc:>3.0f}").strip(chars={'.'}) & '%'
tdiv(class=("poll-meter" & leader)):
span(class="poll-choice-bar", style=style(width, perc))
span(class="poll-choice-value"): text perc
span(class="poll-choice-bar", style={width: percStr})
span(class="poll-choice-value"): text percStr
span(class="poll-choice-option"): text poll.options[i]
span(class="poll-info"):
text $poll.votes & " votes • " & poll.status
text insertSep($poll.votes, ',') & " votes • " & poll.status

proc renderCardImage(card: Card): VNode =
buildHtml(tdiv(class="card-image-container")):
Expand Down Expand Up @@ -169,11 +171,11 @@ proc renderCard(card: Card; prefs: Prefs; path: string): VNode =

proc renderStats(stats: TweetStats; views: string): VNode =
buildHtml(tdiv(class="tweet-stats")):
span(class="tweet-stat"): icon "comment", $stats.replies
span(class="tweet-stat"): icon "retweet", $stats.retweets
span(class="tweet-stat"): icon "heart", $stats.likes
span(class="tweet-stat"): icon "comment", insertSep($stats.replies, ',')
span(class="tweet-stat"): icon "retweet", insertSep($stats.retweets, ',')
span(class="tweet-stat"): icon "heart", insertSep($stats.likes, ',')
if views.len > 0:
span(class="tweet-stat"): icon "play", views
span(class="tweet-stat"): icon "play", insertSep(views, ',')

proc renderReply(tweet: Tweet): VNode =
buildHtml(tdiv(class="replying-to")):
Expand Down Expand Up @@ -279,7 +281,8 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class="";
var views = ""
renderHeader(tweet)

if index == 0 and tweet.reply.len > 0:
if index == 0 and tweet.reply.len > 0 and
(tweet.reply.len > 1 or tweet.reply[0] != tweet.profile.username):
renderReply(tweet)

tdiv(class="tweet-content media-body", dir="auto"):
Expand All @@ -288,9 +291,6 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class="";
if tweet.attribution.isSome:
renderAttribution(tweet.attribution.get())

if tweet.quote.isSome:
renderQuote(tweet.quote.get(), prefs)

if tweet.card.isSome:
renderCard(tweet.card.get(), prefs, path)
elif tweet.photos.len > 0:
Expand All @@ -304,6 +304,9 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class="";
elif tweet.poll.isSome:
renderPoll(tweet.poll.get())

if tweet.quote.isSome:
renderQuote(tweet.quote.get(), prefs)

if mainTweet:
p(class="tweet-published"): text getTweetTime(tweet)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_invalid_username(self, username):

def test_suspended(self):
self.open_nitter('test')
self.assert_text(f'User "test" has been suspended')
self.assert_text('User "test" has been suspended')

@parameterized.expand(banner_color)
def test_banner_color(self, username, color):
Expand Down

0 comments on commit 5eec0bd

Please sign in to comment.