Esempio n. 1
0
        private static void DownloadChapters(List <ushort> chapterIndices, bool forceRedownload = false)
        {
            string[] workingDots = new string[3] {
                ".   ", "..  ", "... "
            };
            if (!forceRedownload)
            {
                for (int j = 0; j < chapterIndices.Count; j++)
                {
                    if (loadedStory.Chapters[chapterIndices[j]].Text != "")
                    {
                        chapterIndices.RemoveAt(j);
                        j--;
                    }
                }
            }
            int i = 0;

            Console.WriteLine("Press any key to cancel.");
            int           chapterIdxIdx = 0;
            int           successCount  = 0;
            int           failureCount  = 0;
            List <string> failMessages  = new List <string>();

            while (chapterIdxIdx < chapterIndices.Count && !Console.KeyAvailable)
            {
                Task dlTask = loadedStory.DownloadChapterData(chapterIndices[chapterIdxIdx]);
                while (dlTask.Status != TaskStatus.RanToCompletion && dlTask.Status != TaskStatus.Faulted && !Console.KeyAvailable)
                {
                    Console.Write("\r" + "Downloading chapters (attempt " + Chapter.DownloadAttempts + ") ");
                    Console.Write("(" + successCount + "/" + chapterIndices.Count + " completed) ");
                    Console.Write("(" + failureCount + " failures) " + workingDots[i]);
                    if (i < 2)
                    {
                        i++;
                    }
                    else
                    {
                        i = 0;
                    }
                    Thread.Sleep(500);
                }
                if (dlTask.Status == TaskStatus.RanToCompletion)
                {
                    successCount++;
                }
                else if (dlTask.Status == TaskStatus.Faulted)
                {
                    failureCount++;
                    string message = dlTask.Exception.Message;
                    if (!failMessages.Contains(message))
                    {
                        failMessages.Add(message);
                    }
                }
                else if (Console.KeyAvailable)
                {
                    Chapter.CancelDownloads = true;
                }

                chapterIdxIdx++;
            }
            Console.Write("\r" + "Downloading chapters (attempt " + Chapter.DownloadAttempts + ") ");
            Console.Write("(" + successCount + "/" + chapterIndices.Count + " completed) ");
            Console.WriteLine("(" + failureCount + " failures) " + workingDots[2]);
            if (Console.KeyAvailable)
            {
                Console.WriteLine("Canceled");
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(false);
                }
            }
            if (failureCount > 0)
            {
                Console.WriteLine("Failure messages: ");
                foreach (string msg in failMessages)
                {
                    Console.WriteLine(msg);
                }
            }
            WaitForEnter();
        }