From 514b727328e6c4c54fb9571d79a3b40bbc96e53f Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 29 Jan 2021 01:15:10 -0500 Subject: [PATCH 1/4] better retweet extraction --- src/BirdsiteLive.Domain/StatusService.cs | 13 ++++++++++- .../Extractors/TweetExtractor.cs | 22 +++++++++++++++---- .../Models/ExtractedTweet.cs | 2 ++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/BirdsiteLive.Domain/StatusService.cs b/src/BirdsiteLive.Domain/StatusService.cs index c669779..4983de1 100644 --- a/src/BirdsiteLive.Domain/StatusService.cs +++ b/src/BirdsiteLive.Domain/StatusService.cs @@ -46,6 +46,17 @@ public Note GetStatus(string username, ExtractedTweet tweet) var extractedTags = _statusExtractor.Extract(tweet.MessageContent); _statisticsHandler.ExtractedStatus(extractedTags.tags.Count(x => x.type == "Mention")); + // Replace RT by a link + var content = extractedTags.content; + if (content.Contains("{RT}") && tweet.IsRetweet) + { + if (!string.IsNullOrWhiteSpace(tweet.RetweetUrl)) + content = content.Replace("{RT}", + $@"RT"); + else + content = content.Replace("{RT}", "RT"); + } + string inReplyTo = null; if (tweet.InReplyToStatusId != default) inReplyTo = $"https://{_instanceSettings.Domain}/users/{tweet.InReplyToAccount.ToLowerInvariant()}/statuses/{tweet.InReplyToStatusId}"; @@ -67,7 +78,7 @@ public Note GetStatus(string username, ExtractedTweet tweet) cc = new string[0], sensitive = false, - content = $"

{extractedTags.content}

", + content = $"

{content}

", attachment = Convert(tweet.Media), tag = extractedTags.tags }; diff --git a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs index 573385c..5f339fd 100644 --- a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs +++ b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs @@ -26,11 +26,25 @@ public ExtractedTweet Extract(ITweet tweet) Media = ExtractMedia(tweet.Media), CreatedAt = tweet.CreatedAt.ToUniversalTime(), IsReply = tweet.InReplyToUserId != null, - IsThread = tweet.InReplyToUserId != null && tweet.InReplyToUserId == tweet.CreatedBy.Id + IsThread = tweet.InReplyToUserId != null && tweet.InReplyToUserId == tweet.CreatedBy.Id, + IsRetweet = tweet.IsRetweet, + RetweetUrl = ExtractRetweetUrl(tweet) }; + return extractedTweet; } + private string ExtractRetweetUrl(ITweet tweet) + { + if (tweet.IsRetweet && tweet.FullText.Contains("https://t.co/")) + { + var retweetId = tweet.FullText.Split(new[] { "https://t.co/" }, StringSplitOptions.RemoveEmptyEntries).Last(); + return $"https://t.co/{retweetId}"; + } + + return null; + } + public string ExtractMessage(ITweet tweet) { var tweetUrls = tweet.Media.Select(x => x.URL).Distinct(); @@ -38,13 +52,13 @@ public string ExtractMessage(ITweet tweet) foreach (var tweetUrl in tweetUrls) message = message.Replace(tweetUrl, string.Empty).Trim(); - if (tweet.QuotedTweet != null) message = $"[Quote RT]{Environment.NewLine}{message}"; + if (tweet.QuotedTweet != null) message = $"[Quote {{RT}}]{Environment.NewLine}{message}"; if (tweet.IsRetweet) { if (tweet.RetweetedTweet != null) - message = $"[RT @{tweet.RetweetedTweet.CreatedBy.ScreenName}]{Environment.NewLine}{tweet.RetweetedTweet.FullText}"; + message = $"[{{RT}} @{tweet.RetweetedTweet.CreatedBy.ScreenName}]{Environment.NewLine}{message}"; else - message = message.Replace("RT", "[RT]"); + message = message.Replace("RT", "[{{RT}}]"); } // Expand URLs diff --git a/src/BirdsiteLive.Twitter/Models/ExtractedTweet.cs b/src/BirdsiteLive.Twitter/Models/ExtractedTweet.cs index 1f6bcea..f7f4e59 100644 --- a/src/BirdsiteLive.Twitter/Models/ExtractedTweet.cs +++ b/src/BirdsiteLive.Twitter/Models/ExtractedTweet.cs @@ -13,5 +13,7 @@ public class ExtractedTweet public string InReplyToAccount { get; set; } public bool IsReply { get; set; } public bool IsThread { get; set; } + public bool IsRetweet { get; set; } + public string RetweetUrl { get; set; } } } \ No newline at end of file From e797e7eee06e261e42c299d2ab29b11c863dcd1d Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 29 Jan 2021 01:29:35 -0500 Subject: [PATCH 2/4] fix retweet text --- src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs index 5f339fd..b2c00d7 100644 --- a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs +++ b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs @@ -50,7 +50,12 @@ public string ExtractMessage(ITweet tweet) var tweetUrls = tweet.Media.Select(x => x.URL).Distinct(); var message = tweet.FullText; foreach (var tweetUrl in tweetUrls) - message = message.Replace(tweetUrl, string.Empty).Trim(); + { + if(tweet.IsRetweet) + message = tweet.RetweetedTweet.FullText.Replace(tweetUrl, string.Empty).Trim(); + else + message = message.Replace(tweetUrl, string.Empty).Trim(); + } if (tweet.QuotedTweet != null) message = $"[Quote {{RT}}]{Environment.NewLine}{message}"; if (tweet.IsRetweet) From a7e8f3a987e19f7f38e7b311fb0cec6d10f8c620 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 29 Jan 2021 19:40:27 -0500 Subject: [PATCH 3/4] road to 0.12.1 --- src/BirdsiteLive/BirdsiteLive.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BirdsiteLive/BirdsiteLive.csproj b/src/BirdsiteLive/BirdsiteLive.csproj index ddba39f..3766f3a 100644 --- a/src/BirdsiteLive/BirdsiteLive.csproj +++ b/src/BirdsiteLive/BirdsiteLive.csproj @@ -4,7 +4,7 @@ netcoreapp3.1 d21486de-a812-47eb-a419-05682bb68856 Linux - 0.12.0 + 0.12.1 From 739f7aaeadbce5e6d35991c931f6d76306cdd3d4 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 29 Jan 2021 20:07:38 -0500 Subject: [PATCH 4/4] fix QuoteRT detection --- src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs index b2c00d7..c432a35 100644 --- a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs +++ b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs @@ -27,7 +27,7 @@ public ExtractedTweet Extract(ITweet tweet) CreatedAt = tweet.CreatedAt.ToUniversalTime(), IsReply = tweet.InReplyToUserId != null, IsThread = tweet.InReplyToUserId != null && tweet.InReplyToUserId == tweet.CreatedBy.Id, - IsRetweet = tweet.IsRetweet, + IsRetweet = tweet.IsRetweet || tweet.QuotedStatusId != null, RetweetUrl = ExtractRetweetUrl(tweet) };