remove extraction methods from service

This commit is contained in:
Nicolas Constant 2020-07-22 20:23:26 -04:00
parent fffc9af534
commit 4bd3d4289d
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
2 changed files with 113 additions and 91 deletions

View file

@ -0,0 +1,106 @@
using System.Collections.Generic;
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,
MessageContent = ExtractMessage(tweet),
Media = ExtractMedia(tweet.Media),
CreatedAt = tweet.CreatedAt
};
return extractedTweet;
}
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.QuotedTweet != null) message = $"[Quote RT] {message}";
if (tweet.IsRetweet)
{
if (tweet.RetweetedTweet != null)
message = $"[RT {tweet.RetweetedTweet.CreatedBy.ScreenName}] {tweet.RetweetedTweet.FullText}";
else
message = message.Replace("RT", "[RT]");
}
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":
var ext = Path.GetExtension(mediaUrl);
switch (ext)
{
case ".jpg":
case ".jpeg":
return "image/jpeg";
case ".png":
return "image/png";
}
return null;
case "animated_gif":
return "image/gif";
case "video":
return "video/mp4";
}
return null;
}
}
}

View file

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Twitter.Extractors;
using BirdsiteLive.Twitter.Models;
using Tweetinvi;
using Tweetinvi.Models;
@ -22,11 +23,13 @@ namespace BirdsiteLive.Twitter
public class TwitterService : ITwitterService
{
private readonly TwitterSettings _settings;
private readonly ITweetExtractor _tweetExtractor;
#region Ctor
public TwitterService(TwitterSettings settings)
public TwitterService(TwitterSettings settings, ITweetExtractor tweetExtractor)
{
_settings = settings;
_tweetExtractor = tweetExtractor;
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
}
#endregion
@ -52,97 +55,10 @@ namespace BirdsiteLive.Twitter
{
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
var tweet = Tweet.GetTweet(statusId);
return Extract(tweet);
return _tweetExtractor.Extract(tweet);
}
private ExtractedTweet Extract(ITweet tweet)
{
var extractedTweet = new ExtractedTweet
{
Id = tweet.Id,
InReplyToStatusId = tweet.InReplyToStatusId,
MessageContent = ExtractMessage(tweet),
Media = ExtractMedia(tweet.Media),
CreatedAt = tweet.CreatedAt
};
return extractedTweet;
}
private 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.QuotedTweet != null) message = $"[Quote RT] {message}";
if (tweet.IsRetweet)
{
if (tweet.RetweetedTweet != null)
message = $"[RT {tweet.RetweetedTweet.CreatedBy.ScreenName}] {tweet.RetweetedTweet.FullText}";
else
message = message.Replace("RT", "[RT]");
}
return message;
}
private 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();
}
private 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;
}
}
private string GetMediaType(string mediaType, string mediaUrl)
{
switch (mediaType)
{
case "photo":
var ext = Path.GetExtension(mediaUrl);
switch (ext)
{
case ".jpg":
case ".jpeg":
return "image/jpeg";
case ".png":
return "image/png";
}
return null;
case "animated_gif":
return "image/gif";
case "video":
return "video/mp4";
}
return null;
}
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
{
@ -166,7 +82,7 @@ namespace BirdsiteLive.Twitter
if (timeline != null) tweets.AddRange(timeline);
}
return tweets.Select(Extract).ToArray();
return tweets.Select(_tweetExtractor.Extract).ToArray();
//return tweets.Where(x => returnReplies || string.IsNullOrWhiteSpace(x.InReplyToScreenName)).ToArray();
}
}