/// <summary> /// Upload video /// </summary> /// <param name="video">Video to upload</param> /// <param name="imageThumbnail">Image thumbnail</param> /// <param name="caption">Caption</param> /// <returns></returns> public async Task <IResult <InstaMedia> > UploadVideoAsync(InstaVideo video, InstaImage imageThumbnail, string caption) { ValidateUser(); ValidateLoggedIn(); return(await _mediaProcessor.UploadVideoAsync(video, imageThumbnail, caption)); }
public static InstaVideoUpload AsUpload( this InstaVideo video, string uri) { video.VideoBytes = File.ReadAllBytes(uri); var thumbnail = new InstaImage(); return(new InstaVideoUpload(video, thumbnail)); }
public async Task DoShow() { var mediaVideo = new InstaVideo(@"c:\somevideo.mp4", 1080, 1080, 3); var mediaImage = new InstaImage { Height = 1080, Width = 1080, URI = new Uri(Path.GetFullPath(@"c:\RamtinJokar.jpg"), UriKind.Absolute).LocalPath }; var result = await _instaApi.UploadVideoAsync(mediaVideo, mediaImage, "ramtinak"); Console.WriteLine(result.Succeeded ? $"Media created: {result.Value.Pk}, {result.Value.Caption}" : $"Unable to upload video: {result.Info.Message}"); }
async void Posting() { string message = post.Message; var mediaVideo = new InstaVideo(post.FileNameVideo, 1080, 1080, 3); var mediaImage = new InstaImage { Height = 1080, Width = 1080, URI = post.FileNamePreview }; var result = await api.UploadVideoAsync(mediaVideo, mediaImage, message); infoAcc.Status = "Опубликовано"; EventUpdateGrid(this, new UpdateGridAutopost(infoAcc)); }
public async void UploadVideo() { Assert.True(_authInfo.ApiInstance.IsUserAuthenticated); var mediaVideo = new InstaVideo(Path.GetFullPath(@"../../../../assets/video.mp4"), 640, 480, 3); var mediaImage = new InstaImage { Height = 480, Width = 640, URI = new Uri(Path.GetFullPath(@"../../../../assets/video_image.jpg"), UriKind.Absolute).LocalPath }; var result = await _authInfo.ApiInstance.UploadVideoAsync(mediaVideo, mediaImage, "mountains"); //assert Assert.True(result.Succeeded); Assert.NotNull(result.Value); }
private async Task <IResult <InstaMedia> > ConfigureVideoAsync(InstaVideo video, string uploadId, string caption) { try { var instaUri = UriCreator.GetMediaConfigureUri(); var androidVersion = AndroidVersion.FromString(_deviceInfo.FirmwareFingerprint.Split('/')[2].Split(':')[1]); if (androidVersion == null) { return(Result.Fail("Unsupported android version", (InstaMedia)null)); } var data = new JObject { { "caption", caption }, { "upload_id", uploadId }, { "source_type", "3" }, { "camera_position", "unknown" }, { "extra", new JObject { { "source_width", video.Width }, { "source_height", video.Height } } }, { "clips", new JArray { new JObject { { "length", 10.0 }, { "creation_date", DateTime.Now.ToString("yyyy-dd-MMTh:mm:ss-0fff") }, { "source_type", "3" }, { "camera_position", "back" } } } }, { "poster_frame_index", 0 }, { "audio_muted", false }, { "filter_type", "0" }, { "video_result", "deprecated" }, { "_csrftoken", _user.CsrfToken }, { "_uuid", _deviceInfo.DeviceGuid.ToString() }, { "_uid", _user.LoggedInUser.UserName } }; var request = HttpHelper.GetSignedRequest(HttpMethod.Post, instaUri, _deviceInfo, data); request.Headers.Host = "i.instagram.com"; var response = await _httpRequestProcessor.SendAsync(request); var json = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(Result.UnExpectedResponse <InstaMedia>(response, json)); } var success = await ExposeVideoAsync(uploadId); if (success.Succeeded) { return(Result.Success(success.Value)); } else { return(Result.Fail <InstaMedia>("Cannot expose media")); } } catch (Exception exception) { _logger?.LogException(exception); return(Result.Fail <InstaMedia>(exception)); } }
public async Task <IResult <InstaMedia> > UploadVideoAsync(InstaVideo video, InstaImage imageThumbnail, string caption) { try { var instaUri = UriCreator.GetUploadVideoUri(); var uploadId = ApiRequestMessage.GenerateUploadId(); var requestContent = new MultipartFormDataContent(uploadId) { { new StringContent("2"), "\"media_type\"" }, { new StringContent(uploadId), "\"upload_id\"" }, { new StringContent(_deviceInfo.DeviceGuid.ToString()), "\"_uuid\"" }, { new StringContent(_user.CsrfToken), "\"_csrftoken\"" }, { new StringContent("{\"lib_name\":\"jt\",\"lib_version\":\"1.3.0\",\"quality\":\"87\"}"), "\"image_compression\"" } }; var request = HttpHelper.GetDefaultRequest(HttpMethod.Post, instaUri, _deviceInfo); request.Content = requestContent; var response = await _httpRequestProcessor.SendAsync(request); var json = await response.Content.ReadAsStringUnZipAsync(); var videoResponse = JsonConvert.DeserializeObject <VideoUploadJobResponse>(json); if (videoResponse == null) { return(Result.Fail <InstaMedia>("Failed to get response from instagram video upload endpoint")); } var fileBytes = File.ReadAllBytes(video.Url); var first = videoResponse.VideoUploadUrls[0]; instaUri = new Uri(HttpUtility.UrlDecode(first.Url)); requestContent = new MultipartFormDataContent(uploadId) { { new StringContent(_user.CsrfToken), "\"_csrftoken\"" }, { new StringContent("{\"lib_name\":\"jt\",\"lib_version\":\"1.3.0\",\"quality\":\"87\"}"), "\"image_compression\"" } }; var videoContent = new ByteArrayContent(fileBytes); videoContent.Headers.Add("Content-Transfer-Encoding", "binary"); videoContent.Headers.Add("Content-Type", "application/octet-stream"); videoContent.Headers.Add("Content-Disposition", $"attachment; filename=\"{Path.GetFileName(video.Url)}\""); requestContent.Add(videoContent); request = HttpHelper.GetDefaultRequest(HttpMethod.Post, instaUri, _deviceInfo); request.Content = requestContent; request.Headers.Host = "upload.instagram.com"; request.Headers.Add("Cookie2", "$Version=1"); request.Headers.Add("Session-ID", uploadId); request.Headers.Add("job", first.Job); response = await _httpRequestProcessor.SendAsync(request); json = await response.Content.ReadAsStringUnZipAsync(); await UploadVideoThumbnailAsync(imageThumbnail, uploadId); return(await ConfigureVideoAsync(video, uploadId, caption)); } catch (Exception exception) { _logger?.LogException(exception); return(Result.Fail <InstaMedia>(exception)); } }
private async Task <IResult <InstaMedia> > ConfigureVideoAsync(Action <InstaUploaderProgress> progress, InstaUploaderProgress upProgress, InstaVideo video, string uploadId, string caption, InstaLocationShort location) { try { upProgress.UploadState = InstaUploadState.Configuring; progress?.Invoke(upProgress); var instaUri = UriCreator.GetMediaConfigureUri(); var data = new JObject { { "caption", caption ?? string.Empty }, { "upload_id", uploadId }, { "source_type", "3" }, { "camera_position", "unknown" }, { "extra", new JObject { { "source_width", 0 }, { "source_height", 0 } } }, { "clips", new JArray { new JObject { { "length", 0 }, { "creation_date", DateTime.Now.ToString("yyyy-dd-MMTh:mm:ss-0fff") }, { "source_type", "3" }, { "camera_position", "back" } } } }, { "poster_frame_index", 0 }, { "audio_muted", false }, { "filter_type", "0" }, { "video_result", "" }, { "_csrftoken", _user.CsrfToken }, { "_uuid", _deviceInfo.DeviceGuid.ToString() }, { "_uid", _user.LoggedInUser.UserName } }; if (location != null) { data.Add("location", location.GetJson()); data.Add("date_time_digitalized", DateTime.Now.ToString("yyyy:dd:MM+h:mm:ss")); } var request = _httpHelper.GetSignedRequest(HttpMethod.Post, instaUri, _deviceInfo, data); request.Headers.Host = "i.instagram.com"; var response = await _httpRequestProcessor.SendAsync(request); var json = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { upProgress.UploadState = InstaUploadState.Error; progress?.Invoke(upProgress); return(Result.UnExpectedResponse <InstaMedia>(response, json)); } upProgress.UploadState = InstaUploadState.Configured; progress?.Invoke(upProgress); var success = await ExposeVideoAsync(uploadId, caption, location); if (success.Succeeded) { upProgress.UploadState = InstaUploadState.Completed; progress?.Invoke(upProgress); return(success); } upProgress.UploadState = InstaUploadState.Error; progress?.Invoke(upProgress); return(Result.Fail <InstaMedia>("Cannot expose media")); } catch (Exception exception) { upProgress.UploadState = InstaUploadState.Error; progress?.Invoke(upProgress); _logger?.LogException(exception); return(Result.Fail <InstaMedia>(exception)); } }
public InstaVideoUpload(InstaVideo video, InstaImage videoThumbnail) { Video = video; VideoThumbnail = videoThumbnail; }
public static async Task SendFile(this InstaDirectInboxThreadWrapper thread, StorageFile file, Action <UploaderProgress> progress) { try { if (file.ContentType.Contains("image", StringComparison.OrdinalIgnoreCase)) { var properties = await file.Properties.GetImagePropertiesAsync(); int imageHeight = (int)properties.Height; int imageWidth = (int)properties.Width; IBuffer buffer; if (properties.Width > 1080 || properties.Height > 1080) { buffer = await Helpers.CompressImage(file, 1080, 1080); double widthRatio = (double)1080 / imageWidth; double heightRatio = (double)1080 / imageHeight; double scaleRatio = Math.Min(widthRatio, heightRatio); imageHeight = (int)Math.Floor(imageHeight * scaleRatio); imageWidth = (int)Math.Floor(imageWidth * scaleRatio); } else { if (file.FileType.Contains("png", StringComparison.OrdinalIgnoreCase)) { buffer = await Helpers.CompressImage(file, imageWidth, imageHeight); } else { buffer = await FileIO.ReadBufferAsync(file); } } await SendBuffer(thread, buffer, imageWidth, imageHeight, progress); } // Not yet tested if (file.ContentType.Contains("video", StringComparison.OrdinalIgnoreCase)) { var properties = await file.Properties.GetVideoPropertiesAsync(); if (properties.Duration > TimeSpan.FromMinutes(1)) { return; } var buffer = await FileIO.ReadBufferAsync(file); var instaVideo = new InstaVideo() { UploadBuffer = buffer, Width = (int)properties.Width, Height = (int)properties.Height, }; var thumbnail = await file.GetThumbnailAsync(ThumbnailMode.VideosView); var thumbnailBuffer = new Windows.Storage.Streams.Buffer((uint)thumbnail.Size); await thumbnail.ReadAsync(thumbnailBuffer, (uint)thumbnail.Size, InputStreamOptions.None); var thumbnailImage = new InstaImage() { UploadBuffer = thumbnailBuffer, Width = (int)thumbnail.OriginalWidth, Height = (int)thumbnail.OriginalHeight }; await Api.SendDirectVideoAsync(progress, new InstaVideoUpload(instaVideo, thumbnailImage), thread.ThreadId); } } catch (Exception e) { DebugLogger.LogException(e); //await HandleException("Failed to send message"); } }
private static async void RunPostTaskAsync(TaskModel t, IInstaApi api) { t = t.Refreshed; if (t.GetValues().Images.Count > 0) { if (t.GetValues().Images.Count > 1) { var _imgs = new List <InstaImage>(); foreach (var img in t.GetValues().Images) { _imgs.Add(new InstaImage { URI = img.StandardResolution.Url, Height = img.StandardResolution.Height, Width = img.StandardResolution.Width }); } ; var result = await api.UploadPhotosAlbumAsync(_imgs.ToArray(), await Helper.ConstructCaptionTextAsync(t.GetValues().Text, t.Searches, api)); if (result.Succeeded) { t.TaskExecuted(); Helper.Log($"Task Executed: {t.Name} - {t.TaskType.ToString()} {Environment.NewLine} {JsonConvert.SerializeObject(result.Value)}"); } else { Helper.Log(JsonConvert.SerializeObject(result.Info)); } } else { var img = t.GetValues().Images.First(); var _img = new InstaImage { URI = img.StandardResolution.Url, Height = img.StandardResolution.Height, Width = img.StandardResolution.Width }; var result = await api.UploadPhotoAsync(_img, await Helper.ConstructCaptionTextAsync(t.GetValues().Text, t.Searches, api)); if (result.Succeeded) { t.TaskExecuted(); Helper.Log($"Task Executed: {t.Name} - {t.TaskType.ToString()} {Environment.NewLine} {JsonConvert.SerializeObject(result.Value)}"); } else { Helper.Log(JsonConvert.SerializeObject(result.Info)); } } } else if (t.GetValues().Videos.Count > 0) { var vid = t.GetValues().Videos.First(); InstaVideo _vid = new InstaVideo ( vid.StandardResolution.Url, vid.StandardResolution.Width, vid.StandardResolution.Height, 1 ); var _img = new InstaImage { URI = @"\src\img\Header.jpg", Height = 100, Width = 100 }; var result = await api.UploadVideoAsync(_vid, _img, await Helper.ConstructCaptionTextAsync(t.GetValues().Text, t.Searches, api)); if (result.Succeeded) { t.TaskExecuted(); Helper.Log($"Task Executed: {t.Name} - {t.TaskType.ToString()} {Environment.NewLine} {JsonConvert.SerializeObject(result.Value)}"); } else { Helper.Log(JsonConvert.SerializeObject(result.Info)); } } }