From b031ea762ea32c712ea85b7524f73bde76564a5f Mon Sep 17 00:00:00 2001 From: 110Percent <110percent@riseup.net> Date: Sat, 26 Feb 2022 13:37:17 -0500 Subject: [PATCH 1/5] Add source label to main tweets --- src/parser.nim | 3 +++ src/types.nim | 1 + src/views/tweet.nim | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/parser.nim b/src/parser.nim index d3d62cf..1f16d12 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -3,6 +3,7 @@ import strutils, options, tables, times, math import packedjson, packedjson/deserialiser import types, parserutils, utils import experimental/parser/unifiedcard +import std/strutils proc parseUser(js: JsonNode; id=""): User = if js.isNull: return @@ -183,12 +184,14 @@ proc parseCard(js: JsonNode; urls: JsonNode): Card = proc parseTweet(js: JsonNode): Tweet = if js.isNull: return + result = Tweet( id: js{"id_str"}.getId, threadId: js{"conversation_id_str"}.getId, replyId: js{"in_reply_to_status_id_str"}.getId, text: js{"full_text"}.getStr, time: js{"created_at"}.getTime, + source: js{"source"}.getStr.split(">")[1].split("<")[0], hasThread: js{"self_thread"}.notNull, available: true, user: User(id: js{"user_id_str"}.getStr), diff --git a/src/types.nim b/src/types.nim index 9a6ad7f..f28e90a 100644 --- a/src/types.nim +++ b/src/types.nim @@ -175,6 +175,7 @@ type tombstone*: string location*: string stats*: TweetStats + source*: string retweet*: Option[Tweet] attribution*: Option[User] mediaTags*: seq[User] diff --git a/src/views/tweet.nim b/src/views/tweet.nim index 8b712a6..b6f8d7e 100644 --- a/src/views/tweet.nim +++ b/src/views/tweet.nim @@ -344,7 +344,7 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; index=0; renderQuote(tweet.quote.get(), prefs, path) if mainTweet: - p(class="tweet-published"): text getTime(tweet) + p(class="tweet-published"): text getTime(tweet) & " · " & tweet.source if tweet.mediaTags.len > 0: renderMediaTags(tweet.mediaTags) From 8f65bd583d059fb353825482cd4e970828132a78 Mon Sep 17 00:00:00 2001 From: 110Percent <110percent@riseup.net> Date: Sat, 26 Feb 2022 13:40:38 -0500 Subject: [PATCH 2/5] Remove accidental newline --- src/parser.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parser.nim b/src/parser.nim index 1f16d12..778187b 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -184,7 +184,6 @@ proc parseCard(js: JsonNode; urls: JsonNode): Card = proc parseTweet(js: JsonNode): Tweet = if js.isNull: return - result = Tweet( id: js{"id_str"}.getId, threadId: js{"conversation_id_str"}.getId, From baf5bf89e79fd92fb68fd0d59c0e9facdad21525 Mon Sep 17 00:00:00 2001 From: Curtis <110percent@riseup.net> Date: Sat, 26 Feb 2022 19:00:06 -0500 Subject: [PATCH 3/5] Implement requested changes --- src/parser.nim | 1 - src/parserutils.nim | 6 ++++++ src/types.nim | 2 +- src/views/tweet.nim | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/parser.nim b/src/parser.nim index 778187b..aa55f60 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -3,7 +3,6 @@ import strutils, options, tables, times, math import packedjson, packedjson/deserialiser import types, parserutils, utils import experimental/parser/unifiedcard -import std/strutils proc parseUser(js: JsonNode; id=""): User = if js.isNull: return diff --git a/src/parserutils.nim b/src/parserutils.nim index a605ea4..e0b2669 100644 --- a/src/parserutils.nim +++ b/src/parserutils.nim @@ -133,6 +133,12 @@ proc getTombstone*(js: JsonNode): string = result = js{"tombstoneInfo", "richText", "text"}.getStr result.removeSuffix(" Learn more") +proc getSource*(js: JsonNode): string = + let + raw_src = js["source"].getStr + src = raw_src.substr(raw_src.find('>') + 1, raw_src.rfind('<') - 1) + return src + proc extractSlice(js: JsonNode): Slice[int] = result = js["indices"][0].getInt ..< js["indices"][1].getInt diff --git a/src/types.nim b/src/types.nim index f28e90a..98433aa 100644 --- a/src/types.nim +++ b/src/types.nim @@ -174,8 +174,8 @@ type available*: bool tombstone*: string location*: string - stats*: TweetStats source*: string + stats*: TweetStats retweet*: Option[Tweet] attribution*: Option[User] mediaTags*: seq[User] diff --git a/src/views/tweet.nim b/src/views/tweet.nim index b6f8d7e..6e6f3af 100644 --- a/src/views/tweet.nim +++ b/src/views/tweet.nim @@ -344,7 +344,7 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; index=0; renderQuote(tweet.quote.get(), prefs, path) if mainTweet: - p(class="tweet-published"): text getTime(tweet) & " · " & tweet.source + p(class="tweet-published"): text &"{getTime(tweet)} · {tweet.source}" if tweet.mediaTags.len > 0: renderMediaTags(tweet.mediaTags) From ce5cbaeb8758fa6afbc89e436a96d0ed8c11b609 Mon Sep 17 00:00:00 2001 From: Curtis <110percent@riseup.net> Date: Sat, 26 Feb 2022 19:02:34 -0500 Subject: [PATCH 4/5] Implement getSource proc --- src/parser.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.nim b/src/parser.nim index aa55f60..1a7c073 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -189,7 +189,7 @@ proc parseTweet(js: JsonNode): Tweet = replyId: js{"in_reply_to_status_id_str"}.getId, text: js{"full_text"}.getStr, time: js{"created_at"}.getTime, - source: js{"source"}.getStr.split(">")[1].split("<")[0], + source: getSource(js), hasThread: js{"self_thread"}.notNull, available: true, user: User(id: js{"user_id_str"}.getStr), From 2536370cd7480880fa1a7157c66fbf52937c09ab Mon Sep 17 00:00:00 2001 From: Zed Date: Fri, 1 Apr 2022 17:56:55 +0200 Subject: [PATCH 5/5] Minor fixes, update tests --- src/parserutils.nim | 6 ++---- tests/test_card.py | 7 +------ 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/parserutils.nim b/src/parserutils.nim index e0b2669..4929c2a 100644 --- a/src/parserutils.nim +++ b/src/parserutils.nim @@ -134,10 +134,8 @@ proc getTombstone*(js: JsonNode): string = result.removeSuffix(" Learn more") proc getSource*(js: JsonNode): string = - let - raw_src = js["source"].getStr - src = raw_src.substr(raw_src.find('>') + 1, raw_src.rfind('<') - 1) - return src + let src = js{"source"}.getStr + result = src.substr(src.find('>') + 1, src.rfind('<') - 1) proc extractSlice(js: JsonNode): Slice[int] = result = js["indices"][0].getInt ..< js["indices"][1].getInt diff --git a/tests/test_card.py b/tests/test_card.py index 7fd233c..e497649 100644 --- a/tests/test_card.py +++ b/tests/test_card.py @@ -11,7 +11,7 @@ card = [ ['nim_lang/status/1136652293510717440', 'Version 0.20.0 released', 'We are very proud to announce Nim version 0.20. This is a massive release, both literally and figuratively. It contains more than 1,000 commits and it marks our release candidate for version 1.0!', - 'nim-lang.org', True], + 'nim-lang.org', False], ['voidtarget/status/1094632512926605312', 'Basic OBS Studio plugin, written in nim, supporting C++ (C fine too)', @@ -25,11 +25,6 @@ card = [ ] no_thumb = [ - ['Bountysource/status/1141879700639215617', - 'Post a bounty on kivy/plyer!', - 'Automation and Screen Reader Support', - 'bountysource.com'], - ['brent_p/status/1088857328680488961', 'Hts Nim Sugar', 'hts-nim is a library that allows one to use htslib via the nim programming language. Nim is a garbage-collected language that compiles to C and often has similar performance. I have become very...',