Example #1
0
        /// <summary>
        /// Attempts to log in to an Instagram account.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnAccountLogin_Click(object sender, EventArgs e)
        {
            btnAccountLogin.Enabled = false;

            // Proxy initialization
            _proxy = new ProxyObject(txtProxy.Text, ':');

            if (!string.IsNullOrEmpty(txtProxy.Text))
            {
                _httpClientHandler.Proxy = _proxy.GetWebProxy();
            }

            _instaApi = InstaApiBuilder.CreateBuilder()
                        .UseHttpClientHandler(_httpClientHandler)
                        .SetUser(new UserSessionData
            {
                UserName = txtAccountUsername.Text,
                Password = txtAccountPassword.Text
            })
                        .Build();

            lblAccountLoginStatus.Text = @"Status: Attempting to log in.";
            var login = await _instaApi.LoginAsync();

            if (login.Succeeded)
            {
                _isLogged                       = true;
                gbDownload.Enabled              = true;
                lblAccountLoginStatus.Text      = @"Status: Logged in.";
                lblAccountLoginStatus.ForeColor = Color.Green;
                Log($@"Successfully logged in as {txtAccountUsername.Text}.", nameof(LogType.Success));
            }
            else
            {
                lblAccountLoginStatus.Text      = @"Status: Failed to log in.";
                lblAccountLoginStatus.ForeColor = Color.Red;
                Log($@"Failed to log in as {txtAccountUsername.Text}. Message: {login.Info.Message}", nameof(LogType.Fail));

                btnAccountLogin.Enabled = true;
                return;
            }

            btnAccountLogin.Enabled  = false;
            btnAccountLogout.Enabled = true;
        }
