From 7ddda8d18c4ec473c236410ff57699e8312182ce Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Tue, 2 Feb 2021 20:05:59 -0500 Subject: [PATCH] better RT extraction --- .../Extractors/TweetExtractor.cs | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs index c432a35..7fce30c 100644 --- a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs +++ b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs @@ -23,7 +23,7 @@ namespace BirdsiteLive.Twitter.Extractors InReplyToStatusId = tweet.InReplyToStatusId, InReplyToAccount = tweet.InReplyToScreenName, MessageContent = ExtractMessage(tweet), - Media = ExtractMedia(tweet.Media), + Media = ExtractMedia(tweet), CreatedAt = tweet.CreatedAt.ToUniversalTime(), IsReply = tweet.InReplyToUserId != null, IsThread = tweet.InReplyToUserId != null && tweet.InReplyToUserId == tweet.CreatedBy.Id, @@ -36,10 +36,17 @@ namespace BirdsiteLive.Twitter.Extractors private string ExtractRetweetUrl(ITweet tweet) { - if (tweet.IsRetweet && tweet.FullText.Contains("https://t.co/")) + if (tweet.IsRetweet) { - var retweetId = tweet.FullText.Split(new[] { "https://t.co/" }, StringSplitOptions.RemoveEmptyEntries).Last(); - return $"https://t.co/{retweetId}"; + if (tweet.RetweetedTweet != null) + { + return tweet.RetweetedTweet.Url; + } + if (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; @@ -47,8 +54,15 @@ namespace BirdsiteLive.Twitter.Extractors public string ExtractMessage(ITweet tweet) { - var tweetUrls = tweet.Media.Select(x => x.URL).Distinct(); var message = tweet.FullText; + var tweetUrls = tweet.Media.Select(x => x.URL).Distinct(); + + if (tweet.IsRetweet && tweet.QuotedStatusId == null && message.StartsWith("RT") && tweet.RetweetedTweet != null) + { + message = tweet.RetweetedTweet.FullText; + tweetUrls = tweet.RetweetedTweet.Media.Select(x => x.URL).Distinct(); + } + foreach (var tweetUrl in tweetUrls) { if(tweet.IsRetweet) @@ -60,8 +74,10 @@ namespace BirdsiteLive.Twitter.Extractors if (tweet.QuotedTweet != null) message = $"[Quote {{RT}}]{Environment.NewLine}{message}"; if (tweet.IsRetweet) { - if (tweet.RetweetedTweet != null) + if (tweet.RetweetedTweet != null && !message.StartsWith("RT")) message = $"[{{RT}} @{tweet.RetweetedTweet.CreatedBy.ScreenName}]{Environment.NewLine}{message}"; + else if (tweet.RetweetedTweet != null && message.StartsWith($"RT @{tweet.RetweetedTweet.CreatedBy.ScreenName}:")) + message = message.Replace($"RT @{tweet.RetweetedTweet.CreatedBy.ScreenName}:", $"[{{RT}} @{tweet.RetweetedTweet.CreatedBy.ScreenName}]{Environment.NewLine}"); else message = message.Replace("RT", "[{{RT}}]"); } @@ -73,10 +89,13 @@ namespace BirdsiteLive.Twitter.Extractors return message; } - public ExtractedMedia[] ExtractMedia(List media) + public ExtractedMedia[] ExtractMedia(ITweet tweet) { - var result = new List(); + var media = tweet.Media; + if (tweet.IsRetweet && tweet.RetweetedTweet != null) + media = tweet.RetweetedTweet.Media; + var result = new List(); foreach (var m in media) { var mediaUrl = GetMediaUrl(m);