コード例 #1
0
ファイル: MainForm.cs プロジェクト: fettony/WebNovelConverter
        private async void convertBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            EBook book = new EBook
            {
                Title = titleTextBox.Text,
                CoverImage = coverTextBox.Text
            };
            foreach (ChapterLink link in chaptersListBox.Items)
            {
                WebNovelSource source = GetSource(link.Url);
                WebNovelChapter chapter = await source.GetChapterAsync(link);

                if (chapter == null)
                {
                    WriteText($"Failed to process {link.Name}!", Color.Red);
                }
                else
                {
                    book.Chapters.Add(new Chapter
                    {
                        Name = (chapter.ChapterName.Length > 0) ? chapter.ChapterName : link.Name,
                        Content = chapter.Content
                    });

                    WriteText($"{link.Name} has been processed.", Color.Green);
                }

                await Task.Delay(TimeSpan.FromSeconds(Settings.Default.DelayPerChapter));
            }

            WriteText("Generating epub...");

            await book.GenerateEpubAsync(e.Argument.ToString());

            WriteText("Done!");

            Invoke((MethodInvoker)delegate
            {
                progressBar.Visible = false;
                convertButton.Enabled = true;
            });
        }
コード例 #2
0
        private async void convertBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var items = new List<object>();
            string type = string.Empty;
            string mode = string.Empty;
            Invoke((MethodInvoker)delegate
            {
                type = ((string)websiteTypeComboBox.SelectedItem).ToLower();
                mode = ((string)modeComboBox.SelectedItem).ToLower();
                items.AddRange(chaptersListBox.Items.Cast<object>());
            });

            EBook book = new EBook
            {
                Title = titleTextBox.Text,
                CoverImage = coverTextBox.Text
            };

            foreach (object obj in items)
            {
                if (obj is ChapterLink)
                {
                    ChapterLink link = (ChapterLink)obj;

                    try
                    {
                        WebNovelSource source = GetSource(link.Url, type);
                        WebNovelChapter chapter = await source.GetChapterAsync(link);

                        if (chapter == null)
                        {
                            WriteText($"Failed to process {link.Name}!", Color.Red);
                        }
                        else
                        {
                            book.Chapters.Add(new Chapter { Name = link.Name, Content = chapter.Content });

                            WriteText($"{link.Name} has been processed.", Color.Green);
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteText($"Failed to process {link.Name}!", Color.Red);
                        WriteText($"ERROR: {ex}", Color.Red);
                    }

                    if (Settings.Default.DelayPerChapter > 0)
                        await Task.Delay(TimeSpan.FromSeconds(Settings.Default.DelayPerChapter));
                }
                else if (obj is WebNovelChapter)
                {
                    WebNovelChapter wn = (WebNovelChapter)obj;

                    book.Chapters.Add(new Chapter { Name = wn.ChapterName, Content = wn.Content });
                }
            }

            WriteText("Generating epub...");

            try
            {
                await book.GenerateEpubAsync(e.Argument.ToString());
            }
            catch (Exception ex)
            {
                WriteText("Error generating Epub", Color.Red);
                WriteText($"ERROR: {ex}", Color.Red);
            }

            WriteText("Done!", Color.Green);

            Invoke((MethodInvoker)delegate
            {
                progressBar.Visible = false;
                convertButton.Enabled = true;
            });
        }