Esempio n. 1
0
        private async void retrieveBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string type             = string.Empty;
            string mode             = string.Empty;
            string modeSelectedText = string.Empty;
            int    amount           = 0;

            Invoke((MethodInvoker) delegate
            {
                type             = ((string)websiteTypeComboBox.SelectedItem).ToLower();
                mode             = ((string)modeComboBox.SelectedItem).ToLower();
                modeSelectedText = modeSelectedTextBox.Text;
                amount           = (int)amountNumericUpDown.Value;
            });

            if (!(modeSelectedText.StartsWith("http://") || modeSelectedText.StartsWith("https://")))
            {
                modeSelectedText = "http://" + modeSelectedText;
            }

            WebNovelSource source = GetSource(modeSelectedText, type);

            WebNovelInfo novelInfo = await source.GetNovelInfoAsync(modeSelectedText);

            if (mode == "table of contents")
            {
                ChapterLink[] links = (await source.GetChapterLinksAsync(modeSelectedText)).ToArray();

                Invoke((MethodInvoker) delegate
                {
                    foreach (ChapterLink link in links)
                    {
                        if (link.Unknown)
                        {
                            unknownListBox.Items.Add(link);
                        }
                        else
                        {
                            chaptersListBox.Items.Add(link);
                        }
                    }

                    if (novelInfo != null)
                    {
                        if (!string.IsNullOrEmpty(novelInfo.CoverUrl))
                        {
                            try
                            {
                                string coverUrl   = novelInfo.CoverUrl;
                                coverUrl          = coverUrl.StartsWith("//") ? coverUrl.Substring(2) : coverUrl;
                                coverTextBox.Text = new UriBuilder(coverUrl).Uri.AbsoluteUri;
                            }
                            catch (UriFormatException) { }
                        }

                        if (!string.IsNullOrEmpty(novelInfo.Title))
                        {
                            titleTextBox.Text = novelInfo.Title;
                        }
                    }

                    progressBar.Visible    = false;
                    retrieveButton.Enabled = true;
                });
            }
            else if (mode == "next chapter link")
            {
                ChapterLink firstChapter = new ChapterLink {
                    Url = modeSelectedText
                };
                ChapterLink current = firstChapter;

                int ctr      = 1;
                var chapters = new List <WebNovelChapter>();
                while (true)
                {
                    WebNovelChapter chapter;
                    try
                    {
                        chapter = await source.GetChapterAsync(current);
                    }
                    catch (HttpRequestException)
                    {
                        break;
                    }

                    if (chapter == null)
                    {
                        break;
                    }

                    if (string.IsNullOrEmpty(chapter.ChapterName))
                    {
                        chapter.ChapterName = current.Url;
                    }

                    chapters.Add(chapter);

                    WriteText($"Found Chapter {chapter.ChapterName}", Color.Green);

                    if (string.IsNullOrEmpty(chapter.NextChapterUrl) || chapter.Url == chapter.NextChapterUrl)
                    {
                        break;
                    }

                    current = new ChapterLink {
                        Url = chapter.NextChapterUrl
                    };

                    if (ctr == amount)
                    {
                        break;
                    }

                    ctr++;
                }

                Invoke((MethodInvoker) delegate
                {
                    chaptersListBox.Items.AddRange(chapters.Cast <object>().ToArray());

                    progressBar.Visible    = false;
                    retrieveButton.Enabled = true;
                });
            }
        }