Example #2
0
        /// <summary>
        /// Starts the download process.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnStartDownloading_Click(object sender, EventArgs e)
        {
            // Input validation
            if (!InputValidation.ValidateWebSettings(txtUserAgent.Text, txtRequestTimeout.Text, txtProxy.Text, ':', txtThreads.Text))
            {
                return;
            }
            if (!InputValidation.ValidateDownloadSettings(txtDownloadFolder.Text, cbSaveStats.Checked, txtDelimiter.Text))
            {
                return;
            }

            if (lvInput.Items.Count == 0)
            {
                Log("No input found to download. Please enter at least one and try again.", nameof(LogType.Error));
                return;
            }

            if (!InputValidation.IsInt(txtThreads.Text))
            {
                Log("Invalid threads input. Fix your threads input and try again.", nameof(LogType.Error));
                return;
            }

            // Proxy initialization
            _proxy = new ProxyObject(txtProxy.Text, ':');

            // Filters initialization
            var descriptionStrings = new List <string>();

            descriptionStrings.AddRange(txtSkipDescriptionStrings.Lines);

            var mediaFilter = new MediaFilter
            {
                SkipTopPosts                  = cbSkipTopPosts.Checked,
                SkipMediaIfVideo              = cbSkipVideos.Checked,
                SkipMediaIfPhoto              = cbSkipPhotos.Checked,
                SkipMediaComments             = cbSkipMediaComments.Checked,
                SkipMediaCommentsMore         = cbSkipMediaCommentsMoreLess.Text == @"more",
                SkipMediaCommentsCount        = !String.IsNullOrEmpty(txtSkipMediaCommentsCount.Text) ? int.Parse(txtSkipMediaCommentsCount.Text) : 0,
                SkipMediaLikes                = cbSkipMediaLikes.Checked,
                SkipMediaLikesMore            = cbSkipMediaLikesMoreLess.Text == @"more",
                SkipMediaLikesCount           = !String.IsNullOrEmpty(txtSkipMediaLikesCount.Text) ? int.Parse(txtSkipMediaLikesCount.Text) : 0,
                SkipMediaIfDescriptionContans = cbSkipMediaDescription.Checked,
                DescriptionStrings            = descriptionStrings,
                SkipMediaUploadDateEnabled    = cbSkipMediaUploadDate.Checked,
                SkipMediaUploadDateNewer      = cbSkipMediaUploadDateMoreLess.Text == @"newer",
                SkipMediaUploadDate           = dtUploadTime.Value,
                //SkipMediaUploadDate = ((DateTimeOffset)dtUpladTime.Value).ToUnixTimeSeconds(),
                SkipMediaVideoViews      = cbSkipVideoViews.Checked,
                SkipMediaVideoViewsMore  = cbSkipVideoViewsMoreLess.Text == @"more",
                SkipMediaVideoViewsCount = !String.IsNullOrEmpty(txtSkipVideoViewsCount.Text) ? int.Parse(txtSkipVideoViewsCount.Text) : 0,
                CustomFolder             = cbCreateNewFolder.Checked,
                SaveStatsInCsvFile       = cbSaveStats.Checked
            };

            if (!InputValidation.ValidateFilters(mediaFilter))
            {
                Log("Error detected in the filters. Please check your filter settings and try again.", nameof(LogType.Error));
                return;
            }

            // Download process
            if (!InputValidation.IsDouble(txtRequestTimeout.Text))
            {
                return;
            }

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken       = _cancellationTokenSource.Token;
            var requestTimeout = double.Parse(txtRequestTimeout.Text);

            // Initialize downloader object
            var downloader = new InstagramDownload(_instaApi, mediaFilter, txtUserAgent.Text, _proxy.GetWebProxy(), requestTimeout,
                                                   txtDownloadFolder.Text, _cancellationToken, txtDelimiter.Text)
            {
                IsTotalDownloadsEnabled = cbTotalDownloads.Checked
            };

            // Set downloader properties
            if (!string.IsNullOrEmpty(txtTotalDownloads.Text))
            {
                downloader.TotalDownloads = int.Parse(txtTotalDownloads.Text);
            }
            downloader.CustomFolder = cbCreateNewFolder.Checked;

            // Update form controls
            btnStartDownloading.Enabled = false;
            btnStopDownloading.Enabled  = true;

            // Upload logs
            Log(@"Started downloading...", nameof(LogType.Success));

            foreach (ListViewItem item in lvInput.Items)
            {
                try
                {
                    switch (item.SubItems[0].Text)
                    {
                    case "Url":
                        try
                        {
                            await downloader.Download(item.SubItems[1].Text, InputType.Url, item.SubItems[2].Text);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Log(ex.Message, nameof(LogType.Error));
                        }

                        break;

                    case "MediaId":
                        try
                        {
                            await downloader.Download(item.SubItems[1].Text, InputType.MediaId, item.SubItems[2].Text);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Log(ex.Message, nameof(LogType.Error));
                        }

                        break;

                    case "Username":
                        try
                        {
                            await downloader.Download(item.SubItems[1].Text, InputType.Username, item.SubItems[2].Text);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Log(ex.Message, nameof(LogType.Error));
                        }

                        break;

                    case "UserId":
                        try
                        {
                            await downloader.Download(item.SubItems[1].Text, InputType.UserId, item.SubItems[2].Text);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Log(ex.Message, nameof(LogType.Error));
                        }

                        break;

                    case "Hashtag":
                        try
                        {
                            await downloader.Download(item.SubItems[1].Text, InputType.Hashtag, item.SubItems[2].Text);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Log(ex.Message, nameof(LogType.Error));
                        }

                        break;

                    case "Location":
                        try
                        {
                            await downloader.Download(item.SubItems[1].Text, InputType.Location, item.SubItems[2].Text);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Log(ex.Message, nameof(LogType.Error));
                        }

                        break;
                    }
                }
                catch (OperationCanceledException ex)
                {
                    Log(ex.Message, nameof(LogType.Error));
                }
            }

            btnStartDownloading.Enabled = true;
            btnStopDownloading.Enabled  = false;

            Log(@"Finished downloading...", nameof(LogType.Success));

            // Start all tasks
            //using (var semaphore = new SemaphoreSlim(int.Parse(txtThreads.Text)))
            //{
            //    var tasks = new List<Task>();
            //    foreach (ListViewItem item in lvInput.Items)
            //    {
            //        await semaphore.WaitAsync();

            //        try
            //        {
            //            tasks.Add(
            //                Task.Run(async () =>
            //                {
            //                    switch (item.SubItems[0].Text)
            //                    {
            //                        case "Url":
            //                            try
            //                            {
            //                                await downloader.Download(item.SubItems[1].Text, InputType.Url,
            //                                    mediaFilter);
            //                            }
            //                            catch (OperationCanceledException ex)
            //                            {
            //                                Log(ex.Message, nameof(LogType.Error));
            //                            }

            //                            break;
            //                        case "Username":
            //                            try
            //                            {
            //                                await downloader.Download(item.SubItems[1].Text, InputType.Username,
            //                                    mediaFilter, item.SubItems[2].Text);
            //                            }
            //                            catch (OperationCanceledException ex)
            //                            {
            //                                Log(ex.Message, nameof(LogType.Error));
            //                            }

            //                            break;
            //                        case "Hashtag":
            //                            try
            //                            {
            //                                await downloader.Download(item.SubItems[1].Text, InputType.Hashtag,
            //                                    mediaFilter, item.SubItems[2].Text);
            //                            }
            //                            catch (OperationCanceledException ex)
            //                            {
            //                                Log(ex.Message, nameof(LogType.Error));
            //                            }

            //                            break;
            //                        case "Location":
            //                            try
            //                            {
            //                                await downloader.Download(item.SubItems[1].Text, InputType.Location,
            //                                    mediaFilter, item.SubItems[2].Text);
            //                            }
            //                            catch (OperationCanceledException ex)
            //                            {
            //                                Log(ex.Message, nameof(LogType.Error));
            //                            }

            //                            break;
            //                    }
            //                }, _cancellationToken)
            //            );
            //        }
            //        catch (Exception ex)
            //        {
            //            Log(ex.Message, nameof(LogType.Error));
            //        }
            //        finally
            //        {
            //            semaphore.Release();
            //        }
            //    }

            //    // Wait for tasks to finish
            //    try
            //    {
            //        await Task.WhenAll(tasks); // might throw an exception if something goes wrong during tasks
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine(ex.StackTrace);
            //    }

            //    Log(@"Finished downloading.", nameof(LogType.Success));

            //    // Update form controls when tasks are finished
            //    btnStartDownloading.Enabled = true;
            //    btnStopDownloading.Enabled = false;
            //}
        }