added media

This commit is contained in:
Vincent Cloutier 2022-11-27 15:41:55 -05:00
parent 068f0af344
commit 35bc724c92
2 changed files with 42 additions and 23 deletions

View file

@ -202,6 +202,25 @@ namespace BirdsiteLive.Twitter
string creationTime = tweet.GetProperty("content").GetProperty("itemContent")
.GetProperty("tweet_results").GetProperty("result").GetProperty("legacy")
.GetProperty("created_at").GetString().Replace(" +0000", "");
JsonElement extendedEntities;
bool hasMedia = tweet.GetProperty("content").GetProperty("itemContent")
.GetProperty("tweet_results").GetProperty("result").GetProperty("legacy")
.TryGetProperty("extended_entities", out extendedEntities);
List<ExtractedMedia> Media = new List<ExtractedMedia>();
if (hasMedia)
{
foreach (JsonElement media in extendedEntities.GetProperty("media").EnumerateArray())
{
var m = new ExtractedMedia
{
MediaType = GetMediaType(media.GetProperty("type").GetString(), media.GetProperty("media_url_https").GetString()),
Url = media.GetProperty("media_url_https").GetString(),
};
Media.Add(m);
}
}
var extractedTweet = new ExtractedTweet
{
Id = Int64.Parse(tweet.GetProperty("sortIndex").GetString()),
@ -212,7 +231,7 @@ namespace BirdsiteLive.Twitter
IsReply = isReply,
IsThread = false,
IsRetweet = isRetweet,
Media = null,
Media = Media.Count() == 0 ? null : Media.ToArray(),
RetweetUrl = "https://t.co/123",
OriginalAuthor = OriginalAuthor,
};

View file

@ -56,10 +56,30 @@ namespace BirdsiteLive.Twitter
var c = await httpResponse.Content.ReadAsStringAsync();
res = JsonDocument.Parse(c);
}
var result = res.RootElement.GetProperty("data").GetProperty("user").GetProperty("result");
string profileBannerURL = null;
JsonElement profileBannerURLObject;
if (result.GetProperty("legacy").TryGetProperty("profile_banner_url", out profileBannerURLObject))
{
profileBannerURL = profileBannerURLObject.GetString();
}
return new TwitterUser
{
Id = long.Parse(result.GetProperty("rest_id").GetString()),
Acct = username,
Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(),
Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(),
Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(),
ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString().Replace("normal", "400x400"),
ProfileBackgroundImageUrl = profileBannerURL,
ProfileBannerURL = profileBannerURL,
Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(),
};
}
catch (HttpRequestException e)
catch (System.Collections.Generic.KeyNotFoundException)
{
throw;
throw new UserNotFoundException();
//if (e.TwitterExceptionInfos.Any(x => x.Message.ToLowerInvariant().Contains("User has been suspended".ToLowerInvariant())))
//{
// throw new UserHasBeenSuspendedException();
@ -88,26 +108,6 @@ namespace BirdsiteLive.Twitter
//foreach (var descriptionUrl in user.Entities?.Description?.Urls?.OrderByDescending(x => x.URL.Length))
// description = description.Replace(descriptionUrl.URL, descriptionUrl.ExpandedURL);
var result = res.RootElement.GetProperty("data").GetProperty("user").GetProperty("result");
string profileBannerURL = null;
JsonElement profileBannerURLObject;
if (result.GetProperty("legacy").TryGetProperty("profile_banner_url", out profileBannerURLObject))
{
profileBannerURL = profileBannerURLObject.GetString();
}
return new TwitterUser
{
Id = long.Parse(result.GetProperty("rest_id").GetString()),
Acct = username,
Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(),
Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(),
Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(),
ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString().Replace("normal", "400x400"),
ProfileBackgroundImageUrl = profileBannerURL,
ProfileBannerURL = profileBannerURL,
Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(),
};
}