Ejemplo n.º 1
0
        public async Task XRayXMLSaveNewTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.xml, book.db, book.guid, book.asin, 0, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, xray.AliasPath);
            _aliasesRepository.LoadAliasesForXRay(xray);
            xray.ExpandFromRawMl(new FileStream(book.rawml, FileMode.Open), null, null, CancellationToken.None, false, false);
            string filename = xray.XRayName();
            string outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
        }
Ejemplo n.º 2
0
        public async Task XRayXmlSaveOldTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.xml, book.db, book.guid, book.asin, 0, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, xray.AliasPath);
            _aliasesRepository.LoadAliasesForXRay(xray);
            xray.ExpandFromRawMl(new FileStream(book.rawml, FileMode.Open), null, null, CancellationToken.None, false, false);
            string filename = xray.XRayName();
            string outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            xray.CreatedAt = new DateTime(2019, 11, 2, 13, 19, 18, DateTimeKind.Utc);
            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
            FileAssert.AreEqual($"testfiles\\XRAY.entities.{book.asin}_old.asc", outpath);
        }
Ejemplo n.º 3
0
        public async Task XRayXMLSaveNewTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, xray.AliasPath, true, false);
            var fakeMetadata = new Metadata();

            _aliasesRepository.LoadAliasesForXRay(xray);
            using var fs = new FileStream(book.Rawml, FileMode.Open);
            _xrayService.ExpandFromRawMl(xray, fakeMetadata, fs, false, true, true, 0, true, null, null, CancellationToken.None, false, false);
            string filename = xray.XRayName();
            string outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
        }
Ejemplo n.º 4
0
        public async Task XRayXmlSaveOldTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, _file, true, false);
            using var fs = new FileStream(book.Bookpath, FileMode.Open, FileAccess.Read);
            var metadata = new MobiMetadata(fs);

            _aliasesRepository.LoadAliasesForXRay(xray);
            using var bookFs = new FileStream(book.Rawml, FileMode.Open);
            _xrayService.ExpandFromRawMl(xray, metadata, bookFs, false, true, 0, true, null, null, CancellationToken.None, false, false);
            var filename = _directoryService.GetArtifactFilename(ArtifactType.XRay, book.Asin, book.Db, book.Guid);
            var outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            xray.CreatedAt = new DateTime(2019, 11, 2, 13, 19, 18, DateTimeKind.Utc);
            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
            FileAssert.AreEqual($"testfiles\\XRAY.entities.{book.Asin}_old.asc", outpath);
        }
