/// <summary> /// Asynchronously Starts the requests to download the users from the search pages. /// </summary> /// <param name="username">The username.</param> /// <param name="threads">The max thread count.</param> private async Task startRequestsAsync(string username, int threads) { status = Status.Preparing; PageRequest pageRequest = new PageRequest(); ResultPage initialRequest = null; while (initialRequest?.Success != true) { initialRequest = await pageRequest.GetPageUsersAsync(username, 1).ConfigureAwait(false); } // Limit of 500 pages or 10,000 users. If less than that then we divide user count by users per page (20) int pages = initialRequest.SearchResultCount > 10000 ? 500 : (int)Math.Ceiling((double)(initialRequest.SearchResultCount / 20.0)); int pagesPerThread = pages <= threads ? 1 : pages / threads; totalUsersSearching = initialRequest.SearchResultCount > 10000 ? 10000 : initialRequest.SearchResultCount; List <List <int> > pagesLists = new List <List <int> >(); List <int> tempPages = new List <int>(); for (int i = 1; i <= pages; i++) { if (tempPages.Count < pagesPerThread) { tempPages.Add(i); } if (tempPages.Count >= pagesPerThread) { pagesLists.Add(new List <int>(tempPages)); tempPages.Clear(); } } pagesLists[0].AddRange(new List <int>(tempPages)); // Add rest of pages to first list status = Status.DownloadingUsers; foreach (var pageList in pagesLists) { var task = new Task(async() => await getUsersAsync(username, pageList).ConfigureAwait(false)); taskList.Add(task); task.Start(); } Task.WaitAll(taskList.ToArray()); }
/// <summary> /// Asynchronously gets the users in the specified pages. /// </summary> /// <param name="username">The username.</param> /// <param name="pages">The pages to check.</param> private async Task getUsersAsync(string username, List <int> pages) { PageRequest pageRequest = new PageRequest(); for (int i = 0; i < pages.Count;) { ResultPage resultPage = await pageRequest.GetPageUsersAsync(username, pages[i]).ConfigureAwait(false); if (resultPage.Success) { bool goodResult = true; foreach (User user in resultPage.Users) { if (!user.ValidProfilePicUrl) { user.ProfilePic = Properties.Resources.default_profile_full; } if (user.ProfilePic == null || user.Id == "NOTFOUND") { goodResult = false; } } // If the result was satisfactory then we // add the users in the page to the rest of // of the users found, and increment i to continue if (goodResult) { semaphore.WaitOne(); addUsers(username, resultPage.Users); semaphore.Release(); i++; } } } }