protected override Tuple <string, HttpWebRequest, HttpWebResponse> DoLoginWhenEnableVerificationCode(Tuple <string, HttpWebRequest, HttpWebResponse> cookieResult, bool isAutoOrc = true) { if (cookieResult == null) { cookieResult = HttpUtils.GetData(Site.Url, _cookie); } _cookie = cookieResult != null && cookieResult.Item2 != null ? cookieResult.Item2.CookieContainer : _cookie; Random ran = new Random(); string imgUrl = UrlUtils.CombileUrl(Site.Url, string.Format("captcha.php?sid={0}", ran.NextDouble())); OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs(); args.VerificationCodeUrl = imgUrl; args.Cookie = _cookie; args.Site = Site; string checkCodeKey = OnVerificationCode(args); if (checkCodeKey.IsNullOrEmptyOrWhiteSpace()) { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } //if (Site.IsLoginByMail) //U2是否有使用用户名登录的接口? string postData = string.Format("login_type=email&login_ajax=1&username={0}&password={1}&captcha={2}&ssl=yes", HttpUtility.UrlEncode(User.Mail), HttpUtility.UrlEncode(User.PassWord), checkCodeKey); return(HttpUtils.PostData(Site.LoginUrl, postData, _cookie)); }
private string Sign(bool isAuto, bool isAutoOrc, int count) { var result = HttpUtils.GetData(Site.SignUrl, _cookie); if (HttpUtils.IsErrorRequest(result.Item1)) { return(result.Item1); } HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(result.Item1); var imgNode = htmlDocument.DocumentNode.SelectSingleNode("//img"); if (imgNode != null && imgNode.Attributes.Contains("src")) { string imgUrl = HttpUtility.HtmlDecode(imgNode.Attributes["src"].Value); if (imgUrl.IsNullOrEmptyOrWhiteSpace()) { return("无法获取到签到验证码。"); } imgUrl = UrlUtils.CombileUrl(Site.Url, imgUrl); string checkCodeHash = imgUrl.UrlSearchKey("imagehash"); string checkCodeKey = string.Empty; Bitmap bmp = null; if (isAutoOrc) { try { bmp = ImageUtils.GetOrcImage((Bitmap)ImageUtils.ImageFromWebTest(imgUrl, _cookie)); if (bmp != null) { var orcResults = BaiDuApiUtil.WebImage(bmp); if (orcResults.Any()) { checkCodeKey = orcResults.FirstOrDefault(); string regEx = @"[^a-z0-9]"; checkCodeKey = Regex.Replace(checkCodeKey, regEx, "", RegexOptions.IgnoreCase); } } } catch (Exception ex) { Logger.Error(string.Format("{0} 验证码识别异常。异常原因:{1}", Site.Name, ex.GetInnerExceptionMessage()), ex); } } if (!isAutoOrc) { OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs(); args.VerificationCodeUrl = imgUrl; args.Site = Site; checkCodeKey = OnVerificationCode(args); } string postData = string.Format("imagehash={0}&imagestring={1}", checkCodeHash, checkCodeKey); result = HttpUtils.PostData(Site.SignUrl + "?cmd=signin", postData, _cookie); if (HttpUtils.IsErrorRequest(result.Item1)) { return(result.Item1); } var o = JsonConvert.DeserializeObject(result.Item1) as JObject; string message = o["msg"].TryPareValue <string>(); if (o["state"].TryPareValue <string>().EqualIgnoreCase("success")) { string signDay = o["signindays"].TryPareValue <string>(); string bonus = o["integral"].TryPareValue <string>(); return(string.Format("签到成功,连续天数:{0},积分:{1}", signDay, bonus)); } else { if (message.IsNullOrEmptyOrWhiteSpace()) { return(string.Format("签到失败,失败原因:{0}", "你可能已经签过到了。")); } } if (!isAuto && count <= 1) { //如果签到失败且之前是自动签到 Logger.Info(string.Format("{0} 签到失败,识别到的验证码为{1}。", Site.Name, checkCodeKey)); return(Sign(isAuto, false, ++count)); } else if (isAuto && count <= 2) { return(Sign(isAuto, true, ++count)); } else { return(string.Format("签到失败,失败原因:{0}", message)); } } else { return("签到失败,获取签到验证码失败。"); } }
private Tuple <string, HttpWebRequest, HttpWebResponse> DoLoginPostWithOutCookie(Tuple <string, HttpWebRequest, HttpWebResponse> cookieResult, bool isRetry = true) { //如果前面Cookie登录没有成功,则下面尝试没有Cookie的情况。 string postData = "username={0}&password={1}&oneCode={2}&imagestring={3}&imagehash={4}"; if (new Uri(Site.LoginUrl).Scheme == "https") { postData += string.Format("&ssl=yes&trackerssl=yes"); } string checkCodeKey = string.Empty; string checkCodeHash = string.Empty; string otpCode = string.Empty; if (Site.IsEnableVerificationCode) { string htmlResult = string.Empty; //这里先看有没有前面是不是有过请求了,如果有的话,那么直接在这里获取验证码,如果没有,则自己获取。 if (cookieResult != null && !cookieResult.Item1.IsNullOrEmptyOrWhiteSpace()) { htmlResult = cookieResult.Item1; } else { htmlResult = HttpUtils.GetData(Site.Url, _cookie).Item1; } HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(htmlResult); HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode(".//table//tr/td/img"); if (node != null) { string imgUrl = HttpUtility.HtmlDecode(node.Attributes["src"].Value); if (imgUrl.IsNullOrEmptyOrWhiteSpace()) { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } imgUrl = UrlUtils.CombileUrl(Site.Url, imgUrl); checkCodeKey = GetVerificationCode(imgUrl, isRetry); checkCodeHash = imgUrl.UrlSearchKey("imagehash"); if (checkCodeKey.IsNullOrEmptyOrWhiteSpace() || checkCodeHash.IsNullOrEmptyOrWhiteSpace()) { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } } else { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } } if (Site.isEnableTwo_StepVerification && User.isEnableTwo_StepVerification) { OnTwoStepVerificationEventArgs e = new OnTwoStepVerificationEventArgs(); e.Site = Site; otpCode = OnTwoStepVerification(e); } postData = string.Format(postData, User.UserName, User.PassWord, otpCode, checkCodeKey, checkCodeHash); var result = HttpUtils.PostData(Site.LoginUrl, postData, _cookie); if (HttpUtils.IsErrorRequest(result.Item1)) { return(result); } //如果登录失败且不是二次尝试的,则重新登录。 if (!isRetry && !IsLoginSuccess(result.Item3)) { Logger.Info(string.Format("{0} 登录没有成功,识别到的验证码为{1}。", Site.Name, checkCodeKey)); return(DoLoginPostWithOutCookie(cookieResult, false)); } else { return(result); } }
protected override bool SetTorrentTitleAndLink(HtmlNode node, PTTorrent torrent) { if (node.ParentNode != null && node.ParentNode.Attributes.Contains("id")) { torrent.Id = node.ParentNode.GetAttributeValue("id", string.Empty); } var linkUrlNode = node.SelectSingleNode("./div[contains(concat(' ', normalize-space(@class), ' '), ' name_left ')]/a"); if (linkUrlNode != null && linkUrlNode.Attributes.Contains("href")) { var linkUrl = HttpUtility.HtmlDecode(linkUrlNode.Attributes["href"].Value); torrent.LinkUrl = string.Join("/", Site.Url, linkUrl); var titleNode = linkUrlNode.SelectSingleNode("./b/text()"); if (titleNode != null) { torrent.Title = HttpUtility.HtmlDecode(titleNode.InnerText); } else { titleNode = linkUrlNode.SelectSingleNode("./b/font/text()"); if (titleNode != null) { torrent.Title = HttpUtility.HtmlDecode(titleNode.InnerText); } } var subNode = linkUrlNode.SelectSingleNode("./b//span[last()]"); if (subNode != null) { string subTitle = string.Empty; if (linkUrlNode.PreviousSibling != null && linkUrlNode.PreviousSibling.Name == "span" && !linkUrlNode.PreviousSibling.InnerText.IsNullOrEmptyOrWhiteSpace()) { subTitle = "[" + linkUrlNode.PreviousSibling.InnerText + "] "; } subTitle += subNode.InnerText; if (subNode.NextSibling != null) { subTitle += subNode.NextSibling.InnerText; } torrent.Subtitle = HttpUtility.HtmlDecode(subTitle); } } var downUrlNode = node.SelectSingleNode("./div[contains(concat(' ', normalize-space(@class), ' '), ' name_right ')]//a"); if (downUrlNode != null && downUrlNode.Attributes.Contains("href")) { torrent.DownUrl = UrlUtils.CombileUrl(Site.Url, HttpUtility.HtmlDecode(downUrlNode.GetAttributeValue("href", string.Empty))); } if (torrent.Id.IsNullOrEmptyOrWhiteSpace() || torrent.DownUrl.IsNullOrEmptyOrWhiteSpace() || torrent.LinkUrl.IsNullOrEmptyOrWhiteSpace() || torrent.Title.IsNullOrEmptyOrWhiteSpace()) { return(false); } else { return(true); } }
private string Sign(bool isAuto, bool isAutoOrc, int count) { //获取ImageHash string hostUrl = Site.Url; string hashUrl = UrlUtils.CombileUrl(hostUrl, "/image_code_ajax.php"); string postData = "action=new"; var result = HttpUtils.PostData(hashUrl, postData, _cookie); if (HttpUtils.IsErrorRequest(result.Item1)) { return(result.Item1); } JObject o = JsonConvert.DeserializeObject(result.Item1) as JObject; if (o["success"].TryPareValue <bool>()) { string checkCodeHash = o["code"].TryPareValue <string>(); string imgUrl = UrlUtils.CombileUrl(hostUrl, string.Format("image.php?action=regimage&imagehash={0}", checkCodeHash)); string checkCodeKey = string.Empty; Bitmap bmp = null; if (isAutoOrc) { try { bmp = ImageUtils.GetOrcImage((Bitmap)ImageUtils.ImageFromWebTest(imgUrl, _cookie)); if (bmp != null) { var orcResults = BaiDuApiUtil.WebImage(bmp); if (orcResults.Any()) { checkCodeKey = orcResults.FirstOrDefault(); string regEx = @"[^a-z0-9]"; checkCodeKey = Regex.Replace(checkCodeKey, regEx, "", RegexOptions.IgnoreCase); } } } catch (Exception ex) { Logger.Error(string.Format("{0} 验证码识别异常。异常原因:{1}", Site.Name, ex.GetInnerExceptionMessage()), ex); } } if (!isAutoOrc) { OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs(); args.VerificationCodeUrl = imgUrl; args.Site = Site; checkCodeKey = OnVerificationCode(args); } postData = string.Format("action=showup&imagehash={0}&imagestring={1}", checkCodeHash, checkCodeKey); result = HttpUtils.PostData(Site.SignUrl, postData, _cookie); if (HttpUtils.IsErrorRequest(result.Item1)) { return(result.Item1); } o = JsonConvert.DeserializeObject(result.Item1) as JObject; string message = o["message"].TryPareValue <string>(); if (o["success"].TryPareValue <bool>()) { return(string.Format("签到成功,积分:{0}", message)); } if (message.EqualIgnoreCase("date_unmatch")) { return(string.Format("签到失败,失败原因:{0}", message)); } if (!isAuto && count <= 1) { //如果签到失败且之前是自动签到 Logger.Info(string.Format("{0} 签到失败,识别到的验证码为{1}。", Site.Name, checkCodeKey)); return(Sign(isAuto, false, ++count)); } else if (isAuto && count <= 2) { return(Sign(isAuto, true, ++count)); } else { return(string.Format("签到失败,失败原因:{0}", message)); } } else { return("签到失败,获取签到验证码失败。"); } }