refactoring

This commit is contained in:
Vincent Cloutier 2022-05-08 19:19:09 -04:00
parent 49efcd1f97
commit 8244b1edf7
3 changed files with 111 additions and 188 deletions

View file

@ -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<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();
return Array.Empty<ExtractedMedia>();
}
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;
}
}
}

View file

@ -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<TwitterTweetsService> _logger;
private HttpClient _httpClient = new HttpClient();
#region Ctor
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> 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<JsonElement, ExtractedTweet>(_tweetExtractor.Extract).ToArray();
return tweets.RootElement.GetProperty("data").EnumerateArray().Select<JsonElement, ExtractedTweet>(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<ExtractedMedia>(),
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;
}
}
}

View file

@ -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<ExtractedMedia>(),
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