better retweet extraction

This commit is contained in:
Nicolas Constant 2021-01-29 01:15:10 -05:00
parent b942a528f2
commit 514b727328
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
3 changed files with 32 additions and 5 deletions

View file

@ -46,6 +46,17 @@ namespace BirdsiteLive.Domain
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}",
$@"<a href=""{tweet.RetweetUrl}"" rel=""nofollow noopener noreferrer"" target=""_blank"">RT</a>");
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 @@ namespace BirdsiteLive.Domain
cc = new string[0],
sensitive = false,
content = $"<p>{extractedTags.content}</p>",
content = $"<p>{content}</p>",
attachment = Convert(tweet.Media),
tag = extractedTags.tags
};

View file

@ -26,11 +26,25 @@ namespace BirdsiteLive.Twitter.Extractors
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 @@ namespace BirdsiteLive.Twitter.Extractors
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

View file

@ -13,5 +13,7 @@ namespace BirdsiteLive.Twitter.Models
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; }
}
}