public IAsyncOperationWithProgress <HtmlDocument, HttpProgress> GetDocumentAsync(Uri uri) { reformUri(ref uri); return(Run <HtmlDocument, HttpProgress>(async(token, progress) => { var request = this.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, false); token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); var doc = new HtmlDocument(); var response = await request; using (var stream = (await response.Content.ReadAsInputStreamAsync()).AsStreamForRead()) { doc.Load(stream); } var rootNode = doc.DocumentNode; if (rootNode.ChildNodes.Count == 1 && rootNode.FirstChild.NodeType == HtmlNodeType.Text) { this.checkIPBanStatus(rootNode.FirstChild.InnerText); } do { if (response.StatusCode != HttpStatusCode.NotFound) { break; } var title = rootNode.Element("html").Element("head").Element("title"); if (title == null) { break; } if (!title.InnerText.DeEntitize().StartsWith("Gallery Not Available - ")) { break; } var error = rootNode.Element("html").Element("body")?.Element("div")?.Element("p"); if (error == null) { break; } var msg = error.InnerText.DeEntitize(); switch (msg) { case "This gallery has been removed or is unavailable.": throw new InvalidOperationException(LocalizedStrings.Resources.GalleryRemoved); case "This gallery has been locked for review. Please check back later.": throw new InvalidOperationException(LocalizedStrings.Resources.GalleryReviewing); default: throw new InvalidOperationException(msg); } } while (false); response.EnsureSuccessStatusCode(); if (HentaiVerseInfo.IsEnabled) { HentaiVerseInfo.AnalyzePage(doc); } return doc; })); }
public IAsyncOperationWithProgress <HtmlDocument, HttpProgress> GetDocumentAsync(Uri uri) { _ReformUri(ref uri); return(Run <HtmlDocument, HttpProgress>(async(token, progress) => { var request = GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, false); token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); var doc = new HtmlDocument(); var response = await request; _CheckSadPanda(response); var resstr = await response.Content.ReadAsStringAsync(); _CheckStringResponse(resstr); doc.LoadHtml(resstr); var rootNode = doc.DocumentNode; do { if (response.StatusCode != HttpStatusCode.NotFound) { break; } var title = rootNode.Element("html").Element("head").Element("title"); if (title is null) { break; } if (!title.GetInnerText().StartsWith("Gallery Not Available - ")) { break; } var error = rootNode.Element("html").Element("body")?.Element("div")?.Element("p"); if (error is null) { break; } var msg = error.GetInnerText(); switch (msg) { case "This gallery has been removed or is unavailable.": throw new InvalidOperationException(LocalizedStrings.Resources.GalleryRemoved); case "This gallery has been locked for review. Please check back later.": throw new InvalidOperationException(LocalizedStrings.Resources.GalleryReviewing); default: throw new InvalidOperationException(msg); } } while (false); response.EnsureSuccessStatusCode(); HentaiVerseInfo.AnalyzePage(doc); ApiToken.Update(resstr); return doc; })); }