diff --git a/Instagram Reels Bot/Helpers/Instagram/InstagramProcessor.cs b/Instagram Reels Bot/Helpers/Instagram/InstagramProcessor.cs index 0fec0f5..34e75de 100644 --- a/Instagram Reels Bot/Helpers/Instagram/InstagramProcessor.cs +++ b/Instagram Reels Bot/Helpers/Instagram/InstagramProcessor.cs @@ -1,15 +1,15 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; +using System.Web; using Discord.WebSocket; +using Instagram_Reels_Bot.Helpers.Instagram; using InstagramApiSharp; using InstagramApiSharp.Classes; using Microsoft.Extensions.Configuration; -using System.Linq; -using Instagram_Reels_Bot.Helpers.Instagram; -using System.Web; namespace Instagram_Reels_Bot.Helpers { @@ -492,8 +492,25 @@ public async Task PostProcessorAsync(string url, int //Video or image: if (isVideo) { - //video: - downloadUrl = media.Videos[0].Uri; + //process video: + List SortedVideo = media.Videos.OrderByDescending(o => o.Height).ToList(); + + // Find video sizes: + foreach (var video in SortedVideo) + { + long size = await GetMediaSize(new Uri(video.Uri)); + // Use video if size is below the max and known + if (size <= maxUploadSize && size != 0) + { + downloadUrl = video.Uri; + break; + } + } + // Get largest if no video below max: + if (string.IsNullOrEmpty(downloadUrl)) + { + downloadUrl = SortedVideo.First().Uri; + } } else { @@ -590,7 +607,24 @@ public async Task StoryProcessorAsync(string url, in if (isVideo) { //process video: - downloadUrl = story.VideoList[0].Uri; + List SortedVideo = story.VideoList.OrderByDescending(o => o.Height).ToList(); + + // Find video sizes: + foreach (var video in SortedVideo) + { + long size = await GetMediaSize(new Uri(video.Uri)); + // Use video if size is below the max and known + if (size <= maxUploadSize && size != 0) + { + downloadUrl = video.Uri; + break; + } + } + // Get largest if no video below max: + if (string.IsNullOrEmpty(downloadUrl)) + { + downloadUrl = SortedVideo.First().Uri; + } } else if (story.ImageList.Count > 0) { @@ -705,6 +739,30 @@ public async Task GetLatestPostDate(long userID) } return LatestMedia[0].TakenAt; } + /// + /// Queries the size of a file from a website + /// + /// The address to get the size from + /// A long with the size in bytes + private async Task GetMediaSize(Uri addr) + { + using (HttpClient client = new HttpClient()) + { + using (var request = new HttpRequestMessage(HttpMethod.Head, addr)) + { + request.Headers.Add("User-Agent", instaApi.GetUserAgent()); + using (var response = await client.SendAsync(request)) + { + long value = 0; + if (response.Content.Headers.ContentLength != null) + { + value = response.Content.Headers.ContentLength.Value; + } + return value; + } + } + } + } #endregion Media #region Errors ///