Ejemplo n.º 5
0
        public async Task XRayXMLSaveNewTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, _file, true, false);
            using var fs = new FileStream(book.Rawml, FileMode.Open);
            var fakeMetadata = Substitute.For <IMetadata>();

            fakeMetadata.IsAzw3.Returns(false);
            fakeMetadata.GetRawMlStream().Returns(new MemoryStream(fs.ReadToEnd()));
            fs.Seek(0, SeekOrigin.Begin);
            _aliasesRepository.LoadAliasesForXRay(xray);
            _xrayService.ExpandFromRawMl(xray, fakeMetadata, fs, true, true, 0, true, null, null, CancellationToken.None, false, false);
            var filename = _directoryService.GetArtifactFilename(ArtifactType.XRay, book.Asin, book.Db, book.Guid);
            var outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Builds an X-Ray file from the parameters given and returns the path at which the file has been saved (or null if something failed)
        /// </summary>
        public async Task <string> BuildAsync([NotNull] Request request, CancellationToken cancellationToken)
        {
            using var metadata = await GetAndValidateMetadataAsync(request.BookPath, cancellationToken);

            if (metadata == null)
            {
                return(null);
            }

            var dataSource = string.IsNullOrEmpty(request.DataUrl) || request.DataUrl == SecondarySourceRoentgen.FakeUrl
                ? _secondaryDataSourceFactory.Get(SecondaryDataSourceFactory.Enum.Roentgen)
                : _secondaryDataSourceFactory.GetInferredSource(request.DataUrl);

            if (dataSource == null)
            {
                _logger.Log("Data source could not be determined from the given path.");
                return(null);
            }

            Core.XRay.XRay xray;
            try
            {
                xray = await _xrayService.CreateXRayAsync(request.DataUrl, metadata.DbName, metadata.UniqueId, metadata.Asin, request.AmazonTld ?? "com", request.IncludeTopics, dataSource, _progress, cancellationToken);

                if (xray.Terms.Count == 0)
                {
                    _logger.Log($"No terms were available on {dataSource.Name}, cancelling the build...");
                    return(null);
                }

                var aliasPath = _directoryService.GetAliasPath(xray.Asin);
                _xrayService.ExportAndDisplayTerms(xray, dataSource, false, request.SplitAliases);

                if (xray.Terms.Any(term => term.Aliases?.Count > 0))
                {
                    _logger.Log("Character aliases read from the XML file.");
                }
                else if (!File.Exists(aliasPath))
                {
                    _logger.Log("Aliases file not found.");
                }
                else
                {
                    _aliasesRepository.LoadAliasesForXRay(xray);
                    _logger.Log($"Character aliases read from {aliasPath}.");
                }

                _logger.Log("Initial X-Ray built, adding locations and chapters...");
                //Expand the X-Ray file from the unpacked mobi
                Task buildTask = metadata switch
                {
                    // ReSharper disable AccessToDisposedClosure
                    MobiMetadata _ => Task.Run(() => _xrayService.ExpandFromRawMl(xray, metadata, metadata.GetRawMlStream(), true, true, 25, true, null, _progress, cancellationToken, true, false), cancellationToken),
                    KfxContainer kfx => Task.Run(() => _kfxXrayService.AddLocations(xray, kfx, true, 25, _progress, cancellationToken), cancellationToken),
                    _ => throw new NotSupportedException()
                };
                await buildTask.ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                _logger.Log("Build canceled.");
                return(null);
            }
            catch (Exception ex)
            {
                _logger.Log($"An error occurred while building the X-Ray:\r\n{ex.Message}\r\n{ex.StackTrace}");
                return(null);
            }

            _logger.Log("Saving X-Ray to file...");
            var xrayPath = _directoryService.GetArtifactPath(ArtifactType.XRay, metadata, Path.GetFileNameWithoutExtension(request.BookPath), true);

            try
            {
                var xrayExporter = _xrayExporterFactory.Get(XRayExporterFactory.Enum.Sqlite);
                xrayExporter.Export(xray, xrayPath, _progress, cancellationToken);
            }
            catch (OperationCanceledException)
            {
                _logger.Log("Building canceled.");
                return(null);
            }
            catch (Exception ex)
            {
                // TODO: Add option to retry maybe?
                _logger.Log($"An error occurred while creating the X-Ray file. Is it opened in another program?\r\n{ex.Message}");
                return(null);
            }

            _logger.Log($"X-Ray file created successfully!\r\nSaved to {xrayPath}");

            return(xrayPath);
        }
