/// <summary> /// Get followers list by username asynchronously /// </summary> /// <param name="username">Username</param> /// <param name="paginationParameters">Pagination parameters: next id and max amount of pages to load</param> /// <param name="searchQuery">Search string to locate specific followers</param> /// <returns> /// <see cref="InstaUserShortList" /> /// </returns> public async Task <IResult <InstaUserShortList> > GetUserFollowersAsync(string username, PaginationParameters paginationParameters, string searchQuery) { UserAuthValidator.Validate(_userAuthValidate); var followers = new InstaUserShortList(); try { var user = await GetUserAsync(username); var userFollowersUri = UriCreator.GetUserFollowersUri(user.Value.Pk, _user.RankToken, searchQuery, paginationParameters.NextId); var followersResponse = await GetUserListByUriAsync(userFollowersUri); if (!followersResponse.Succeeded) { return(Result.Fail(followersResponse.Info, (InstaUserShortList)null)); } followers.AddRange( followersResponse.Value.Items?.Select(ConvertersFabric.Instance.GetUserShortConverter) .Select(converter => converter.Convert())); followers.NextId = followersResponse.Value.NextMaxId; var pagesLoaded = 1; while (!string.IsNullOrEmpty(followersResponse.Value.NextMaxId) && pagesLoaded < paginationParameters.MaximumPagesToLoad) { var nextFollowersUri = UriCreator.GetUserFollowersUri(user.Value.Pk, _user.RankToken, searchQuery, followersResponse.Value.NextMaxId); followersResponse = await GetUserListByUriAsync(nextFollowersUri); if (!followersResponse.Succeeded) { return(Result.Fail(followersResponse.Info, followers)); } followers.AddRange( followersResponse.Value.Items?.Select(ConvertersFabric.Instance.GetUserShortConverter) .Select(converter => converter.Convert())); pagesLoaded++; followers.NextId = followersResponse.Value.NextMaxId; } return(Result.Success(followers)); } catch (Exception exception) { _logger?.LogException(exception); return(Result.Fail(exception, followers)); } }
public async Task <IResult <InstaUserShortList> > GetUserFollowingAsync(string username, PaginationParameters paginationParameters) { var following = new InstaUserShortList(); try { var user = await GetUserAsync(username); var uri = UriCreator.GetUserFollowingUri(user.Value.Pk, _user.RankToken, paginationParameters.NextId); var userListResponse = await GetUserListByUriAsync(uri); if (!userListResponse.Succeeded) { Result.Fail(userListResponse.Info, following); } following.AddRange( userListResponse.Value.Items.Select(ConvertersFabric.Instance.GetUserShortConverter) .Select(converter => converter.Convert())); following.NextId = userListResponse.Value.NextMaxId; var pages = 1; while (!string.IsNullOrEmpty(following.NextId) && pages < paginationParameters.MaximumPagesToLoad) { var nextUri = UriCreator.GetUserFollowingUri(user.Value.Pk, _user.RankToken, userListResponse.Value.NextMaxId); userListResponse = await GetUserListByUriAsync(nextUri); if (!userListResponse.Succeeded) { return(Result.Success($"Not all pages were downloaded: {userListResponse.Info.Message}", following)); } following.AddRange( userListResponse.Value.Items.Select(ConvertersFabric.Instance.GetUserShortConverter) .Select(converter => converter.Convert())); pages++; following.NextId = userListResponse.Value.NextMaxId; } return(Result.Success(following)); } catch (Exception exception) { _logger?.LogException(exception); return(Result.Fail(exception, following)); } }