public async Task LoadWithoutUnsafeNodes_DifferentContent() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); using (var fileReadStream = sampleFileInfo.OpenRead()) { // loading document first time var firstDocument = new Fb2Document(); await firstDocument.LoadAsync(fileReadStream); RewindStream(fileReadStream); // loading document without unsafe nodes var secondDocument = new Fb2Document(); await secondDocument.LoadAsync(fileReadStream, new Fb2StreamLoadingOptions(false)); firstDocument.Should().NotBe(secondDocument); var firstBook = firstDocument.Book; var secondBook = secondDocument.Book; // different content due to skipped unsafe nodes firstBook.Should().NotBe(secondBook); } }
public async Task Load_InvalidFile_Throws() { var invalidFileInfo = GetSampleFileInfo(InvalidSampleFileName); using (var stream = invalidFileInfo.OpenRead()) { var fb2Document = new Fb2Document(); await fb2Document .Invoking(async f => await f.LoadAsync(stream)) .Should() .ThrowExactlyAsync <Fb2DocumentLoadingException>() .WithMessage("Document asynchronous loading failed."); RewindStream(stream); fb2Document .Invoking(f => f.Load(stream)) .Should() .ThrowExactly <Fb2DocumentLoadingException>() .WithMessage("Document loading failed."); } var invalidSampleXmlString = await ReadFileAsString(invalidFileInfo); var secondDocument = new Fb2Document(); secondDocument .Invoking(s => s.Load(invalidSampleXmlString)) .Should() .ThrowExactly <Fb2DocumentLoadingException>() .WithMessage("Document loading failed."); }
public void Fb2Document_Empty_EqualityTest() { var emptyFirstDocument = new Fb2Document(); emptyFirstDocument.Book.Should().BeNull(); emptyFirstDocument.IsLoaded.Should().BeFalse(); emptyFirstDocument.Equals(null).Should().BeFalse(); var emptySecondDocument = new Fb2Document(); emptySecondDocument.Book.Should().BeNull(); emptySecondDocument.IsLoaded.Should().BeFalse(); emptyFirstDocument.Should().Be(emptySecondDocument); var emptyCreatedFirstDocument = Fb2Document.CreateDocument(); emptyCreatedFirstDocument.Book.Should().BeNull(); emptyCreatedFirstDocument.IsLoaded.Should().BeFalse(); var emptyCreatedSecondDocument = Fb2Document.CreateDocument(); emptyCreatedSecondDocument.Book.Should().BeNull(); emptyCreatedSecondDocument.IsLoaded.Should().BeFalse(); emptyCreatedFirstDocument.Should().Be(emptyCreatedSecondDocument); emptyFirstDocument.Should().Be(emptyCreatedFirstDocument); }
public void Fb2Document_CreateWithContent_Test() { // let's imagine there was real content, right? var emptyCreatedFirstDocument = Fb2Document.CreateDocument(new FictionBook()); emptyCreatedFirstDocument.Book.Should().NotBeNull(); emptyCreatedFirstDocument.IsLoaded.Should().BeTrue(); }
public async Task Load_WithCloseInput_ClosesStream() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); using (var fileReadStream = sampleFileInfo.OpenRead()) { // loading document without unsafe nodes var firstDocument = new Fb2Document(); await firstDocument .LoadAsync(fileReadStream, new Fb2StreamLoadingOptions(closeInputStream : true)); await fileReadStream .Invoking(async s => await s.WriteAsync(new byte[5] { 4, 2, 0, 6, 9 })) .Should() .ThrowExactlyAsync <ObjectDisposedException>(); } }
public async Task SameFile_DifferentLoads_SameContent() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); var fileStringContent = await ReadFileAsString(sampleFileInfo); var xDocument = await ReadFileAsXDocument(sampleFileInfo); var stringLoadedFb2Document = new Fb2Document(); stringLoadedFb2Document.Load(fileStringContent); // string var stringLoadedAsyncFb2Document = new Fb2Document(); await stringLoadedAsyncFb2Document.LoadAsync(fileStringContent); var xmlLoadedFb2Document = new Fb2Document(); xmlLoadedFb2Document.Load(xDocument); // xDocument var streamLoadedFb2Document = new Fb2Document(); var streamLoadedAsyncFb2Document = new Fb2Document(); using (var stream = sampleFileInfo.OpenRead()) { streamLoadedFb2Document.Load(stream); // sync stream RewindStream(stream); await streamLoadedAsyncFb2Document.LoadAsync(stream); // async stream } stringLoadedFb2Document .Should().Be(stringLoadedAsyncFb2Document) .And.Be(xmlLoadedFb2Document) .And.Be(streamLoadedFb2Document) .And.Be(streamLoadedAsyncFb2Document); stringLoadedFb2Document.Book .Should().Be(stringLoadedAsyncFb2Document.Book) .And.Be(xmlLoadedFb2Document.Book) .And.Be(streamLoadedFb2Document.Book) .And.Be(streamLoadedAsyncFb2Document.Book); }
public async Task ExportDocument_AndReload_SameContent() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); using (var fileReadStream = sampleFileInfo.OpenRead()) { // loading document first time var firstDocument = new Fb2Document(); await firstDocument.LoadAsync(fileReadStream); var firstDocXml = firstDocument.ToXml(); var secondDocument = new Fb2Document(); secondDocument.Load(firstDocXml); firstDocument.Should().Be(secondDocument); var firstBook = firstDocument.Book; var secondBook = secondDocument.Book; firstBook.Should().Be(secondBook); } }
public static bool FindTextInFb2File(string fileFullPath, string text, ref CancellationTokenSource cts) { try { using (var fileStream = new FileStream(fileFullPath, FileMode.Open)) { var document = Fb2Document.CreateDocument(); document.Load(fileStream); foreach (var documentBody in document.Bodies) { if (cts.IsCancellationRequested) { break; } foreach (var documentBodyContentNode in documentBody.Content) { if (cts.IsCancellationRequested) { break; } var contentNodeText = Fb2.Document.Extensions.XNodeExtension.GetNodeContent(documentBodyContentNode.ToXml()); if (contentNodeText.Contains(text)) { return(true); } } } } } catch (Exception) { return(false); } return(false); }
public async Task InstancesOfBookAreSame() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); using (var fileReadStream = sampleFileInfo.OpenRead()) { var firstDocument = new Fb2Document(); await firstDocument.LoadAsync(fileReadStream); RewindStream(fileReadStream); var secondDocument = Fb2Document.CreateDocument(); await secondDocument.LoadAsync(fileReadStream); firstDocument.Should().Be(secondDocument); var firstBook = firstDocument.Book; var secondBook = secondDocument.Book; firstBook.Should().Be(secondBook); fileReadStream.Close(); } }
public async Task LoadWithoutMetadata_DifferentContent() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); using (var fileReadStream = sampleFileInfo.OpenRead()) { // loading document first time var firstDocument = new Fb2Document(); await firstDocument.LoadAsync(fileReadStream); RewindStream(fileReadStream); // loading document without unsafe nodes var secondDocument = new Fb2Document(); await secondDocument.LoadAsync(fileReadStream, new Fb2StreamLoadingOptions(loadNamespaceMetadata : false)); firstDocument.Book !.NodeMetadata.Should().NotBeNull(); secondDocument.Book !.NodeMetadata.Should().BeNull(); firstDocument.Bodies.First().NodeMetadata.Should().NotBeNull(); secondDocument.Bodies.First().NodeMetadata.Should().BeNull(); } }
public async Task BookContentCheck() { var sampleFileInfo = GetSampleFileInfo(SampleFileName); using (var fileReadStream = sampleFileInfo.OpenRead()) { var document = new Fb2Document(); await document.LoadAsync(fileReadStream); document.Bodies.Should().HaveCount(3); var firstBody = document.Bodies[0]; var firstBodyTitle = firstBody.GetFirstChild <Title>(); firstBodyTitle.Should().NotBeNull(); var firstBodySections = firstBody.GetChildren <BodySection>(); firstBodySections.Should().HaveCount(9); var secondBody = document.Bodies[1]; var secondBodyAttributes = secondBody.Attributes.Should().HaveCount(1); var secondBodyNameAttribute = secondBody.Attributes.First(); secondBodyNameAttribute.Key.Should().Be(AttributeNames.Name); secondBodyNameAttribute.Value.Should().Be("notes"); var secondBodyTitle = secondBody.GetFirstChild <Title>(); secondBodyTitle.Should().NotBeNull(); var secondBodySections = secondBody.GetChildren <BodySection>(); secondBodySections.Should().HaveCount(20); var thirdBody = document.Bodies[2]; var thirdBodySections = thirdBody.GetChildren <BodySection>(); thirdBodySections.Should().HaveCount(1); document.BinaryImages.Should().HaveCount(33); } }
static void Main(string[] args) { Console.WriteLine(GetText()); Console.ReadKey(); Fb2Document fb2Document = Fb2Document.CreateDocument(); var options = new ChromeOptions { //BinaryLocation = @"C:\Program Files\Google\Chrome Beta\Application\chrome.exe" }; options.AddArgument("--log-level=3"); options.AddArgument("--disable-logging"); //options.AddArgument("--headless"); var driver = new ChromeDriver(options) { Url = "https://author.today/" }; if (!File.Exists("cookies")) { driver.FindElement(By.LinkText("Войти")).Click(); input("Войдите в свой аккаунт, и нажмите *ENTER*"); SaveCookies(driver.Manage().Cookies.AllCookies.ToArray()); } else { driver.Manage().Cookies.DeleteAllCookies(); foreach (var cookie in LoadCookies()) { driver.Manage().Cookies.AddCookie(cookie); } driver.Navigate().Refresh(); SaveCookies(driver.Manage().Cookies.AllCookies.ToArray()); } var bookId = input("Введите ссылку на книгу (https://author.today/work/119568)") .Replace("https://", "") .Replace("http://", "") .Split('/')[2] .intParse(); driver.Navigate().GoToUrl($"https://author.today/reader/{bookId}"); again: Thread.Sleep(500); var fragments = driver.FindElements(By.XPath("//div[@class='text-container']//p")); foreach (var fragment in fragments) { Console.WriteLine($"{fragment.Text}\r\n"); var textItem = new TextItem(); textItem.Load(new XText(fragment.Text)); var p = new Paragraph(); p.Content.Add(textItem); var section = new BodySection(); section.Content.Add(p); var body = new BookBody(); body.Content.Add(section); fb2Document.Book.Content.Add(body); } File.WriteAllText("book.fb2", fb2Document.ToXmlString()); try { driver.FindElement(By.XPath("//li[@class='next']//span[1]")).Click(); goto again; } catch { } Console.ReadKey(); driver.Close(); driver.Quit(); Environment.Exit(0); }