/// <summary> /// 设置其他信息 /// </summary> /// <param name="nodes"></param> /// <returns></returns> protected virtual void SetTorrentOtherInfo(HtmlNodeCollection nodes, PTTorrent torrent) { //设置资源类型 var imgNode = nodes[TorrentMaps[YUEnums.TorrentMap.ResourceType]].SelectSingleNode(".//img"); if (imgNode != null && imgNode.Attributes.Contains("alt")) { torrent.ResourceType = imgNode.Attributes["alt"].Value; } HtmlNode timeNode = nodes[TorrentMaps[YUEnums.TorrentMap.TimeAlive]].SelectSingleNode(".//span"); if (timeNode != null && timeNode.Attributes.Contains("title")) { torrent.UpLoadTime = timeNode.Attributes["title"].Value.TryPareValue <DateTime>(); } else { string uploadTimeStr = nodes[TorrentMaps[YUEnums.TorrentMap.TimeAlive]].InnerText; if (!uploadTimeStr.IsNullOrEmptyOrWhiteSpace() && uploadTimeStr.Length >= 18) { string dateStr = uploadTimeStr.Substring(0, 10); string timeStr = uploadTimeStr.Substring(10); torrent.UpLoadTime = string.Format("{0} {1}", dateStr, timeStr).TryPareValue <DateTime>(); } } torrent.Size = nodes[TorrentMaps[YUEnums.TorrentMap.Size]].InnerText; torrent.SeederNumber = nodes[TorrentMaps[YUEnums.TorrentMap.SeederNumber]].InnerText.TryPareValue <int>(); torrent.LeecherNumber = nodes[TorrentMaps[YUEnums.TorrentMap.LeecherNumber]].InnerText.TryPareValue <int>(); torrent.SnatchedNumber = nodes[TorrentMaps[YUEnums.TorrentMap.SnatchedNumber]].InnerText.TryPareValue <int>(); torrent.UpLoader = nodes[TorrentMaps[YUEnums.TorrentMap.UpLoader]].InnerText; }
protected override void SetTorrentFreeTime(HtmlNode node, PTTorrent torrent) { var freeNode = node.SelectSingleNode(".//td[contains(concat(' ', normalize-space(@class), ' '), ' discount ')]//span[not(@class)][not(@style)][last()]"); if (freeNode != null && !freeNode.InnerText.IsNullOrEmptyOrWhiteSpace()) { torrent.FreeTime = "剩余:" + freeNode.InnerText; } else { freeNode = node.SelectSingleNode(".//td[contains(concat(' ', normalize-space(@class), ' '), ' embedded ')]//*[contains(normalize-space(@class), 'free')] "); if (freeNode != null && !freeNode.OuterHtml.IsNullOrEmptyOrWhiteSpace()) { string html = HttpUtility.HtmlDecode(freeNode.OuterHtml); var index = html.LastIndexOf("<span"); var lastIndex = html.LastIndexOf("</span>"); if (index > 0 && lastIndex > 0) { string span = html.Substring(index, lastIndex - index); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(span); if (htmlDocument.DocumentNode != null && htmlDocument.DocumentNode.FirstChild != null) { torrent.FreeTime = "剩余:" + htmlDocument.DocumentNode.FirstChild.InnerText; } } } } }
/// <summary> /// 设置种子Id,链接和标题 /// </summary> /// <param name="node"></param> /// <returns></returns> protected override bool SetTorrentTitleAndLink(HtmlNode node, PTTorrent torrent) { var titleNode = node.SelectSingleNode(".//td/h3/a"); var imgDownNode = node.SelectSingleNode(".//td[contains(concat(' ', normalize-space(@class), ' '), ' act ')]//img[contains(concat(' ', normalize-space(@class), ' '), ' download ')]"); if (titleNode != null && titleNode.Attributes.Contains("title") && titleNode.Attributes.Contains("href")) { var linkUrl = HttpUtility.HtmlDecode(titleNode.Attributes["href"].Value); torrent.LinkUrl = string.Join("/", Site.Url, linkUrl); torrent.Id = torrent.LinkUrl.UrlSearchKey("id"); torrent.Title = HttpUtility.HtmlDecode(titleNode.Attributes["title"].Value); } else { return(false); } if (imgDownNode != null && imgDownNode.ParentNode.Attributes.Contains("href")) { var downNode = imgDownNode.ParentNode; torrent.DownUrl = string.Join("/", Site.Url, HttpUtility.HtmlDecode(downNode.Attributes["href"].Value)); } else { return(false); } return(true); }
/// <summary> /// 设置促销信息 /// </summary> /// <param name="node"></param> /// <param name="torrent"></param> /// protected virtual void SetTorrentPromotionType(HtmlNode node, PTTorrent torrent) { string html = string.Empty; if (!torrent.Title.IsNullOrEmptyOrWhiteSpace()) { html = node.InnerHtml.Replace(torrent.Title, ""); } if (!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()) { html = node.InnerHtml.Replace(torrent.Subtitle, ""); } torrent.PromotionType = YUEnums.PromotionType.NORMAL; foreach (var item in PromoptionDict) { foreach (var value in item.Value) { if (html.Contains(value)) { torrent.PromotionType = item.Key; return; } } } }
protected override void SetTorrentPromotionType(HtmlNode node, PTTorrent torrent) { string html = string.Empty; //由于ChdBits tr行的Class bg是错误的,这里当做不可信处理,移除tr的html. node = node.SelectSingleNode("./table/tr"); if (!torrent.Title.IsNullOrEmptyOrWhiteSpace()) { html = node.InnerHtml.Replace(torrent.Title, ""); } if (!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()) { html = node.InnerHtml.Replace(torrent.Subtitle, ""); } torrent.PromotionType = YUEnums.PromotionType.NORMAL; foreach (var item in PromoptionDict) { foreach (var value in item.Value) { //ChdBits所有bg Class都不可信。 if (html.Contains(value) && !value.Contains("bg")) { torrent.PromotionType = item.Key; return; } } } }
protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { //判断有没有官方的注解如[国语][中字]等 var subNode = node.SelectSingleNode(".//td[1]/div[not(contains(concat(' ', normalize-space(@class), ' '), ' progressBar '))]"); if (subNode == null) { subNode = node.SelectSingleNode(".//td[1]/br"); if (subNode != null && subNode.NextSibling != null) { torrent.Subtitle = HttpUtility.HtmlDecode(subNode.NextSibling.InnerText); } } else if (subNode.NextSibling != null) { string tagTitle = string.Empty; var tagNodes = subNode.SelectNodes("./div[contains(concat(' ', normalize-space(@class), ' '), ' tag ')]"); if (tagNodes != null && tagNodes.Any()) { foreach (var tagNode in tagNodes) { if (!tagNode.InnerText.IsNullOrEmptyOrWhiteSpace()) { tagTitle += string.Format("[{0}]", tagNode.InnerText); } } } torrent.Subtitle = string.Format("{0} {1}", tagTitle, HttpUtility.HtmlDecode(subNode.NextSibling.InnerText)); } return(!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()); }
protected override void SetTorrentFreeTime(HtmlNode node, PTTorrent torrent) { var freeNode = node.SelectSingleNode(".//td[contains(concat(' ', normalize-space(@class), ' '), ' embedded ')]/span[last()]"); if (freeNode != null && !freeNode.InnerText.IsNullOrEmptyOrWhiteSpace()) { torrent.FreeTime = "剩余:" + freeNode.InnerText; } }
protected override void SetTorrentFreeTime(HtmlNode node, PTTorrent torrent) { var freeNode = node.SelectSingleNode("./div[contains(concat(' ', normalize-space(@class), ' '), ' name_left ')]/span[not(@class)][not(@id)]"); if (freeNode != null && !freeNode.InnerText.IsNullOrEmptyOrWhiteSpace()) { torrent.FreeTime = HttpUtility.HtmlDecode(freeNode.InnerText); } }
protected override void SetTorrentFreeTime(HtmlNode node, PTTorrent torrent) { var freeNode = node.SelectSingleNode(".//td[contains(concat(' ', normalize-space(@class), ' '), ' embedded ')]//time"); if (freeNode != null && !freeNode.InnerText.IsNullOrEmptyOrWhiteSpace()) { torrent.FreeTime = "剩余:" + HttpUtility.HtmlDecode(freeNode.InnerText.Replace("­", "")); } }
/// <summary> /// 获取种子下载名称 /// </summary> /// <param name="torrent"></param> /// <returns></returns> public string GetTorrentDownFileName(PTTorrent torrent) { if (torrent != null) { string fileName = string.Format("[{0}].{1}.{2}", SiteId, torrent.Title.Trim(), "torrent"); if (Global.Config.IsEnablePostFileName) { try { var url = torrent.DownUrl; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "GET"; //这里用IE9的内核解析 httpWebRequest.UserAgent = "Mozilla / 5.0(compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident / 5.0)"; httpWebRequest.KeepAlive = true; httpWebRequest.Timeout = 3000; httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br"); httpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh"); httpWebRequest.CookieContainer = _cookie; HttpWebResponse webRespon = (HttpWebResponse)httpWebRequest.GetResponse(); if (webRespon.Headers.AllKeys.Contains("Content-Disposition")) { string contentDis = webRespon.Headers.Get("Content-Disposition"); if (!contentDis.IsNullOrEmptyOrWhiteSpace()) { string[] headers = contentDis.Split(';', '='); if (headers.Length >= 3 && headers[2].Contains("torrent")) { fileName = HttpUtility.UrlDecode(headers[2]).Replace("\"", "").Trim(); } } } } catch (Exception ex) { Logger.Error(string.Format("{0} 获取种子文件名称失败,种子标题:{1}", SiteId, torrent.Title), ex); } } foreach (var car in Path.GetInvalidFileNameChars()) { if (fileName.Contains(car)) { fileName = fileName.Replace(car, '_'); } } return(fileName); } else { throw new Exception("获取种子文件名称失败,失败原因:无法获取到种子信息。"); } }
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 = titleNode.InnerText; } else { titleNode = linkUrlNode.SelectSingleNode("./b/font/text()"); if (titleNode != null) { torrent.Title = titleNode.InnerText; } } var subNode = linkUrlNode.SelectSingleNode("./b//span[last()]"); if (subNode != null) { string subTtile = subNode.InnerText; if (subNode.NextSibling != null) { subTtile += subNode.NextSibling.InnerText; } torrent.Subtitle = subTtile; } } var downUrlNode = node.SelectSingleNode("./div[contains(concat(' ', normalize-space(@class), ' '), ' name_right ')]//a"); if (downUrlNode != null && downUrlNode.Attributes.Contains("href")) { torrent.DownUrl = string.Join("/", 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); } }
protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { bool result = base.SetTorrentSubTitle(node, torrent); if (!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()) { torrent.Subtitle = torrent.Subtitle.Replace("[优惠剩余时间:", ""); } return(result); }
protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { var subNode = node.SelectSingleNode(".//td[2]/br"); if (subNode != null && subNode.NextSibling != null) { torrent.Subtitle = HttpUtility.HtmlDecode(subNode.NextSibling.InnerText); } return(false); }
protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { var subNode = node.SelectSingleNode(".//span[contains(concat(' ', normalize-space(@class), ' '), ' tooltip ')]"); if (subNode != null) { torrent.Subtitle = subNode.InnerText; } return(!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()); }
/// <summary> /// 设置副标题 /// </summary> /// <param name="node"></param> /// <returns></returns> protected virtual bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { var subNode = node.SelectSingleNode(".//td/font[contains(concat(' ', normalize-space(@class), ' '), ' subtitle ')]"); if (subNode != null) { torrent.Subtitle = HttpUtility.HtmlDecode(subNode.InnerText); return(true); } return(false); }
protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { //获取副标题在上面处理了,这里不再处理,以上面结果为准。 if (torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()) { return(false); } else { return(true); } }
/// <summary> /// 设置副标题 /// </summary> /// <param name="node"></param> /// <returns></returns> protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { var subNode = node.SelectSingleNode(".//td/h4"); if (subNode != null) { if (torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()) { torrent.Subtitle = HttpUtility.HtmlDecode(subNode.InnerText); } } return(!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()); }
protected override void SetTorrentOtherInfo(HtmlNodeCollection nodes, PTTorrent torrent) { //设置资源类型 var imgNode = nodes[TorrentMaps[YUEnums.TorrentMap.ResourceType]].SelectSingleNode(".//img"); if (imgNode != null && imgNode.Attributes.Contains("alt")) { torrent.ResourceType = imgNode.Attributes["alt"].Value; } string uploadTimeStr = nodes[TorrentMaps[YUEnums.TorrentMap.TimeAlive]].InnerText; if (!uploadTimeStr.IsNullOrEmptyOrWhiteSpace() && uploadTimeStr.Length >= 18) { string dateStr = uploadTimeStr.Substring(0, 10); string timeStr = uploadTimeStr.Substring(10); torrent.UpLoadTime = string.Format("{0} {1}", dateStr, timeStr).TryPareValue <DateTime>(); } torrent.Size = nodes[TorrentMaps[YUEnums.TorrentMap.Size]].InnerText; var seedNodes = nodes[TorrentMaps[YUEnums.TorrentMap.SeederNumber]].SelectNodes("./b"); if (seedNodes != null && seedNodes.Count > 0) { torrent.SeederNumber = seedNodes[0].InnerText.TryPareValue <int>(); if (seedNodes.Count > 1) { torrent.LeecherNumber = seedNodes[1].InnerText.TryPareValue <int>(); } } var snatchNode = nodes[TorrentMaps[YUEnums.TorrentMap.SnatchedNumber]].SelectSingleNode("./text()"); if (snatchNode != null) { torrent.SnatchedNumber = snatchNode.InnerText.TryPareValue <int>(); } torrent.UpLoader = nodes[TorrentMaps[YUEnums.TorrentMap.UpLoader]].InnerText; }
protected override bool SetTorrentSubTitle(HtmlNode node, PTTorrent torrent) { //判断有没有官方的注解如[国语][中字]等 var subNode = node.SelectSingleNode(".//td[1]/div[not(contains(concat(' ', normalize-space(@class), ' '), ' progressBar '))]"); if (subNode == null) { subNode = node.SelectSingleNode(".//td[1]/br"); if (subNode != null && subNode.NextSibling != null) { torrent.Subtitle = HttpUtility.HtmlDecode(subNode.NextSibling.InnerText); } } else if (subNode.NextSibling != null) { torrent.Subtitle = string.Format("{0} {1}", subNode.InnerText, HttpUtility.HtmlDecode(subNode.NextSibling.InnerText)); return(true); } return(false); }
/// <summary> /// 设置HR信息 /// </summary> /// <param name="node"></param> /// <param name="torrent"></param> protected virtual void SetTorrentHR(HtmlNode node, PTTorrent torrent) { string html = string.Empty; if (!torrent.Title.IsNullOrEmptyOrWhiteSpace()) { html = node.InnerHtml.Replace(torrent.Title, ""); } if (!torrent.Subtitle.IsNullOrEmptyOrWhiteSpace()) { html = node.InnerHtml.Replace(torrent.Subtitle, ""); } torrent.IsHR = false; if (html.Contains("hit_run")) { torrent.IsHR = true; return; } }
protected override bool SetTorrentTitleAndLink(HtmlNode node, PTTorrent torrent) { var nodes = node.SelectNodes(".//td[contains(concat(' ', normalize-space(@class), ' '), ' embedded ')]//a"); if (nodes != null && nodes.Count >= 2) { var titleNode = nodes[0]; var downNode = nodes[1]; if (titleNode != null && titleNode.Attributes.Contains("href")) { var linkUrl = HttpUtility.HtmlDecode(titleNode.Attributes["href"].Value); torrent.LinkUrl = string.Join("/", Site.Url, linkUrl); torrent.Id = torrent.LinkUrl.UrlSearchKey("id"); torrent.Title = HttpUtility.HtmlDecode(titleNode.InnerText); } else { return(false); } if (downNode != null && downNode.Attributes.Contains("href")) { torrent.DownUrl = string.Join("/", Site.Url, HttpUtility.HtmlDecode(downNode.Attributes["href"].Value)); if (!(torrent.DownUrl.Contains("download") && torrent.DownUrl.Contains(torrent.Id))) { torrent.DownUrl = string.Join("/", Site.Url, string.Format("download.php?id={0}", torrent.Id)); } } else { return(false); } return(true); } return(false); }
public List <PTTorrent> SearchTorrent(string searchKey, YUEnums.PromotionType promotionType = YUEnums.PromotionType.ALL, YUEnums.AliveType aliveType = YUEnums.AliveType.ALL, YUEnums.FavType favType = YUEnums.FavType.ALL) { if (Site.SearchUrl.IsNullOrEmptyOrWhiteSpace()) { //站点还没有支持搜索 throw new Exception(string.Format("搜索种子错误,错误站点:{0},关键字:{1},错误原因:该站点尚未支持搜索。", Site.Name, searchKey)); } if (_cookie == null || _cookie.Count <= 0) { throw new Exception(string.Format("搜索种子错误,错误站点:{0},关键字:{1},错误原因:未登录,请先登录。", Site.Name, searchKey)); } string searchUrl = BuildSearchUrl(searchKey, promotionType, aliveType, favType); string htmlResult = HttpUtils.GetDataGetHtml(searchUrl, _cookie); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(htmlResult);//加载HTML字符串,如果是文件可以用htmlDocument.Load方法加载 var trNodes = GetTorrentNodes(htmlDocument); //如果只有一个节点,那么应该是Table的标题,这里忽略,从第二个节点开始算。 if (trNodes == null) { throw new Exception(string.Format("{0}无法搜索到对应结果,请尝试更换其他关键字。", SiteId)); } else { List <PTTorrent> torrents = new List <PTTorrent>(); for (int i = 1; i < trNodes.Count; i++) { var trNode = trNodes[i]; var tdNodes = GetTorrentNodes(trNode); //这里一般有10列,但是Frds为9列,所以这里以9列来处理 if (tdNodes == null || tdNodes.Count < 9) { continue; } else { PTTorrent torrent = new PTTorrent(); torrent.SiteId = SiteId; //tdNodes[1]为种子信息 //种子链接和标题是最重要的,如果这里拿不到,直接跳过了 if (!SetTorrentTitleAndLink(tdNodes[TorrentMaps[YUEnums.TorrentMap.Detail]], torrent)) { continue; } //这里的副标题如果没有的话,是否需要跳过?暂时先保留把。 SetTorrentSubTitle(tdNodes[TorrentMaps[YUEnums.TorrentMap.Detail]], torrent); SetTorrentPromotionType(tdNodes[TorrentMaps[YUEnums.TorrentMap.Detail]], torrent); SetTorrentHR(tdNodes[TorrentMaps[YUEnums.TorrentMap.Detail]], torrent); SetTorrentOtherInfo(tdNodes, torrent); torrents.Add(torrent); } } return(torrents); } }
protected override void SetTorrentHR(HtmlNode node, PTTorrent torrent) { torrent.IsHR = YUEnums.HRType.HR; }