Beispiel #1
0
        public List <BranchBankCurrency> AngleSharp()
        {
            string currentBankName    = null;
            var    currencies         = new List <BranchBankCurrency>();
            var    rateFactory        = new RateFactory();
            var    descriptionFactory = new BranchBankDescriptionFactory();
            var    currencyFactory    = new BranchBankCurrencyFactory();

            var parser        = new HtmlParser();
            var document      = parser.Parse(Html);
            var currencyTable = document.QuerySelector <IHtmlTableElement>("table");

            foreach (var row in currencyTable.Rows.Skip(1))
            {
                if (!row.ClassList.Contains("tablesorter-childRow"))
                {
                    var currentBankCell = row.Cells[1];
                    currentBankName = currentBankCell.Text();
                    continue;
                }

                var cellsText = row.Cells.Select(x => x.Text()).ToArray();

                var description = descriptionFactory.GetDescription(cellsText.ElementAt(0));

                var rates = rateFactory.CreateRatesFromRawData(cellsText.Skip(1).ToArray());

                var currency = currencyFactory.GetBranchBankCurrency(currentBankName, description, rates);

                currencies.Add(currency);
            }

            return(currencies);
        }
Beispiel #2
0
        public List <BranchBankCurrency> Regex()
        {
            string currentBankName    = null;
            var    currencies         = new List <BranchBankCurrency>();
            var    rateFactory        = new RateFactory();
            var    descriptionFactory = new BranchBankDescriptionFactory();
            var    currencyFactory    = new BranchBankCurrencyFactory();


            Regex rowRegex = new Regex("<tr[^>]*?>(?<rowContent>((?!</tr>).)*)</tr>",
                                       RegexOptions.Compiled | RegexOptions.Singleline);
            Regex cellRegex = new Regex("<td[^>]*?>(?<cell>((?!</td>).)*)</td>",
                                        RegexOptions.Compiled | RegexOptions.Singleline);
            Regex attributeRegex = new Regex("(\\S+)=[\"']?((?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?",
                                             RegexOptions.Compiled | RegexOptions.Singleline);
            Regex cleanTagRegex = new Regex("\\<[^\\>]*\\>",
                                            RegexOptions.Compiled | RegexOptions.Singleline);

            foreach (var row in rowRegex.Matches(Html).Cast <Match>().Select(match => new
            {
                InnerHtml = WebUtility.HtmlDecode(match.Groups["rowContent"].ToString()),
                Content = WebUtility.HtmlDecode(match.ToString())
            }).Skip(1))
            {
                var cells = cellRegex.Matches(row.InnerHtml)
                            .Cast <Match>()
                            .Select(match => WebUtility.HtmlDecode(match.Groups["cell"].ToString()))
                            .ToList();

                var attributes = attributeRegex.Matches(row.Content)
                                 .Cast <Match>()
                                 .FirstOrDefault(m => m.ToString().Contains("tablesorter-childRow"));

                if (cells.Any(cell => !string.IsNullOrWhiteSpace(cell)) && cells.Count > 2)
                {
                    if (attributes == null)
                    {
                        currentBankName = cleanTagRegex.Replace(cells[1], string.Empty);
                        continue;
                    }

                    var rawDescription = cleanTagRegex.Replace(cells.ElementAt(0), string.Empty);

                    var description = descriptionFactory.GetDescription(rawDescription);

                    var rates = rateFactory.CreateRatesFromRawData(cells.Skip(1).ToArray());

                    var currency = currencyFactory.GetBranchBankCurrency(currentBankName, description, rates);

                    currencies.Add(currency);
                }
            }

            return(currencies);
        }
Beispiel #3
0
        public List <BranchBankCurrency> HtmlAgilityPack()
        {
            string currentBankName    = null;
            var    currencies         = new List <BranchBankCurrency>();
            var    rateFactory        = new RateFactory();
            var    descriptionFactory = new BranchBankDescriptionFactory();
            var    currencyFactory    = new BranchBankCurrencyFactory();

            HtmlDocument htmlSnippet = new HtmlDocument();

            htmlSnippet.LoadHtml(Html);

            foreach (HtmlNode row in htmlSnippet.DocumentNode.SelectNodes("//table[@id='curr_table']/tbody/tr"))
            {
                if (!row.GetAttributeValue("class", string.Empty).Contains("tablesorter-childRow"))
                {
                    var cellNodes = row.SelectNodes("td");
                    if (cellNodes != null)
                    {
                        var currentBankCell = row.SelectNodes("td").Skip(1).First();
                        currentBankName = currentBankCell.InnerText;
                    }

                    continue;
                }


                HtmlNodeCollection cells = row.SelectNodes("td");

                var cellsText = cells.Select(x => x.InnerText).ToArray();

                var description = descriptionFactory.GetDescription(cellsText.ElementAt(0));

                var rates = rateFactory.CreateRatesFromRawData(cellsText.Skip(1).ToArray());

                var currency = currencyFactory.GetBranchBankCurrency(currentBankName, description, rates);

                currencies.Add(currency);
            }

            return(currencies);
        }
Beispiel #4
0
        public List <BranchBankCurrency> CsQuery()
        {
            string currentBankName    = null;
            var    currencies         = new List <BranchBankCurrency>();
            var    rateFactory        = new RateFactory();
            var    descriptionFactory = new BranchBankDescriptionFactory();
            var    currencyFactory    = new BranchBankCurrencyFactory();

            CQ  doc           = CQ.Create(Html);
            var currencyTable = doc["#curr_table"];

            foreach (var row in currencyTable.Find("tr").Skip(1))
            {
                if (!row.Cq().HasClass("tablesorter-childRow"))
                {
                    var bankCells = row.Cq().Find("td").ToList();
                    if (bankCells.Any())
                    {
                        var currentBankCell = bankCells.Skip(1).First();
                        currentBankName = currentBankCell.Cq().Text();
                        continue;
                    }
                }

                var cellsText = row.Cq().Find("td").Select(td => td.Cq().Text()).ToArray();

                if (cellsText.Any())
                {
                    var description = descriptionFactory.GetDescription(cellsText.ElementAt(0));

                    var rates = rateFactory.CreateRatesFromRawData(cellsText.Skip(1).ToArray());

                    var currency = currencyFactory.GetBranchBankCurrency(currentBankName, description, rates);

                    currencies.Add(currency);
                }
            }

            return(currencies);
        }