From 8244b1edf72294f48d108e026a0fbb3dbb21fd2a Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Sun, 8 May 2022 19:19:09 -0400 Subject: [PATCH] refactoring --- .../Extractors/TweetExtractor.cs | 182 ------------------ .../TwitterTweetsService.cs | 63 +++++- .../TwitterUserService.cs | 54 ++++++ 3 files changed, 111 insertions(+), 188 deletions(-) delete mode 100644 src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs diff --git a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs b/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs deleted file mode 100644 index 4cea323..0000000 --- a/src/BirdsiteLive.Twitter/Extractors/TweetExtractor.cs +++ /dev/null @@ -1,182 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.Json; -using BirdsiteLive.Twitter.Models; - -namespace BirdsiteLive.Twitter.Extractors -{ - public interface ITweetExtractor - { - ExtractedTweet Extract(JsonElement tweet); - } - - public class TweetExtractor : ITweetExtractor - { - - private readonly ITwitterTweetsService _twitterTweetsService; - - public TweetExtractor(ITwitterTweetsService twitterTweetsService) - { - _twitterTweetsService = twitterTweetsService; - } - - public ExtractedTweet Extract(JsonElement tweet) - { - bool IsRetweet = false; - bool IsReply = false; - long? replyId = null; - JsonElement replyAccount; - string? replyAccountString = null; - JsonElement referenced_tweets; - if(tweet.TryGetProperty("in_reply_to_user_id", out replyAccount)) - { - replyAccountString = replyAccount.GetString(); - - } - if(tweet.TryGetProperty("referenced_tweets", out referenced_tweets)) - { - var first = referenced_tweets.EnumerateArray().ToList()[0]; - if (first.GetProperty("type").GetString() == "retweeted") - { - IsRetweet = true; - var statusId = Int64.Parse(first.GetProperty("id").GetString()); - var extracted = _twitterTweetsService.GetTweet(statusId); - extracted.IsRetweet = true; - return extracted; - - } - if (first.GetProperty("type").GetString() == "replied_to") - { - IsReply = true; - replyId = Int64.Parse(first.GetProperty("id").GetString()); - } - if (first.GetProperty("type").GetString() == "quoted") - { - IsReply = true; - replyId = Int64.Parse(first.GetProperty("id").GetString()); - } - } - - var extractedTweet = new ExtractedTweet - { - Id = Int64.Parse(tweet.GetProperty("id").GetString()), - InReplyToStatusId = replyId, - InReplyToAccount = replyAccountString, - MessageContent = ExtractMessage(tweet), - Media = ExtractMedia(tweet), - CreatedAt = DateTime.Now, // tweet.GetProperty("data").GetProperty("in_reply_to_status_id").GetDateTime(), - IsReply = IsReply, - IsThread = false, - IsRetweet = IsRetweet, - RetweetUrl = ExtractRetweetUrl(tweet) - }; - - return extractedTweet; - } - - private string ExtractRetweetUrl(JsonElement tweet) - { - var retweetId = "123"; - return $"https://t.co/{retweetId}"; - - } - - private string ExtractMessage(JsonElement tweet) - { - return tweet.GetProperty("text").GetString(); - //var message = tweet.FullText; - //var tweetUrls = tweet.Media.Select(x => x.URL).Distinct(); - - //if (tweet.IsRetweet && 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) - // message = tweet.RetweetedTweet.FullText.Replace(tweetUrl, string.Empty).Trim(); - // else - // message = message.Replace(tweetUrl, string.Empty).Trim(); - //} - - //if (tweet.QuotedTweet != null) message = $"[Quote {{RT}}]{Environment.NewLine}{message}"; - //if (tweet.IsRetweet) - //{ - // 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}}]"); - //} - - //// Expand URLs - //foreach (var url in tweet.Urls.OrderByDescending(x => x.URL.Length)) - // message = message.Replace(url.URL, url.ExpandedURL); - - //return message; - } - - private ExtractedMedia[] ExtractMedia(JsonElement tweet) - { - //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); - // var mediaType = GetMediaType(m.MediaType, mediaUrl); - // if (mediaType == null) continue; - - // var att = new ExtractedMedia - // { - // MediaType = mediaType, - // Url = mediaUrl - // }; - // result.Add(att); - //} - - //return result.ToArray(); - return Array.Empty(); - } - - - private string GetMediaType(string mediaType, string mediaUrl) - { - switch (mediaType) - { - case "photo": - var pExt = Path.GetExtension(mediaUrl); - switch (pExt) - { - case ".jpg": - case ".jpeg": - return "image/jpeg"; - case ".png": - return "image/png"; - } - return null; - - case "animated_gif": - var vExt = Path.GetExtension(mediaUrl); - switch (vExt) - { - case ".gif": - return "image/gif"; - case ".mp4": - return "video/mp4"; - } - return "image/gif"; - case "video": - return "video/mp4"; - } - return null; - } - } -} diff --git a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs index a01306c..45b802d 100644 --- a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs +++ b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs @@ -6,7 +6,6 @@ using System.Text.Json; using System.Threading.Tasks; using BirdsiteLive.Common.Settings; using BirdsiteLive.Statistics.Domain; -using BirdsiteLive.Twitter.Extractors; using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Tools; using Microsoft.Extensions.Logging; @@ -22,17 +21,15 @@ namespace BirdsiteLive.Twitter public class TwitterTweetsService : ITwitterTweetsService { private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer; - private readonly ITweetExtractor _tweetExtractor; private readonly ITwitterStatisticsHandler _statisticsHandler; private readonly ITwitterUserService _twitterUserService; private readonly ILogger _logger; private HttpClient _httpClient = new HttpClient(); #region Ctor - public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger logger) + public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger logger) { _twitterAuthenticationInitializer = twitterAuthenticationInitializer; - _tweetExtractor = tweetExtractor; _statisticsHandler = statisticsHandler; _twitterUserService = twitterUserService; _logger = logger; @@ -62,7 +59,7 @@ namespace BirdsiteLive.Twitter _statisticsHandler.CalledTweetApi(); if (tweet == null) return null; //TODO: test this - return _tweetExtractor.Extract(tweet.RootElement); + return Extract(tweet.RootElement); } catch (Exception e) { @@ -110,7 +107,61 @@ namespace BirdsiteLive.Twitter return null; } - return tweets.RootElement.GetProperty("data").EnumerateArray().Select(_tweetExtractor.Extract).ToArray(); + return tweets.RootElement.GetProperty("data").EnumerateArray().Select(Extract).ToArray(); + } + + public ExtractedTweet Extract(JsonElement tweet) + { + bool IsRetweet = false; + bool IsReply = false; + long? replyId = null; + JsonElement replyAccount; + string? replyAccountString = null; + JsonElement referenced_tweets; + if(tweet.TryGetProperty("in_reply_to_user_id", out replyAccount)) + { + replyAccountString = replyAccount.GetString(); + + } + if(tweet.TryGetProperty("referenced_tweets", out referenced_tweets)) + { + var first = referenced_tweets.EnumerateArray().ToList()[0]; + if (first.GetProperty("type").GetString() == "retweeted") + { + IsRetweet = true; + var statusId = Int64.Parse(first.GetProperty("id").GetString()); + var extracted = GetTweet(statusId); + extracted.IsRetweet = true; + return extracted; + + } + if (first.GetProperty("type").GetString() == "replied_to") + { + IsReply = true; + replyId = Int64.Parse(first.GetProperty("id").GetString()); + } + if (first.GetProperty("type").GetString() == "quoted") + { + IsReply = true; + replyId = Int64.Parse(first.GetProperty("id").GetString()); + } + } + + var extractedTweet = new ExtractedTweet + { + Id = Int64.Parse(tweet.GetProperty("id").GetString()), + InReplyToStatusId = replyId, + InReplyToAccount = replyAccountString, + MessageContent = tweet.GetProperty("text").GetString(), + Media = Array.Empty(), + CreatedAt = DateTime.Now, // tweet.GetProperty("data").GetProperty("in_reply_to_status_id").GetDateTime(), + IsReply = IsReply, + IsThread = false, + IsRetweet = IsRetweet, + RetweetUrl = "https://t.co/123" + }; + + return extractedTweet; } } } diff --git a/src/BirdsiteLive.Twitter/TwitterUserService.cs b/src/BirdsiteLive.Twitter/TwitterUserService.cs index 6a8ecc7..9a2f68e 100644 --- a/src/BirdsiteLive.Twitter/TwitterUserService.cs +++ b/src/BirdsiteLive.Twitter/TwitterUserService.cs @@ -108,6 +108,60 @@ namespace BirdsiteLive.Twitter }; } + + public ExtractedTweet Extract(JsonElement tweet) + { + bool IsRetweet = false; + bool IsReply = false; + long? replyId = null; + JsonElement replyAccount; + string? replyAccountString = null; + JsonElement referenced_tweets; + if(tweet.TryGetProperty("in_reply_to_user_id", out replyAccount)) + { + replyAccountString = replyAccount.GetString(); + + } + if(tweet.TryGetProperty("referenced_tweets", out referenced_tweets)) + { + var first = referenced_tweets.EnumerateArray().ToList()[0]; + if (first.GetProperty("type").GetString() == "retweeted") + { + IsRetweet = true; + var statusId = Int64.Parse(first.GetProperty("id").GetString()); + var extracted = GetTweet(statusId); + extracted.IsRetweet = true; + return extracted; + + } + if (first.GetProperty("type").GetString() == "replied_to") + { + IsReply = true; + replyId = Int64.Parse(first.GetProperty("id").GetString()); + } + if (first.GetProperty("type").GetString() == "quoted") + { + IsReply = true; + replyId = Int64.Parse(first.GetProperty("id").GetString()); + } + } + + var extractedTweet = new ExtractedTweet + { + Id = Int64.Parse(tweet.GetProperty("id").GetString()), + InReplyToStatusId = replyId, + InReplyToAccount = replyAccountString, + MessageContent = tweet.GetProperty("text").GetString(), + Media = Array.Empty(), + CreatedAt = DateTime.Now, // tweet.GetProperty("data").GetProperty("in_reply_to_status_id").GetDateTime(), + IsReply = IsReply, + IsThread = false, + IsRetweet = IsRetweet, + RetweetUrl = "https://t.co/123" + }; + + return extractedTweet; + } public bool IsUserApiRateLimited() { // Retrieve limit from tooling