2020-08-01 16:42:32 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-07-22 20:23:26 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using BirdsiteLive.Twitter.Models;
|
|
|
|
|
using Tweetinvi.Models;
|
|
|
|
|
using Tweetinvi.Models.Entities;
|
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Twitter.Extractors
|
|
|
|
|
{
|
|
|
|
|
public interface ITweetExtractor
|
|
|
|
|
{
|
|
|
|
|
ExtractedTweet Extract(ITweet tweet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TweetExtractor : ITweetExtractor
|
|
|
|
|
{
|
|
|
|
|
public ExtractedTweet Extract(ITweet tweet)
|
|
|
|
|
{
|
|
|
|
|
var extractedTweet = new ExtractedTweet
|
|
|
|
|
{
|
|
|
|
|
Id = tweet.Id,
|
|
|
|
|
InReplyToStatusId = tweet.InReplyToStatusId,
|
2020-08-01 00:32:04 -04:00
|
|
|
|
InReplyToAccount = tweet.InReplyToScreenName,
|
2020-07-22 20:23:26 -04:00
|
|
|
|
MessageContent = ExtractMessage(tweet),
|
|
|
|
|
Media = ExtractMedia(tweet.Media),
|
2021-01-22 18:31:30 -05:00
|
|
|
|
CreatedAt = tweet.CreatedAt.ToUniversalTime(),
|
|
|
|
|
IsReply = tweet.InReplyToUserId != null,
|
2021-01-29 01:15:10 -05:00
|
|
|
|
IsThread = tweet.InReplyToUserId != null && tweet.InReplyToUserId == tweet.CreatedBy.Id,
|
|
|
|
|
IsRetweet = tweet.IsRetweet,
|
|
|
|
|
RetweetUrl = ExtractRetweetUrl(tweet)
|
2020-07-22 20:23:26 -04:00
|
|
|
|
};
|
2021-01-29 01:15:10 -05:00
|
|
|
|
|
2020-07-22 20:23:26 -04:00
|
|
|
|
return extractedTweet;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 01:15:10 -05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 20:23:26 -04:00
|
|
|
|
public string ExtractMessage(ITweet tweet)
|
|
|
|
|
{
|
|
|
|
|
var tweetUrls = tweet.Media.Select(x => x.URL).Distinct();
|
|
|
|
|
var message = tweet.FullText;
|
|
|
|
|
foreach (var tweetUrl in tweetUrls)
|
2021-01-29 01:29:35 -05:00
|
|
|
|
{
|
|
|
|
|
if(tweet.IsRetweet)
|
|
|
|
|
message = tweet.RetweetedTweet.FullText.Replace(tweetUrl, string.Empty).Trim();
|
|
|
|
|
else
|
|
|
|
|
message = message.Replace(tweetUrl, string.Empty).Trim();
|
|
|
|
|
}
|
2020-07-22 20:23:26 -04:00
|
|
|
|
|
2021-01-29 01:15:10 -05:00
|
|
|
|
if (tweet.QuotedTweet != null) message = $"[Quote {{RT}}]{Environment.NewLine}{message}";
|
2020-07-22 20:23:26 -04:00
|
|
|
|
if (tweet.IsRetweet)
|
|
|
|
|
{
|
|
|
|
|
if (tweet.RetweetedTweet != null)
|
2021-01-29 01:15:10 -05:00
|
|
|
|
message = $"[{{RT}} @{tweet.RetweetedTweet.CreatedBy.ScreenName}]{Environment.NewLine}{message}";
|
2020-07-22 20:23:26 -04:00
|
|
|
|
else
|
2021-01-29 01:15:10 -05:00
|
|
|
|
message = message.Replace("RT", "[{{RT}}]");
|
2020-07-22 20:23:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 23:57:48 -05:00
|
|
|
|
// Expand URLs
|
|
|
|
|
foreach (var url in tweet.Urls.OrderByDescending(x => x.URL.Length))
|
|
|
|
|
message = message.Replace(url.URL, url.ExpandedURL);
|
|
|
|
|
|
2020-07-22 20:23:26 -04:00
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExtractedMedia[] ExtractMedia(List<IMediaEntity> media)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<ExtractedMedia>();
|
|
|
|
|
|
|
|
|
|
foreach (var m in media)
|
|
|
|
|
{
|
|
|
|
|
var mediaUrl = GetMediaUrl(m);
|
|
|
|
|
var mediaType = GetMediaType(m.MediaType, mediaUrl);
|
|
|
|
|
if (mediaType == null) continue;
|
|
|
|
|
|
|
|
|
|
var att = new ExtractedMedia
|
|
|
|
|
{
|
|
|
|
|
MediaType = mediaType,
|
|
|
|
|
Url = mediaUrl
|
|
|
|
|
};
|
|
|
|
|
result.Add(att);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetMediaUrl(IMediaEntity media)
|
|
|
|
|
{
|
|
|
|
|
switch (media.MediaType)
|
|
|
|
|
{
|
|
|
|
|
case "photo": return media.MediaURLHttps;
|
|
|
|
|
case "animated_gif": return media.VideoDetails.Variants[0].URL;
|
|
|
|
|
case "video": return media.VideoDetails.Variants.OrderByDescending(x => x.Bitrate).First().URL;
|
|
|
|
|
default: return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetMediaType(string mediaType, string mediaUrl)
|
|
|
|
|
{
|
|
|
|
|
switch (mediaType)
|
|
|
|
|
{
|
|
|
|
|
case "photo":
|
2021-01-21 00:28:36 -05:00
|
|
|
|
var pExt = Path.GetExtension(mediaUrl);
|
|
|
|
|
switch (pExt)
|
2020-07-22 20:23:26 -04:00
|
|
|
|
{
|
|
|
|
|
case ".jpg":
|
|
|
|
|
case ".jpeg":
|
|
|
|
|
return "image/jpeg";
|
|
|
|
|
case ".png":
|
|
|
|
|
return "image/png";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
case "animated_gif":
|
2021-01-21 00:28:36 -05:00
|
|
|
|
var vExt = Path.GetExtension(mediaUrl);
|
|
|
|
|
switch (vExt)
|
|
|
|
|
{
|
|
|
|
|
case ".gif":
|
|
|
|
|
return "image/gif";
|
|
|
|
|
case ".mp4":
|
|
|
|
|
return "video/mp4";
|
|
|
|
|
}
|
2020-07-22 20:23:26 -04:00
|
|
|
|
return "image/gif";
|
|
|
|
|
case "video":
|
|
|
|
|
return "video/mp4";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|