Ejemplo n.º 7
0
        private async Task btnBuild_Run()
        {
            //Check current settings
            if (!File.Exists(txtMobi.Text))
            {
                MessageBox.Show(@"Specified book was not found.", @"Book Not Found");
                return;
            }
            if (rdoGoodreads.Checked && txtGoodreads.Text == "")
            {
                MessageBox.Show($"No {_dataSource.Name} link was specified.", $"Missing {_dataSource.Name} Link");
                return;
            }
            if (!Directory.Exists(_settings.outDir))
            {
                MessageBox.Show(@"Specified output directory does not exist.\r\nPlease review the settings page.", @"Output Directory Not found");
                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;
            }

            prgBar.Value = 0;

            // todo this is crap
            var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, _settings.saverawml, _logger));

            if (metadata == null)
            {
                return;
            }

            // Added author name to log output
            _logger.Log($"Book's {_dataSource.Name} URL: {txtGoodreads.Text}");
            if (_cancelTokens.IsCancellationRequested)
            {
                return;
            }
            _logger.Log("Attempting to build X-Ray...");

            //If AZW3 file use AZW3 offset, if checked. Checked by default.
            var AZW3 = Path.GetExtension(txtMobi.Text) == ".azw3" && _settings.overrideOffset;

            _logger.Log($"Offset: {(AZW3 ? $"{_settings.offsetAZW3} (AZW3)" : _settings.offset.ToString())}");

            //Create X-Ray and attempt to create the base file (essentially the same as the site)
            XRay.XRay xray;
            SetDatasourceLabels(); // Reset the dataSource for the new build process
            try
            {
                Task <XRay.XRay> xrayTask;
                if (rdoGoodreads.Checked)
                {
                    xrayTask = _xrayService.CreateXRayAsync(txtGoodreads.Text, metadata.DbName, metadata.UniqueId, metadata.Asin,
                                                            AZW3 ? _settings.offsetAZW3 : _settings.offset, _dataSource, _progress, _cancelTokens.Token);
                }
                else
                {
                    // TODO Set datasource properly
                    var fileDataSource = _diContainer.GetInstance <SecondaryDataSourceFactory>().Get(SecondaryDataSourceFactory.Enum.File);
                    xrayTask = _xrayService.CreateXRayAsync(txtXMLFile.Text, metadata.DbName, metadata.UniqueId, metadata.Asin,
                                                            AZW3 ? _settings.offsetAZW3 : _settings.offset, fileDataSource, _progress, _cancelTokens.Token);
                }

                xray = await Task.Run(() => xrayTask).ConfigureAwait(false);

                _xrayService.ExportAndDisplayTerms(xray, xray.AliasPath);

                if (_settings.enableEdit && DialogResult.Yes ==
                    MessageBox.Show(
                        "Terms have been exported to an alias file or already exist in that file. Would you like to open the file in notepad for editing?\r\n"
                        + "See the MobileRead forum thread (link in Settings) for more information on building aliases.",
                        "Aliases",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button2))
                {
                    Functions.RunNotepad(xray.AliasPath);
                }
                if (!File.Exists(xray.AliasPath))
                {
                    _logger.Log("Aliases file not found.");
                }
                else
                {
                    _aliasesRepository.LoadAliasesForXRay(xray);
                    _logger.Log($"Character aliases read from {xray.AliasPath}.");
                }

                _logger.Log("Initial X-Ray built, adding locations and chapters...");
                //Expand the X-Ray file from the unpacked mobi
                if (await Task.Run(() => xray.ExpandFromRawMl(metadata.GetRawMlStream(), SafeShow, _progress, _cancelTokens.Token, _settings.ignoresofthyphen, !_settings.useNewVersion)).ConfigureAwait(false) > 0)
                {
                    _logger.Log("Build canceled or error occurred while processing locations and chapters.");
                    return;
                }
            }
            catch (OperationCanceledException)
            {
                _logger.Log("Build canceled.");
                return;
            }
            catch (Exception ex)
            {
                _logger.Log($"An error occurred while building the X-Ray:\r\n{ex.Message}\r\n{ex.StackTrace}");
                return;
            }

            _logger.Log("Saving X-Ray to file...");
            string outFolder;

            try
            {
                if (_settings.android)
                {
                    outFolder = $@"{_settings.outDir}\Android\{metadata.Asin}";
                    Directory.CreateDirectory(outFolder);
                }
                else
                {
                    outFolder = OutputDirectory(metadata.Author, metadata.Title, metadata.Asin, Path.GetFileNameWithoutExtension(txtMobi.Text), true);
                }
            }
            catch (Exception ex)
            {
                _logger.Log($"Failed to create output directory: {ex.Message}\r\n{ex.StackTrace}\r\nFiles will be placed in the default output directory.");
                outFolder = _settings.outDir;
            }
            var newPath = $"{outFolder}\\{xray.XRayName(_settings.android)}";

            try
            {
                var xrayExporter = _xrayExporterFactory.Get(_settings.useNewVersion ? XRayExporterFactory.Enum.Sqlite : XRayExporterFactory.Enum.Json);
                xrayExporter.Export(xray, newPath, _progress, _cancelTokens.Token);
            }
            catch (OperationCanceledException)
            {
                _logger.Log("Building canceled.");
                return;
            }
            catch (Exception ex)
            {
                // TODO: Add option to retry maybe?
                _logger.Log($"An error occurred while creating the X-Ray file. Is it opened in another program?\r\n{ex.Message}");
                return;
            }

            if (_settings.useNewVersion)
            {
                XrPath = $@"{outFolder}\XRAY.entities.{metadata.Asin}";

                //Save the new XRAY.ASIN.previewData file
                try
                {
                    var pdPath = $@"{outFolder}\XRAY.{metadata.Asin}.previewData";
                    _previewDataExporter.Export(xray, pdPath);
                    _logger.Log($"X-Ray previewData file created successfully!\r\nSaved to {pdPath}");
                }
                catch (Exception ex)
                {
                    _logger.Log($"An error occurred saving the previewData file: {ex.Message}\r\n{ex.StackTrace}");
                }
            }

            _logger.Log($"X-Ray file created successfully!\r\nSaved to {newPath}");

            checkFiles(metadata.Author, metadata.Title, metadata.Asin, Path.GetFileNameWithoutExtension(txtMobi.Text));

            if (_settings.playSound)
            {
                var player = new System.Media.SoundPlayer(Environment.CurrentDirectory + @"\done.wav");
                player.Play();
            }

            metadata.Dispose();
        }