示例#1
0
        public List <OddsInfoModel> GetOdds(string oddsAsHtmlStr, string roundTag)
        {
            int spieltag = int.Parse(roundTag);

            var tableIndex = 1;

            switch (spieltag)
            {
            case 8:
                tableIndex = 2;
                break;
            }

            var oddsList = new List <OddsInfoModel>();

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(oddsAsHtmlStr);

            // remove all unnecessary html
            {
                ScrubHelper.ScrubHtml(doc);

                var nodes = doc.DocumentNode.SelectNodes("//comment()");
                if (nodes != null)
                {
                    foreach (HtmlNode comment in nodes)
                    {
                        comment.ParentNode.RemoveChild(comment);
                    }
                }
            }

            var htmlRows = doc.DocumentNode
                           .SelectSingleNode("//body")
                           .SelectSingleNode(string.Format("(//table)[{0}]", tableIndex))
                           .CssSelect("tr");


            var iter = htmlRows.GetEnumerator();

            if (iter.MoveNext())
            {
                // skip two header rows
                iter.MoveNext();
                while (iter.MoveNext())
                {
                    var newModel = new OddsInfoModel();

                    var cells = iter.Current.CssSelect("td").ToArray();

                    if (cells.Length > 0)
                    {
                        var kickoff = ExtractKickoff(cells[0].InnerText);
                        if (kickoff.HasValue == false)
                        {
                            continue;
                        }

                        var team1 = (spieltag < 7) ? ExtractTeams(cells[2]) : ExtractTeams(cells[1]);
                        var team2 = (spieltag < 7) ? ExtractTeams(cells[3]) : ExtractTeams(cells[2]);

                        if (spieltag == 10)
                        {
                            team1 = "Portugal";
                            team2 = "Frankreich";
                        }

                        if (string.IsNullOrEmpty(team1) || string.IsNullOrEmpty(team2))
                        {
                            continue;
                        }

                        newModel.Id             = 0;
                        newModel.HomeTeam       = team1;
                        newModel.HomeTeamSearch = team1.Substring(0, 3).ToUpper();
                        newModel.AwayTeam       = team2;
                        newModel.AwayTeamSearch = team2.Substring(0, 3).ToUpper();
                        newModel.WinOdds        = (spieltag < 7) ? ExtractOdds(cells[5].InnerText) : ExtractOdds(cells[4].InnerText);
                        newModel.DrawOdds       = (spieltag < 7) ? ExtractOdds(cells[6].InnerText): ExtractOdds(cells[5].InnerText);
                        newModel.LossOdds       = (spieltag < 7) ? ExtractOdds(cells[7].InnerText) : ExtractOdds(cells[6].InnerText);

                        // add fix search expressions
                        {
                            newModel.AwayTeamSearch2 = newModel.AwayTeamSearch;
                            newModel.HomeTeamSearch2 = newModel.HomeTeamSearch;

                            // Niederlande
                            if (newModel.AwayTeamSearch == "HOL")
                            {
                                newModel.AwayTeamSearch2 = "NIE";
                            }
                            else if (newModel.HomeTeamSearch == "HOL")
                            {
                                newModel.HomeTeamSearch2 = "NIE";
                            }
                        }


                        oddsList.Add(newModel);
                    }
                }
            }

            return(oddsList);
        }
        private List <OddsInfoModel> GetOdds(HtmlDocument doc, string roundTag)
        {
            int spieltag = int.Parse(roundTag);

            var oddsList = new List <OddsInfoModel>();

            {
                // remove all unnecessary html
                {
                    ScrubHelper.ScrubHtml(doc);

                    var nodes = doc.DocumentNode.SelectNodes("//comment()");
                    if (nodes != null)
                    {
                        foreach (HtmlNode comment in nodes)
                        {
                            comment.ParentNode.RemoveChild(comment);
                        }
                    }
                }

                HtmlNode spieltagNode = GetSpieltagHtml(doc, spieltag);
                if (spieltagNode == null)
                {
                    throw new ApplicationException("Spieltag nicht gefunden.");
                }

                HtmlNode oddsTableNode = spieltagNode
                                         .ParentNode
                                         .ParentNode
                                         .ParentNode
                                         .NextSibling;

                // Odds
                {
                    HtmlNode trNode = oddsTableNode.NextSibling.NextSibling.NextSibling;

                    for (int ii = 3; ii < 12; ii++)
                    {
                        Debug.WriteLine(trNode.InnerHtml);
                        var model = new OddsInfoModel();

                        int kk = 0;
                        foreach (var tdNode in trNode.SelectNodes("./td"))
                        {
                            switch (kk)
                            {
                            case 1:
                                model.HomeTeam       = tdNode.InnerText;
                                model.HomeTeamSearch = model.HomeTeam.ToUpper();
                                break;

                            case 2:
                                model.AwayTeam       = tdNode.InnerText;
                                model.AwayTeamSearch = model.AwayTeam.ToUpper();
                                break;

                            case 4:
                            {
                                var oddsNode = tdNode.SelectSingleNode(".//strong");
                                model.WinOdds = ScrubHelper.ConvertToDouble(oddsNode);
                            }
                            break;

                            case 5:
                            {
                                var oddsNode = tdNode.SelectSingleNode(".//strong");
                                model.DrawOdds = ScrubHelper.ConvertToDouble(oddsNode);
                            }
                            break;

                            case 6:
                            {
                                var oddsNode = tdNode.SelectSingleNode(".//strong");
                                model.LossOdds = ScrubHelper.ConvertToDouble(oddsNode);
                            }
                            break;
                            }

                            kk++;
                        }

                        trNode = trNode.NextSibling.NextSibling;

                        oddsList.Add(model);
                    }
                }
            }

            return(oddsList);
        }