public Common.Plugin.Metadata ExtractMetadata(string inputPath) { try { using (var stream = new FileStream(inputPath, FileMode.Open, FileAccess.Read)) { var meta = new MobiMetadata(stream); var metadata = new Common.Plugin.Metadata { Abstract = Regex.Replace(meta.MobiHeader.ExthHeader.Description, "<.*?>", Empty), Title = IsNullOrEmpty(meta.MobiHeader.ExthHeader.UpdatedTitle) ? Path.GetFileNameWithoutExtension(inputPath) : meta.MobiHeader.ExthHeader.UpdatedTitle, Asin = meta.MobiHeader.ExthHeader.Asin, Isbn = meta.MobiHeader.ExthHeader.Ibsn, Subject = meta.MobiHeader.ExthHeader.Subject }; // metadata.Title = Utils.CleanInput(metadata.Title); DateTime publisheddate; if (DateTime.TryParse(meta.MobiHeader.ExthHeader.PublishedDate, out publisheddate)) { metadata.PublishedDate = publisheddate; } metadata.Authors = new List <Author>(); if (meta.MobiHeader.ExthHeader.Author.Contains(",")) { var split = meta.MobiHeader.ExthHeader.Author.Split(','); var author = new Author { FirstName = split[1].Replace(" ", Empty), LastName = split[0].Replace(" ", Empty) }; metadata.Authors.Add(author); } else { var split = meta.MobiHeader.ExthHeader.Author.Split(' '); var author = new Author(); if (split.Length > 1) { author.FirstName = split[0]; author.LastName = split[1]; metadata.Authors.Add(author); } } return(metadata); } } catch (Exception ex) { throw new BookieException("Error with mobi file", ex); } }
private static ListViewItem AddMobi(FileSystemInfo file) { MobiMetadata mobiMetadata = new MobiMetadata(file.FullName); ListViewItem listViewItem1 = new ListViewItem() { Text = file.Name, Tag = (object)(file.FullName + "| non-retail | " + mobiMetadata.MobiHeader.EXTHHeader.ASIN) }; string str = mobiMetadata.MobiHeader.EXTHHeader.Author; if (str.Contains <char>(',')) { string[] strArray = str.Split(','); str = strArray[1].Trim() + " " + strArray[0]; } string updatedTitle = mobiMetadata.MobiHeader.EXTHHeader.UpdatedTitle; string text1 = mobiMetadata.MobiHeader.EXTHHeader.Publisher.Split(',')[0]; string text2 = Book.DigitsOnly(mobiMetadata.MobiHeader.EXTHHeader.PublishedDate.Split('-')[0]); if (string.IsNullOrWhiteSpace(text2) || Convert.ToInt32(text2) > DateTime.Now.Year) { text2 = "0"; } string text3 = ""; if (!string.IsNullOrEmpty(mobiMetadata.MobiHeader.EXTHHeader.IBSN)) { text3 = Book.IsISBNValid(Book.DigitsOnly(mobiMetadata.MobiHeader.EXTHHeader.IBSN)) ? Book.DigitsOnly(mobiMetadata.MobiHeader.EXTHHeader.IBSN) : ""; } else if (mobiMetadata.MobiHeader.EXTHHeader.Source.Contains("ISBN")) { text3 = Book.IsISBNValid(Book.DigitsOnly(mobiMetadata.MobiHeader.EXTHHeader.Source)) ? Book.DigitsOnly(mobiMetadata.MobiHeader.EXTHHeader.Source) : ""; } string description = mobiMetadata.MobiHeader.EXTHHeader.Description; listViewItem1.SubItems.Add("None"); listViewItem1.SubItems.Add(str); listViewItem1.SubItems.Add(updatedTitle); listViewItem1.SubItems.Add(text1); listViewItem1.SubItems.Add(text2); listViewItem1.SubItems.Add(text3); listViewItem1.SubItems.Add(description); listViewItem1.SubItems.Add("0"); listViewItem1.SubItems.Add(""); listViewItem1.SubItems.Add(""); ListViewItem listViewItem2 = BiblioUpTik.Properties.Settings.Default.OnRun ? Book.ScrapeMobiData(listViewItem1) : listViewItem1; listViewItem2.SubItems[8].Text = Book.DigitsOnly(listViewItem2.SubItems[8].Text); if (string.IsNullOrWhiteSpace(listViewItem2.SubItems[8].Text)) { listViewItem2.SubItems[8].Text = "0"; } for (int index = 2; index < listViewItem2.SubItems.Count; ++index) { listViewItem2.SubItems[index].Text = HttpUtility.HtmlDecode(listViewItem2.SubItems[index].Text); } return(listViewItem2); }
public async Task Mobi7ParserShouldExtractNavigationStream() { await using var stream = AssetFactory.GetAsset("pg42324.mobi"); await using var rawMlStream = new MobiMetadata(stream).GetRawMlStream(); var parser = new Mobi7Parser(); var contentStream = await parser.GetNavigationStream(rawMlStream); Assert.IsNotNull(contentStream); Assert.AreEqual(4133, contentStream.Length); }
public HashSet <Occurrence> FindOccurrences(IMetadata metadata, Term term, Paragraph paragraph) { if (!term.Match) { return(new HashSet <Occurrence>()); } return(metadata switch { MobiMetadata _ => FindOccurrencesLegacy(term, paragraph), KfxContainer _ => FindOccurrences(term, paragraph), _ => FindOccurrencesLegacy(term, paragraph) });
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); }
/// <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); }