/// <summary> /// /// </summary> /// <param name="Protocol"></param> /// <param name="AnonymityLevel"></param> /// <param name="SpeedRateAtLeast"></param> /// <param name="ConnectionTimeRateAtLeast"></param> /// <returns></returns> public FreeProxy GetFreeProxy(ProxyProtocolEnum Protocol = ProxyProtocolEnum.HTTP , ProxyAnonymityLevelEnum AnonymityLevel = ProxyAnonymityLevelEnum.High , int SpeedRateAtLeast = 35 , int ConnectionTimeRateAtLeast = 35) { while (this.FreeProxies == null || this.FreeProxies.Count == 0) { Thread.Sleep(10); } _again: int _faultCount = 0; FreeProxy _freePrx = null; try { var _proxies = FreeProxies.ToArray().Where(p => p.UsageTokenId != UsageTokenId && p.Protocol == Protocol && p.AnonymityLevel == AnonymityLevel && p.SpeedRate >= SpeedRateAtLeast && p.ConnectionTimeRate >= ConnectionTimeRateAtLeast); if (_proxies == null || _proxies.Count() == 0) { UsageTokenId = Guid.NewGuid(); } _freePrx = FreeProxies.ToArray().Where(p => p.UsageTokenId != UsageTokenId && p.Protocol == Protocol && p.AnonymityLevel == AnonymityLevel && p.SpeedRate >= SpeedRateAtLeast && p.ConnectionTimeRate >= ConnectionTimeRateAtLeast).FirstOrDefault(); } catch (ArgumentException ex) { if (_faultCount > 3) { throw ex; } else { _faultCount++; Thread.Sleep(1000); goto _again; } } if (_freePrx != null) { _freePrx.UsageTokenId = UsageTokenId; } return(_freePrx); }
/// <summary> /// /// </summary> /// <param name="hmaPgDoc"></param> private void ExtractProxies(string hmaPgDoc) { HtmlAgilityPack.HtmlDocument _doc = new HtmlAgilityPack.HtmlDocument(); _doc.LoadHtml(hmaPgDoc); var _tblNode = HtmlUtil.GetNode(_doc.DocumentNode, "table", "id", "listtable"); if (_tblNode != null) { var _tRowNodes = _tblNode.Descendants("tr"); if (_tRowNodes != null && _tRowNodes.Count() > 0) { if (this.FreeProxies == null) { this.FreeProxies = new Queue <FreeProxy>(); } for (int ctr = 1; ctr < _tRowNodes.Count(); ctr++) { FreeProxy _freeProxy = null; try { if (ParseRowToProxy(_tRowNodes.ToArray()[ctr], ref _freeProxy)) { if (_freeProxy != null && !this.FreeProxies.ToList().Exists(p => p.Host.Equals(_freeProxy.Host))) { this.FreeProxies.Enqueue(_freeProxy); } } } catch (Exception ex) { } } } } }
/// <summary> /// /// </summary> /// <param name="rowNode"></param> /// <param name="freeProxy"></param> /// <returns></returns> private bool ParseRowToProxy(HtmlAgilityPack.HtmlNode rowNode, ref FreeProxy freeProxy) { try { var _prxCells = rowNode.Descendants("td"); if (_prxCells != null && _prxCells.Count() > 0) { freeProxy = new FreeProxy(); freeProxy.Host = SnatchIPAddress(_prxCells.ToArray()[1].InnerHtml); freeProxy.Port = int.Parse(_prxCells.ToArray()[2].InnerText); string _country = _prxCells.ToArray()[3].InnerText.Trim().ToUpper(); _country = _country.Replace("; ", "___").Replace(", ", "__").Replace(" ", "_"); freeProxy.Country = (ProxyCountry)Enum.Parse(typeof(ProxyCountry), _country); var _connspeedNode = _prxCells.ToArray()[5]; _connspeedNode = HtmlUtil.GetNode(_connspeedNode, "div", "class", "speedbar connection_time"); _connspeedNode = _connspeedNode.Descendants("div").ToArray()[0]; string _rate = _connspeedNode.Attributes["style"].Value.Replace("width:", "").Replace("%", ""); freeProxy.ConnectionTimeRate = int.Parse(_rate); if (freeProxy.ConnectionTimeRate <= 35) { freeProxy.ConnectionTime = ProxyConnectionTimeEnum.Slow; } else if (freeProxy.ConnectionTimeRate > 35 && freeProxy.ConnectionTimeRate <= 65) { freeProxy.ConnectionTime = ProxyConnectionTimeEnum.Medium; } else if (freeProxy.ConnectionTimeRate > 65) { freeProxy.ConnectionTime = ProxyConnectionTimeEnum.Medium; } var _speedNode = _prxCells.ToArray()[4]; _speedNode = HtmlUtil.GetNode(_speedNode, "div", "class", "speedbar response_time"); _speedNode = _speedNode.Descendants("div").ToArray()[0]; _rate = _speedNode.Attributes["style"].Value.Replace("width:", "").Replace("%", ""); freeProxy.SpeedRate = int.Parse(_rate); if (freeProxy.SpeedRate <= 35) { freeProxy.Speed = ProxySpeedEnum.Slow; } else if (freeProxy.SpeedRate > 35 && freeProxy.SpeedRate <= 65) { freeProxy.Speed = ProxySpeedEnum.Medium; } else if (freeProxy.SpeedRate > 65) { freeProxy.Speed = ProxySpeedEnum.Medium; } string _protocol = _prxCells.ToArray()[6].InnerText.Replace("/", "_"); freeProxy.Protocol = (ProxyProtocolEnum)Enum.Parse(typeof(ProxyProtocolEnum), _protocol); string _anonymity = _prxCells.ToArray()[7].InnerText.Replace(" +", "__"); freeProxy.AnonymityLevel = (ProxyAnonymityLevelEnum)Enum.Parse(typeof(ProxyAnonymityLevelEnum), _anonymity); return(true); } } catch (Exception ex) { throw ex; } return(false); }