From f8f4487c339e12e65cf69e5464e866f3aa958096 Mon Sep 17 00:00:00 2001 From: Zed Date: Sat, 21 Dec 2019 05:44:58 +0100 Subject: [PATCH] Support tweet locations --- src/formatters.nim | 5 +++++ src/parser.nim | 1 + src/parserutils.nim | 6 ++++++ src/sass/tweet/_base.scss | 5 +++++ src/types.nim | 1 + src/views/profile.nim | 8 ++++---- src/views/tweet.nim | 13 ++++++++++++- 7 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/formatters.nim b/src/formatters.nim index a8b9dd7..f80ac6f 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -103,3 +103,8 @@ proc getTwitterLink*(path: string; params: Table[string, string]): string = result = $(parseUri("https://twitter.com") / path ? p) if username.len > 0: result = result.replace("/" & username, "") + +proc getLocation*(u: Profile | Tweet): (string, string) = + let loc = u.location.split(":") + let url = if loc.len > 1: "/search?q=place:" & loc[1] else: "" + (loc[0], url) diff --git a/src/parser.nim b/src/parser.nim index 4e4b2a5..a5e5171 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -108,6 +108,7 @@ proc parseTweet*(node: XmlNode): Tweet = stats: parseTweetStats(tweet), reply: parseTweetReply(tweet), mediaTags: getMediaTags(tweet), + location: getTweetLocation(tweet), hasThread: tweet.select(".content > .self-thread-context") != nil, pinned: "pinned" in tweet.attr("class"), available: true diff --git a/src/parserutils.nim b/src/parserutils.nim index b009295..639b927 100644 --- a/src/parserutils.nim +++ b/src/parserutils.nim @@ -285,3 +285,9 @@ proc getMediaTags*(node: XmlNode): seq[Profile] = let un = user["screen_name"].getStr if un notin usernames: continue result.add Profile(username: un, fullname: user["name"].getStr) + +proc getTweetLocation*(node: XmlNode): string = + let geo = node.select(".js-geo-pivot-link") + if geo == nil: return + result = geo.innerText().stripText() + result &= ":" & geo.attr("data-place-id") diff --git a/src/sass/tweet/_base.scss b/src/sass/tweet/_base.scss index d7fe469..b009f7b 100644 --- a/src/sass/tweet/_base.scss +++ b/src/sass/tweet/_base.scss @@ -128,6 +128,10 @@ font-size: 13px; } +.tweet-geo { + color: var(--fg_faded); +} + .replying-to { color: var(--fg_faded); margin: -2px 0 4px; @@ -168,6 +172,7 @@ .show-thread { display: block; pointer-events: all; + padding-top: 2px; } .unavailable-box { diff --git a/src/types.nim b/src/types.nim index 971be22..4deba0a 100644 --- a/src/types.nim +++ b/src/types.nim @@ -147,6 +147,7 @@ type hasThread*: bool available*: bool tombstone*: string + location*: string stats*: TweetStats retweet*: Option[Retweet] attribution*: Option[Profile] diff --git a/src/views/profile.nim b/src/views/profile.nim index 6e0703b..0b78a68 100644 --- a/src/views/profile.nim +++ b/src/views/profile.nim @@ -31,11 +31,11 @@ proc renderProfileCard*(profile: Profile; prefs: Prefs): VNode = if profile.location.len > 0: tdiv(class="profile-location"): span: icon "location" - let loc = profile.location.split(":") - if loc.len > 1: - a(href=("/search?q=place:" & loc[1])): text loc[0] + let (place, url) = profile.getLocation() + if url.len > 1: + a(href=url): text place else: - span: text loc[0] + span: text place if profile.website.len > 0: tdiv(class="profile-website"): diff --git a/src/views/tweet.nim b/src/views/tweet.nim index f60c38b..8843827 100644 --- a/src/views/tweet.nim +++ b/src/views/tweet.nim @@ -245,6 +245,17 @@ proc renderQuote(quote: Quote; prefs: Prefs): VNode = a(class="show-thread", href=getLink(quote)): text "Show this thread" +proc renderLocation*(tweet: Tweet): string = + let (place, url) = tweet.getLocation() + if place.len == 0: return + let node = buildHtml(span(class="tweet-geo")): + text " – at " + if url.len > 1: + a(href=url): text place + else: + text place + return $node + proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; index=0; total=(-1); last=false; showThread=false; mainTweet=false): VNode = @@ -272,7 +283,7 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; renderReply(tweet) tdiv(class="tweet-content media-body", dir="auto"): - verbatim replaceUrl(tweet.text, prefs) + verbatim replaceUrl(tweet.text, prefs) & renderLocation(tweet) if tweet.attribution.isSome: renderAttribution(tweet.attribution.get())