Exemplo n.º 1
0
        /// <summary>
        /// Get the starting page for tallying for the provided quest, using either
        /// the explicitly determined page number, or the calculated page number
        /// based on the starting post and the quest's post per page.
        /// </summary>
        /// <param name="quest">The quest this info is for.</param>
        /// <returns>Returns the page number to start tallying.</returns>
        public int GetStartPage(IQuest quest)
        {
            if (ByNumber)
            {
                return(ThreadInfo.GetPageNumberOfPost(Number, quest));
            }

            return(Page);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a list of posts from the provided pages from a quest.
        /// </summary>
        /// <param name="quest">The quest being tallied.</param>
        /// <param name="adapter">The quest's forum adapter.</param>
        /// <param name="rangeInfo">The thread range info for the tally.</param>
        /// <param name="pages">The pages that are being loaded.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns>Returns a list of PostComponents comprising the posts from the threads that fall within the specified range.</returns>
        private async Task <List <PostComponents> > GetPostsFromPagesAsync(
            IQuest quest, IForumAdapter adapter, ThreadRangeInfo rangeInfo, List <Task <HtmlDocument> > pages, CancellationToken token)
        {
            List <PostComponents> postsList = new List <PostComponents>();

            var firstPageTask = pages.First();

            while (pages.Any())
            {
                var finishedPage = await Task.WhenAny(pages).ConfigureAwait(false);

                pages.Remove(finishedPage);

                if (finishedPage.IsCanceled)
                {
                    throw new OperationCanceledException();
                }

                // This will throw any pending exceptions that occurred while trying to load the page.
                // This removes the need to check for finishedPage.IsFaulted.
                var page = await finishedPage.ConfigureAwait(false);

                if (page == null)
                {
                    Exception ae = new Exception("Not all pages loaded.  Rerun tally.");
                    ae.Data["Application"] = true;
                    throw ae;
                }

                var posts = from post in adapter.GetPosts(page, quest)
                            where post != null && post.IsVote && post.IsAfterStart(rangeInfo) &&
                            (quest.ReadToEndOfThread || rangeInfo.IsThreadmarkSearchResult || post.Number <= quest.EndPost)
                            select post;

                postsList.AddRange(posts);
            }

            var firstPage = firstPageTask.Result;

            ThreadInfo threadInfo = adapter.GetThreadInfo(firstPage);

            ViewModelService.MainViewModel.VoteCounter.Title = threadInfo.Title;

            // Get all posts that are not filtered out, either explicitly, or (for the thread author) implicity.
            postsList = postsList
                        .Where(p => (
                                   (quest.UseCustomUsernameFilters && !quest.UsernameFilter.Match(p.Author)) || (!quest.UseCustomUsernameFilters && p.Author != threadInfo.Author)) &&
                               (!quest.UseCustomPostFilters || !(quest.PostsToFilter.Contains(p.Number) || quest.PostsToFilter.Contains(p.IDValue))
                               )
                               )
                        .Distinct().OrderBy(p => p.Number).ToList();

            return(postsList);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Determines the page number range that will be loaded for the quest.
        /// Returns a tuple of first page number, last page number, and pages to scan.
        /// </summary>
        /// <param name="quest">The quest being tallied.</param>
        /// <param name="adapter">The forum adapter for the quest.</param>
        /// <param name="threadRangeInfo">The thread range info, as provided by the adapter.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns>Returns a tuple of the page number info that was determined.</returns>
        private async Task <(int firstPageNumber, int lastPageNumber, int pagesToScan)> GetPagesToScanAsync(
            IQuest quest, IForumAdapter adapter, ThreadRangeInfo threadRangeInfo, CancellationToken token)
        {
            IPageProvider pageProvider = ViewModelService.MainViewModel.PageProvider;

            int firstPageNumber = threadRangeInfo.GetStartPage(quest);
            int lastPageNumber  = 0;
            int pagesToScan     = 0;

            if (threadRangeInfo.Pages > 0)
            {
                // If the startInfo obtained the thread pages info, just use that.
                lastPageNumber = threadRangeInfo.Pages;
            }
            else if (quest.ReadToEndOfThread || threadRangeInfo.IsThreadmarkSearchResult)
            {
                // If we're reading to the end of the thread (end post 0, or based on a threadmark),
                // then we need to load the first page to find out how many pages there are in the thread.
                // Make sure to bypass the cache, since it may have changed since the last load.

                string firstPageUrl = adapter.GetUrlForPage(firstPageNumber, quest.PostsPerPage);

                HtmlDocument page = await pageProvider.GetPage(firstPageUrl, $"Page {firstPageNumber}",
                                                               CachingMode.BypassCache, ShouldCache.Yes, SuppressNotifications.No, token)
                                    .ConfigureAwait(false);

                if (page == null)
                {
                    throw new InvalidOperationException($"Unable to load web page: {firstPageUrl}");
                }

                lastPageNumber = adapter.GetThreadInfo(page).Pages;
            }
            else
            {
                // If we're not reading to the end of the thread, just calculate
                // what the last page number will be.  Pages to scan will be the
                // difference in pages +1.
                lastPageNumber = ThreadInfo.GetPageNumberOfPost(quest.EndPost, quest);
            }

            pagesToScan = lastPageNumber - firstPageNumber + 1;

            return(firstPageNumber, lastPageNumber, pagesToScan);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets all posts from the provided pages list.
        /// </summary>
        /// <param name="loadingPages">The pages that are being loaded for the tally.</param>
        /// <param name="quest">The quest being tallied.</param>
        /// <param name="adapter">The forum adapter that handles the quest's thread.</param>
        /// <returns>Returns all posts extracted from all pages provided,
        /// and the thread title.</returns>
        private async Task <(ThreadInfo threadInfo, List <Post> posts)> GetPostsFromPagesAsync(
            List <Task <HtmlDocument?> > loadingPages,
            IQuest quest, IForumAdapter2 adapter,
            ThreadRangeInfo threadRangeInfo)
        {
            ThreadInfo? threadInfo = null;
            List <Post> postsList  = new List <Post>();
            int         pageNumber = threadRangeInfo.GetStartPage(quest) - 1;
            bool        incomplete = false;

            foreach (var loadingPage in loadingPages)
            {
                var page = await loadingPage.ConfigureAwait(false);

                pageNumber++;

                if (page == null)
                {
                    incomplete = true;
                    continue;
                }

                if (threadInfo == null)
                {
                    threadInfo = adapter.GetThreadInfo(page);
                }

                postsList.AddRange(adapter.GetPosts(page, quest, pageNumber));
            }

            if (incomplete)
            {
                InvalidOperationException e = new InvalidOperationException("Unable to load all pages.");
                e.Data["Application"] = true;
                throw e;
            }

            if (threadInfo == null)
            {
                threadInfo = new ThreadInfo("Unknown", "Unknown", 0);
            }

            return(threadInfo, postsList);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Run the provided post list through the various filters, as set
        /// by quest options and post numbers, etc.
        /// </summary>
        /// <param name="postsList">The list of posts to filter.</param>
        /// <param name="quest">The quest with relevant options.</param>
        /// <param name="threadInfo">Thread info provides the thread author.</param>
        /// <param name="rangeInfo">Range info provides information on the range of valid posts.</param>
        /// <returns>Returns a list of posts that satisfy the filtering criteria.</returns>
        private List <Post> FilterPosts(List <Post> postsList,
                                        IQuest quest, ThreadInfo threadInfo, ThreadRangeInfo rangeInfo)
        {
            // Remove any posts that are not votes, that aren't in the valid post range, or that
            // hit any filters the quest has set up.  Then do a grouping to get distinct results.
            var filtered = from post in postsList
                           where post.HasVote &&
                           (PostIsAfterStart(post, rangeInfo) && PostIsBeforeEnd(post, quest, rangeInfo)) &&
                           ((quest.UseCustomUsernameFilters && !quest.UsernameFilter.Match(post.Origin.Author)) ||
                            (!quest.UseCustomUsernameFilters && !string.Equals(post.Origin.Author, threadInfo.Author, StringComparison.Ordinal))) &&
                           (!quest.UseCustomPostFilters ||
                            !(quest.PostsToFilter.Contains(post.Origin.ThreadPostNumber) ||
                              quest.PostsToFilter.Contains(post.Origin.ID.Value)))
                           // Group to deal with sticky posts that should only be processed once.
                           group post by post.Origin.ThreadPostNumber into postNumGroup
                           orderby postNumGroup.Key
                           select postNumGroup.First();

            return(filtered.ToList());
        }