diff --git a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs index 788f046..2746293 100644 --- a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs +++ b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs @@ -224,10 +224,31 @@ namespace BirdsiteLive.Twitter { foreach (JsonElement media in extendedEntities.GetProperty("media").EnumerateArray()) { + var type = media.GetProperty("type").GetString(); + string url = ""; + if (type == "video") + { + var bitrate = 0; + foreach (JsonElement v in media.GetProperty("video_info").GetProperty("variants").EnumerateArray()) + { + if (v.GetProperty("content_type").GetString() != "video/mp4") + continue; + int vBitrate = v.GetProperty("bitrate").GetInt32(); + if (vBitrate > bitrate) + { + bitrate = vBitrate; + url = v.GetProperty("url").GetString(); + } + } + } + else + { + url = media.GetProperty("media_url_https").GetString(); + } var m = new ExtractedMedia { - MediaType = GetMediaType(media.GetProperty("type").GetString(), media.GetProperty("media_url_https").GetString()), - Url = media.GetProperty("media_url_https").GetString(), + MediaType = GetMediaType(type, media.GetProperty("media_url_https").GetString()), + Url = url, }; Media.Add(m); diff --git a/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs b/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs index b81e3b1..7f57f96 100644 --- a/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs +++ b/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs @@ -17,7 +17,7 @@ namespace BirdsiteLive.ActivityPub.Tests { var logger1 = new Mock>(MockBehavior.Strict); var logger2 = new Mock>(MockBehavior.Strict); - var logger3 = new Mock>(MockBehavior.Strict); + var logger3 = new Mock>(); var stats = new Mock(); ITwitterAuthenticationInitializer auth = new TwitterAuthenticationInitializer(logger1.Object); ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object); @@ -37,7 +37,8 @@ namespace BirdsiteLive.ActivityPub.Tests var tweet = await _tweetService.GetTweetAsync(1593344577385160704); Assert.AreEqual(tweet.MessageContent, "Speaker Nancy Pelosi will go down as one of most accomplished legislators in American history—breaking barriers, opening doors for others, and working every day to serve the American people. I couldn’t be more grateful for her friendship and leadership."); - // TODO validate media type and length + // TODO validate media type + Assert.AreEqual(tweet.Media.Length, 1); // TODO test alt-text of images } @@ -47,5 +48,16 @@ namespace BirdsiteLive.ActivityPub.Tests var tweet = await _tweetService.GetTweetAsync(1602618920996945922); Assert.AreEqual(tweet.MessageContent, "#Linux 6.2 Expands Support For More #Qualcomm #Snapdragon SoCs, #Apple M1 Pro/Ultra/Max\n\nhttps://www.phoronix.com/news/Linux-6.2-Arm-SoC-Updates"); } + + [TestMethod] + public async Task SimpleTextAndSingleVideoTweet() + { + var tweet = await _tweetService.GetTweetAsync(1604231025311129600); + Assert.AreEqual(tweet.MessageContent, "Falcon 9’s first stage has landed on the Just Read the Instructions droneship, completing the 15th launch and landing of this booster!"); + + Assert.AreEqual(tweet.Media.Length, 1); + Assert.AreEqual(tweet.Media[0].MediaType, "video/mp4"); + Assert.IsTrue(tweet.Media[0].Url.StartsWith("https://video.twimg.com/")); + } } } \ No newline at end of file