public async Task <IResult <InstaFeed> > GetTagFeedAsync(string tag, int maxPages = 0) { ValidateUser(); ValidateLoggedIn(); var userFeedUri = UriCreator.GetTagFeedUri(tag); var request = HttpHelper.GetDefaultRequest(HttpMethod.Get, userFeedUri, _deviceInfo); var response = await _httpClient.SendAsync(request); var json = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK) { var feedResponse = JsonConvert.DeserializeObject <InstaMediaListResponse>(json, new InstaMediaListDataConverter()); var converter = ConvertersFabric.GetMediaListConverter(feedResponse); var tagFeed = new InstaFeed(); tagFeed.Medias.AddRange(converter.Convert()); var nextId = feedResponse.NextMaxId; while (feedResponse.MoreAvailable && tagFeed.Pages < maxPages) { var nextMedia = await GetTagFeedWithMaxIdAsync(tag, nextId); if (!nextMedia.Succeeded) { Result.Success($"Not all pages was downloaded: {nextMedia.Info.Message}", tagFeed); } nextId = nextMedia.Value.NextMaxId; converter = ConvertersFabric.GetMediaListConverter(nextMedia.Value); tagFeed.Medias.AddRange(converter.Convert()); tagFeed.Pages++; } return(Result.Success(tagFeed)); } return(Result.Fail(GetBadStatusFromJsonString(json).Message, (InstaFeed)null)); }
public async Task <IResult <InstaMediaList> > GetUserMediaAsync(string username, int maxPages = 0) { ValidateUser(); if (maxPages == 0) { maxPages = int.MaxValue; } var user = GetUser(username).Value; var instaUri = UriCreator.GetUserMediaListUri(user.Pk); var request = HttpHelper.GetDefaultRequest(HttpMethod.Get, instaUri, _deviceInfo); var response = await _httpClient.SendAsync(request); var json = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK) { var mediaResponse = JsonConvert.DeserializeObject <InstaMediaListResponse>(json, new InstaMediaListDataConverter()); var converter = ConvertersFabric.GetMediaListConverter(mediaResponse); var mediaList = converter.Convert(); var nextId = mediaResponse.NextMaxId; while (mediaResponse.MoreAvailable && mediaList.Pages < maxPages) { instaUri = UriCreator.GetMediaListWithMaxIdUri(user.Pk, nextId); var nextMedia = await GetUserMediaListWithMaxIdAsync(instaUri); if (!nextMedia.Succeeded) { Result.Success($"Not all pages was downloaded: {nextMedia.Info.Message}", mediaList); } nextId = nextMedia.Value.NextMaxId; mediaList.AddRange(converter.Convert()); mediaList.Pages++; } return(Result.Success(mediaList)); } return(Result.Fail(GetBadStatusFromJsonString(json).Message, (InstaMediaList)null)); }