public async Task Start() { await Task.Run(async() => { var url = $"https://www.reddit.com/r/{_subredditName}/new.json"; var existingPosts = await _reddit.GetPosts(url); _knownPostIds = existingPosts.Select(p => p.Id); Observable .Interval(_interval) .Subscribe( async x => { try { var newPosts = await _reddit.GetPosts(url); foreach (var post in newPosts.Where(p => !_knownPostIds.Contains(p.Id))) { _posts.OnNext(post); } _knownPostIds = newPosts.Select(p => p.Id); } catch (Exception ex) { _onError(ex); } }); }); }
private async Task GetAllPosts(string baseUrl) { var url = baseUrl; var count = 0; IEnumerable <RedditPostData> posts = null; do { try { posts = await _reddit.GetPosts(url); if (posts.Any()) { foreach (var post in posts.Where(p => p.CreatedUtc.FromUnixTime() >= _minimumDate)) { _posts.OnNext(post); } count += 25; var lastPost = posts?.Last(); url = $"{baseUrl}&count={count}&after={lastPost?.Name}"; _stop = posts == null || posts.Any(p => p.CreatedUtc.FromUnixTime() < _minimumDate); } else { _stop = true; } } catch (Exception ex) { _logError(ex, "Error retrieving posts"); } }while (!_stop && posts != null && posts.Any()); }