Beispiel #1
0
        /// <summary>
        /// Get the last page number of the tally. This may be determined solely
        /// from the thread range info, or might require information from the
        /// provided first page, where we can extract how many pages are in the thread.
        /// </summary>
        /// <param name="quest">The quest being tallied.</param>
        /// <param name="adapter">The forum adapter that handles the quest's thread.</param>
        /// <param name="threadRangeInfo">The range of posts that are wanted in the tally.</param>
        /// <param name="firstPage">The first page of the tally, from which we can get the page range of the thread.</param>
        /// <returns>Returns the last page number of the tally.</returns>
        private async Task <int> GetLastPageNumber(IQuest quest, IForumAdapter2 adapter,
                                                   ThreadRangeInfo threadRangeInfo, Task <HtmlDocument?> firstPage)
        {
            // Check for quick results first.
            if (threadRangeInfo.Pages > 0)
            {
                // If the page range has already been determined, use that.
                return(threadRangeInfo.Pages);
            }

            if (!quest.ReadToEndOfThread && !threadRangeInfo.IsThreadmarkSearchResult)
            {
                // 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.
                return(ThreadInfo.GetPageNumberOfPost(quest.EndPost, quest));
            }

            // 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.
            var page = await firstPage.ConfigureAwait(false);

            if (page == null)
            {
                throw new InvalidOperationException($"Unable to load first page of {quest.ThreadName}");
            }

            return(adapter.GetThreadInfo(page).Pages);
        }
Beispiel #2
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);
        }