public async Task <FixtureResponse <Models.Participant> > GetParticipants()
        {
            try
            {
                var json = await File.ReadAllTextAsync(FeedPath);

                var model = JsonConvert.DeserializeObject <Fixture>(json);

                var participants = model.RawData?.Markets
                                   ?.SelectMany(x => x.Selections?.Select(p => new Models.Participant()
                {
                    Price = p.Price, Name = p.Tags?.Name
                }))
                                   .ToList();

                return(new FixtureResponse <Models.Participant> {
                    Data = participants
                });
            }
            catch (FileNotFoundException fnfe)
            {
                // log it
                return(FixtureResponse <Models.Participant> .AsErrorResponse(fnfe.Message));
            }
            catch (Exception e)
            {
                // log it
                return(FixtureResponse <Models.Participant> .AsErrorResponse(e.Message));
            }
        }
Exemple #2
0
        public async Task <FixtureResponse <Participant> > GetParticipants()
        {
            try
            {
                var feed = await File.ReadAllTextAsync(FeedPath);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(new StringReader(feed));

                var horses      = new List <Horse>();
                var horsePrices = new List <HorsePrice>();

                foreach (XmlNode race in xmlDoc.SelectNodes("/meeting/races/race"))
                {
                    foreach (XmlNode horseNode in race.SelectNodes("horses/horse"))
                    {
                        var horse = new Horse {
                            Name = horseNode.Attributes.GetNamedItem("name")?.Value
                        };
                        var numberT = horseNode.SelectSingleNode("number");
                        horse.Number = numberT?.InnerText;

                        horses.Add(horse);
                    }

                    foreach (XmlNode priceNode in race.SelectNodes("prices/price/horses/horse"))
                    {
                        var horsePrice = new HorsePrice
                        {
                            Number = priceNode.Attributes.GetNamedItem("number")?.Value,
                            Price  = priceNode.Attributes.GetNamedItem("Price")?.Value.ToDouble() ?? 0
                        };

                        horsePrices.Add(horsePrice);
                    }
                }

                var participants = horsePrices
                                   .Select(x => new Participant {
                    Price = x.Price, Name = horses.FirstOrDefault(h => h.Number == x.Number)?.Name
                })
                                   .ToList();


                return(new FixtureResponse <Participant> {
                    Data = participants
                });
            }
            catch (FileNotFoundException fnfe)
            {
                // log it
                return(FixtureResponse <Participant> .AsErrorResponse(fnfe.Message));
            }
            catch (Exception e)
            {
                // log it
                return(FixtureResponse <Participant> .AsErrorResponse(e.Message));
            }
        }
 public static DbMatch ToDbMatch(FixtureResponse fixtureResponse)
 {
     return(new DbMatch
     {
         Id = fixtureResponse.Id,
         Date = fixtureResponse.Date.ToUTC().Rfc1123,
         Status = fixtureResponse.Status.ToInt(),
         MatchDay = fixtureResponse.Matchday,
         HomeScore = fixtureResponse.HomeTeamScore,
         AwayScore = fixtureResponse.AwayTeamScore,
         HomeScoreHalf = fixtureResponse.HomeTeamScoreHalf,
         AwayScoreHalf = fixtureResponse.AwayTeamScoreHalf,
         HomeId = fixtureResponse.HomeTeamId,
         AwayId = fixtureResponse.AwayTeamId,
         LeagueId = fixtureResponse.CompetitionId
     });
 }
Exemple #4
0
        public FixtureResponse GetFixtures(string Url)
        {
            var ret = new FixtureResponse();

            var web = new HtmlWeb();

            var input = web.Load(Url);

            if (web.StatusCode != System.Net.HttpStatusCode.OK)
            {
                logger.Error($"{Url} {web.StatusCode}");
                ret.StatusCode = web.StatusCode;
                return(ret);
            }

            //Console.WriteLine($"Parsing {t.Name} results");
            var body = input.DocumentNode.SelectSingleNode("//div[@class='fixres__body callfn']");

            if (body == null)
            {
                logger.Info($"{Url} no results nodes found");
                ret.StatusCode = System.Net.HttpStatusCode.NotFound;
                return(ret);
            }

            int      year        = 0;
            DateTime kickoff     = DateTime.MinValue;
            string   hometeam    = string.Empty;
            string   awayteam    = string.Empty;
            string   competition = string.Empty;

            foreach (var node in body.ChildNodes)
            {
                try
                {
                    if (node.Name == "h3")
                    {
                        var m = node.InnerText.Split(' ');
                        year        = Int32.Parse(m[1]);
                        kickoff     = DateTime.MinValue;
                        competition = string.Empty;
                        hometeam    = string.Empty;
                        awayteam    = string.Empty;
                        logger.Debug($"New result found {node.OuterHtml}");
                    }
                    else if (node.Name == "h4")
                    {
                        kickoff  = GetKickOff(node, year);
                        hometeam = string.Empty;
                        awayteam = string.Empty;
                        logger.Debug($"Kickoff found {node.OuterHtml}");
                    }
                    else if (node.Name == "h5")
                    {
                        // on a league fixtures page, the competition is in the page title
                        competition = GetCompetition(node);
                        logger.Debug($"Competition found {node.OuterHtml}");
                    }
                    else if (node.Name == "div")
                    {
                        hometeam = GetHomeTeam(node);
                        awayteam = GetAwayTeam(node);

                        var status = GetStatus(node);
                        var id     = GetId(node);

                        logger.Debug($"Teams and scores found {node.OuterHtml}");

                        if (hometeam != string.Empty && awayteam != string.Empty && kickoff != DateTime.MinValue)
                        {
                            // add to results
                            var f = new Fixture
                            {
                                Id          = id,
                                HomeTeam    = context.GetTeam(hometeam),
                                AwayTeam    = context.GetTeam(awayteam),
                                Kickoff     = kickoff,
                                Competition = context.GetCompetition(competition)
                            };
                            if (status.ToUpper() == "FT")
                            {
                                f.HomeScore = GetHomeScore(node);
                                f.AwayScore = GetAwayScore(node);
                            }

                            ret.Fixtures.Add(f);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                    ret.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                }
            }

            ret.StatusCode = System.Net.HttpStatusCode.OK;
            return(ret);
        }