示例#1
0
 public void Setup()
 {
     _logger                    = new ConsoleLogger();
     _httpClient                = new HttpClient(_logger);
     _amazonInfoParser          = new AmazonInfoParser(_logger, _httpClient);
     _amazonClient              = new AmazonClient(_httpClient, _amazonInfoParser, _logger);
     _authorProfileGenerator    = new AuthorProfileGenerator(_httpClient, _logger, _amazonClient);
     _secondarySourceGoodreads  = new SecondarySourceGoodreads(_logger, _httpClient, _amazonClient);
     _endActionsArtifactService = new EndActionsArtifactService(_logger);
 }
示例#2
0
        private async Task btnKindleExtras_Run()
        {
            //Check current settings
            if (!File.Exists(txtMobi.Text))
            {
                MessageBox.Show("Specified book was not found.", "Book Not Found");
                return;
            }
            if (rdoGoodreads.Checked)
            {
                if (txtGoodreads.Text == "")
                {
                    MessageBox.Show($"No {_dataSource.Name} link was specified.", $"Missing {_dataSource.Name} Link");
                    return;
                }
                if (!txtGoodreads.Text.ToLower().Contains(_settings.dataSource.ToLower()))
                {
                    MessageBox.Show($"Invalid {_dataSource.Name} link was specified.\r\n"
                                    + $"If you do not want to use {_dataSource.Name}, you can change the data source in Settings."
                                    , $"Invalid {_dataSource.Name} Link");
                    return;
                }
            }
            if (_settings.realName.Trim().Length == 0 || _settings.penName.Trim().Length == 0)
            {
                MessageBox.Show(
                    "Both Real and Pen names are required for End Action\r\n" +
                    "file creation. This information allows you to rate this\r\n" +
                    "book on Amazon. Please review the settings page.",
                    "Amazon Customer Details Not found");
                return;
            }

            var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, _settings.saverawml, _logger));

            if (metadata == null)
            {
                return;
            }

            SetDatasourceLabels(); // Reset the dataSource for the new build process
            _logger.Log($"Book's {_dataSource.Name} URL: {txtGoodreads.Text}");
            try
            {
                var bookInfo = new BookInfo(metadata, txtGoodreads.Text, txtMobi.Text);

                var outputDir = OutputDirectory(bookInfo.Author, bookInfo.Title, bookInfo.Asin, Path.GetFileNameWithoutExtension(txtMobi.Text), true);

                _logger.Log("Attempting to build Author Profile...");

                ApPath = $@"{outputDir}\AuthorProfile.profile.{bookInfo.Asin}.asc";

                // TODO: Load existing ap to use for end actions / start actions
                if (!Settings.Default.overwrite && File.Exists(ApPath))
                {
                    _logger.Log("AuthorProfile file already exists... Skipping!\r\n" +
                                "Please review the settings page if you want to overwite any existing files.");
                    return;
                }

                var response = await _authorProfileGenerator.GenerateAsync(new AuthorProfileGenerator.Request
                {
                    Book     = bookInfo,
                    Settings = new AuthorProfileGenerator.Settings
                    {
                        AmazonTld     = _settings.amazonTLD,
                        SaveBio       = _settings.saveBio,
                        UseNewVersion = _settings.useNewVersion,
                        EditBiography = _settings.editBiography
                    }
                }, _cancelTokens.Token);

                if (response == null)
                {
                    return;
                }

                var authorProfileOutput = JsonConvert.SerializeObject(AuthorProfileGenerator.CreateAp(response, bookInfo.Asin));

                try
                {
                    File.WriteAllText(ApPath, authorProfileOutput);
                    _logger.Log($"Author Profile file created successfully!\r\nSaved to {ApPath}");
                }
                catch (Exception ex)
                {
                    _logger.Log($"An error occurred while writing the Author Profile file: {ex.Message}\r\n{ex.StackTrace}");
                    return;
                }

                SaPath = $@"{outputDir}\StartActions.data.{bookInfo.Asin}.asc";
                _logger.Log("Attempting to build Start Actions and End Actions...");

                string AsinPrompt(string title, string author)
                {
                    var frmAsin = _diContainer.GetInstance <frmASIN>();

                    frmAsin.Text           = "Series Information";
                    frmAsin.lblTitle.Text  = title;
                    frmAsin.lblAuthor.Text = author;
                    frmAsin.tbAsin.Text    = "";
                    frmAsin.ShowDialog();
                    return(frmAsin.tbAsin.Text);
                }

                var ea = new EndActions(response, bookInfo, metadata.RawMlSize, _dataSource, new EndActions.Settings
                {
                    AmazonTld         = _settings.amazonTLD,
                    Android           = _settings.android,
                    OutDir            = _settings.outDir,
                    OutputToSidecar   = _settings.outputToSidecar,
                    PenName           = _settings.penName,
                    RealName          = _settings.realName,
                    UseNewVersion     = _settings.useNewVersion,
                    UseSubDirectories = _settings.useSubDirectories,
                    PromptAsin        = _settings.promptASIN
                }, AsinPrompt, _logger, _httpClient, _amazonClient, _amazonInfoParser);
                if (!await ea.Generate())
                {
                    return;
                }

                if (_settings.useNewVersion)
                {
                    await ea.GenerateNewFormatData(_progress, _cancelTokens.Token);

                    // TODO: Do the templates differently
                    Extras.Artifacts.EndActions eaBase;
                    try
                    {
                        var template = File.ReadAllText($@"{Environment.CurrentDirectory}\dist\BaseEndActions.json", Encoding.UTF8);
                        eaBase = JsonConvert.DeserializeObject <Extras.Artifacts.EndActions>(template);
                    }
                    catch (FileNotFoundException)
                    {
                        _logger.Log(@"Unable to find dist\BaseEndActions.json, make sure it has been extracted!");
                        return;
                    }
                    catch (Exception e)
                    {
                        _logger.Log($@"An error occurred while loading dist\BaseEndActions.json (make sure any new versions have been extracted!)\r\n{e.Message}\r\n{e.StackTrace}");
                        return;
                    }

                    await ea.GenerateEndActionsFromBase(eaBase);

                    StartActions sa;
                    try
                    {
                        var template = File.ReadAllText($@"{Environment.CurrentDirectory}\dist\BaseStartActions.json", Encoding.UTF8);
                        sa = JsonConvert.DeserializeObject <StartActions>(template);
                    }
                    catch (FileNotFoundException)
                    {
                        _logger.Log(@"Unable to find dist\BaseStartActions.json, make sure it has been extracted!");
                        return;
                    }
                    catch (Exception e)
                    {
                        _logger.Log($@"An error occurred while loading dist\BaseStartActions.json (make sure any new versions have been extracted!)\r\n{e.Message}\r\n{e.StackTrace}");
                        return;
                    }

                    // TODO: Separate out SA logic
                    string saContent = null;
                    if (_settings.downloadSA)
                    {
                        _logger.Log("Attempting to download Start Actions...");
                        try
                        {
                            saContent = await _amazonClient.DownloadStartActions(metadata.Asin);

                            _logger.Log("Successfully downloaded pre-made Start Actions!");
                        }
                        catch
                        {
                            _logger.Log("No pre-made Start Actions available, building...");
                        }
                    }
                    if (string.IsNullOrEmpty(saContent))
                    {
                        saContent = ea.GenerateStartActionsFromBase(sa);
                    }

                    _logger.Log("Writing StartActions to file...");
                    File.WriteAllText(ea.SaPath, saContent);
                    _logger.Log($"StartActions file created successfully!\r\nSaved to {SaPath}");

                    cmsPreview.Items[3].Enabled = true;
                    EaPath = $@"{outputDir}\EndActions.data.{bookInfo.Asin}.asc";
                }
                else
                {
                    ea.GenerateOld();
                }

                cmsPreview.Items[1].Enabled = true;

                checkFiles(bookInfo.Author, bookInfo.Title, bookInfo.Asin, Path.GetFileNameWithoutExtension(txtMobi.Text));
                if (_settings.playSound)
                {
                    var player = new System.Media.SoundPlayer($@"{Environment.CurrentDirectory}\done.wav");
                    player.Play();
                }
            }
            catch (Exception ex)
            {
                _logger.Log("An error occurred while creating the new Author Profile, Start Actions, and/or End Actions files:\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
            finally
            {
                metadata.Dispose();
            }
        }