This commit is contained in:
Vincent Cloutier 2022-05-07 12:37:40 -04:00
parent 6b2579db50
commit d796a6c52d
2 changed files with 12 additions and 14 deletions

View file

@ -9,21 +9,21 @@ namespace BirdsiteLive.Twitter.Extractors
{
public interface ITweetExtractor
{
ExtractedTweet Extract(JsonDocument tweet);
ExtractedTweet Extract(JsonElement tweet);
}
public class TweetExtractor : ITweetExtractor
{
public ExtractedTweet Extract(JsonDocument tweet)
public ExtractedTweet Extract(JsonElement tweet)
{
var extractedTweet = new ExtractedTweet
{
Id = tweet.RootElement.GetProperty("data").GetProperty("id").GetInt64(),
InReplyToStatusId = tweet.RootElement.GetProperty("data").GetProperty("in_reply_to_status_id").GetInt64(),
InReplyToAccount = tweet.RootElement.GetProperty("data").GetProperty("in_reply_to_status_id").GetString(),
Id = tweet.GetProperty("id").GetInt64(),
InReplyToStatusId = null, //tweet.GetProperty("in_reply_to_status_id").GetInt64(),
InReplyToAccount = null, //tweet.GetProperty("in_reply_to_user_id").GetString(),
MessageContent = ExtractMessage(tweet),
Media = ExtractMedia(tweet),
CreatedAt = tweet.RootElement.GetProperty("data").GetProperty("in_reply_to_status_id").GetDateTime(),
CreatedAt = DateTime.Now, // tweet.GetProperty("data").GetProperty("in_reply_to_status_id").GetDateTime(),
IsReply = false,
IsThread = false,
IsRetweet = false,
@ -33,16 +33,16 @@ namespace BirdsiteLive.Twitter.Extractors
return extractedTweet;
}
private string ExtractRetweetUrl(JsonDocument tweet)
private string ExtractRetweetUrl(JsonElement tweet)
{
var retweetId = "123";
return $"https://t.co/{retweetId}";
}
private string ExtractMessage(JsonDocument tweet)
private string ExtractMessage(JsonElement tweet)
{
return "hello world";
return tweet.GetProperty("text").GetString();
//var message = tweet.FullText;
//var tweetUrls = tweet.Media.Select(x => x.URL).Distinct();
@ -78,7 +78,7 @@ namespace BirdsiteLive.Twitter.Extractors
//return message;
}
private ExtractedMedia[] ExtractMedia(JsonDocument tweet)
private ExtractedMedia[] ExtractMedia(JsonElement tweet)
{
//var media = tweet.Media;
//if (tweet.IsRetweet && tweet.RetweetedTweet != null)

View file

@ -62,7 +62,7 @@ namespace BirdsiteLive.Twitter
_statisticsHandler.CalledTweetApi();
if (tweet == null) return null; //TODO: test this
return _tweetExtractor.Extract(tweet);
return _tweetExtractor.Extract(tweet.RootElement);
}
catch (Exception e)
{
@ -105,9 +105,7 @@ namespace BirdsiteLive.Twitter
return null;
}
return Array.Empty<ExtractedTweet>();
//return tweets.RootElement.GetProperty("data").Select(_tweetExtractor.Extract).ToArray();
return tweets.RootElement.GetProperty("data").EnumerateArray().Select<JsonElement, ExtractedTweet>(_tweetExtractor.Extract).ToArray();
}
}
}