From 4bd3d4289dbf5402574d542767dd7a59acb27b93 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Wed, 22 Jul 2020 20:23:26 -0400 Subject: [PATCH] remove extraction methods from service --- .../Extractors/TweetExtractor.cs | 106 ++++++++++++++++++ src/BirdsiteLive.Twitter/TwitterService.cs | 98 ++-------------- 2 files changed, 113 insertions(+), 91 deletions(-) create mode 100644 src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs diff --git a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs new file mode 100644 index 0000000..7bdb67f --- /dev/null +++ b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs @@ -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 media) + { + var result = new List(); + + 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; + } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive.Twitter/TwitterService.cs b/src/BirdsiteLive.Twitter/TwitterService.cs index ddbaaf0..21853ad 100644 --- a/src/BirdsiteLive.Twitter/TwitterService.cs +++ b/src/BirdsiteLive.Twitter/TwitterService.cs @@ -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 media) - { - var result = new List(); - - 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(); } }