public static async Task LoadImage(SciHubData data) { var doc = new HtmlDocument(); doc.LoadHtml(data.Html); data.Id = doc.DocumentNode .SelectSingleNode(".//input[@type='hidden']") .GetAttributeValue("value", string.Empty); var img = doc.DocumentNode .SelectSingleNode(".//img") .GetAttributeValue("src", string.Empty); var shortUrl = data.Url; shortUrl = shortUrl.Substring(0, shortUrl.IndexOf('/', 10)); shortUrl += img; try { data.ImageData = await WebUtility.SimpleByteRequest(shortUrl); if (data.ImageData == null) { throw new Exception("Somehow no image data was loaded"); } } catch (Exception e) { throw new SciHubException(SciHubExceptionType.Captcha, e); } }
public static async Task PostCaptcha(SciHubData data, string solved) { var body = $"id={data.Id}&answer={solved}"; using (var res = await WebUtility.SimpleRequest(data.Url, HttpMethod.Post, body, CancellationToken.None)) { var bytes = await res.Content.ReadAsByteArrayAsync(); if (!IsHtml(bytes)) { data.ArticleData = bytes; } } }
private static async Task <byte[]> LoadArticleInternal(SciHubData data, ICaptchaSolvingStrategy solvingStrategy) { var bytes = await WebUtility.SimpleByteRequest(data.Url); if (bytes == null || bytes.Length < 6) { throw new SciHubException(SciHubExceptionType.Article); } if (!IsHtml(bytes)) { return(bytes); } var fullHtml = Encoding.UTF8.GetString(bytes); data.Html = fullHtml; await LoadImage(data); do { var solved = await solvingStrategy.Solve(data.ImageData); if (solved.Aborted) { return(null); } if (solved.Reload) { return(await LoadArticleInternal(data, solvingStrategy)); } await PostCaptcha(data, solved.Captcha); if (data.ArticleData == null) { await solvingStrategy.Invalidate(solved.Captcha); } }while (data.ArticleData == null); return(data.ArticleData); }
public static async Task <byte[]> LoadArticle(string url, ICaptchaSolvingStrategy strategy) { var data = new SciHubData { Url = url }; try { await strategy.Initialize(); var result = await LoadArticleInternal(data, strategy); return(result); } catch { throw; } finally { await strategy.Finish(); } }