Ejemplo n.º 1
0
        public static async void Download(string url, string thumbnail,
                                          bool isVideo = false, string username = null, string caption = null, bool sendNotify = false, bool story = false, bool userDownloader = false)
        {
            try
            {
                StorageFolder folder;
                if (story)
                {
                    folder = await Helper.GetPictureFolderForStories();
                }
                else
                {
                    folder = await Helper.GetPictureFolder();
                }
                var date = DateTime.UtcNow;

                //DateTime.Now.ToString("yyyy-dd-MMTh:mm:ss-0fffZ")
                var name = $"{username?.ToUpper()}_IMG_{date.ToString("yyyyddMM_hmmssfff", CultureInfo.CurrentCulture)}.jpg";
                if (isVideo)
                {
                    name = $"{username?.ToUpper()}_VID_{date.ToString("yyyyddMM_hmmssfff", CultureInfo.CurrentCulture)}.mp4";
                }

                var destinationFile = await folder.CreateFileAsync(name, CreationCollisionOption.GenerateUniqueName);


                if (!string.IsNullOrEmpty(caption))
                {
                    if (caption.Length > 110)
                    {
                        caption = caption.Substring(0, 108);
                    }
                }
                if (caption == null)
                {
                    caption = string.Empty;
                }
                caption = ReplaceInvalidXmlCharacterReferences(caption);
                ToastNotification failed  = null;
                ToastNotification success = null;
                if (userDownloader)
                {
                    failed = NotificationHelper.GetSingleUserNotify(username, thumbnail, "Download failed");

                    success = NotificationHelper.GetSuccessUserNotify(username, thumbnail);
                }
                else
                {
                    try
                    {
                        failed = NotificationHelper.GetFailedNotify(caption, thumbnail);
                    }
                    catch
                    {
                        failed = NotificationHelper.GetFailedNotify(null, thumbnail);
                    }
                    try
                    {
                        success = NotificationHelper.GetSuccessNotify(caption, thumbnail);
                    }
                    catch
                    {
                        success = NotificationHelper.GetSuccessNotify(null, thumbnail);
                    }
                }
                BackgroundDownloader downloader = new BackgroundDownloader
                {
                    FailureToastNotification = failed,
                    SuccessToastNotification = success
                };
                if (sendNotify)
                {
                    MainPage.Current.ShowInAppNotify($"Download started...", 1200);
                }

                DownloadOperation download = downloader.CreateDownload(new Uri(url), destinationFile);
                await download.StartAsync().AsTask();
            }
            catch (Exception ex) { ex.PrintException("Download"); }
        }
Ejemplo n.º 2
0
        private async Task <IResult <bool> > ConfigureVideoAsync()
        {
            try
            {
                Debug.WriteLine("----------------------------------------ConfigurePhotoAsync----------------------------------");
                var instaUri     = GetMediaConfigureUri();
                var retryContext = GetRetryContext();
                var _user        = InstaApi.GetLoggedUser();
                var _deviceInfo  = InstaApi.GetCurrentDevice();
                var data         = new JObject
                {
                    //{"date_time_digitalized", DateTime.UtcNow.ToString("yyyy:MM:dd+hh:mm:ss")},
                    //{"date_time_original", DateTime.UtcNow.ToString("yyyy:MM:dd+hh:mm:ss")},
                    //{"is_suggested_venue", "false"},
                    { "filter_type", "0" },
                    { "timezone_offset", InstaApi.GetTimezoneOffset().ToString() },
                    { "_csrftoken", _user.CsrfToken },
                    { "media_folder", "Camera" },
                    { "source_type", "4" },
                    { "_uid", _user.LoggedInUser.Pk.ToString() },
                    { "_uuid", _deviceInfo.DeviceGuid.ToString() },
                    { "device_id", _deviceInfo.DeviceId },
                    { "caption", Caption ?? string.Empty },
                    { "upload_id", UploadId },
                    { "date_time_original", "19040101T000000.000Z" },
                    {
                        "device", new JObject {
                            { "manufacturer", _deviceInfo.HardwareManufacturer },
                            { "model", _deviceInfo.DeviceModelIdentifier },
                            { "android_release", _deviceInfo.AndroidVer.VersionNumber },
                            { "android_version", int.Parse(_deviceInfo.AndroidVer.APILevel) }
                        }
                    },
                    { "length", Duration },
                    {
                        "extra", new JObject
                        {
                            { "source_width", 0 },
                            { "source_height", 0 }
                        }
                    },
                    {
                        "clips", new JArray {
                            new JObject
                            {
                                { "length", Duration },
                                //{"creation_date", DateTime.Now.ToString("yyyy-dd-MMTh:mm:ss-0fff")},
                                { "source_type", "4" },
                                //{"camera_position", "back"}
                            }
                        }
                    },
                    { "audio_muted", false },
                    { "poster_frame_index", 0 },
                };


                var _httpRequestProcessor = InstaApi.HttpRequestProcessor;
                var request = InstaApi.HttpHelper.GetSignedRequest(HttpMethod.Post, instaUri, _deviceInfo, data);
                request.Headers.Add("retry_context", GetRetryContext());
                var response = await _httpRequestProcessor.SendAsync(request);

                var json = await response.Content.ReadAsStringAsync();

                var mediaResponse = JsonConvert.DeserializeObject <InstaMediaItemResponse>(json, new InstaMediaDataConverter());
                var converter     = ConvertersFabric.Instance.GetSingleMediaConverter(mediaResponse);
                try
                {
                    var obj = converter.Convert();
                    if (obj != null)
                    {
                        Views.Main.MainView.Current?.MainVM.PostsGenerator.Items.Insert(0, new InstaPost
                        {
                            Media = obj,
                            Type  = InstagramApiSharp.Enums.InstaFeedsType.Media
                        });
                        ShowNotify("Your photo uploaded successfully...", 3500);
                        NotificationHelper.ShowNotify(Caption?.Truncate(50), NotifyFile?.Path, "Media uploaded");
                    }
                }
                catch { }
                SendNotify(102);
                //RemoveThis();

                MainPage.Current?.HideMediaUploadingUc();
                return(Result.Success(true));
            }
            catch (HttpRequestException httpException)
            {
                SendNotify(102);
                //RemoveThis();

                MainPage.Current?.HideMediaUploadingUc();
                return(Result.Fail(httpException, false, ResponseType.NetworkProblem));
            }
            catch (Exception exception)
            {
                SendNotify(102);
                //RemoveThis();
                MainPage.Current?.HideMediaUploadingUc();
                return(Result.Fail <bool>(exception));
            }
        }