コード例 #1
0
        public ActionResult <IEnumerable <List_LeagueOddsFixture> > GetOdds_SoccerByLeague_ThisDay(string leagueID)
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US"));
            // Initial List
            List <Football_API.Models.Models_Fixture.Fixture> ListFixture = new List <Football_API.Models.Models_Fixture.Fixture>();
            List <Football_API.Models.Models_Odds.Odd>        ListOdds    = new List <Football_API.Models.Models_Odds.Odd>();
            List <LeagueOddsFixture>      ListLeagueFixOdds      = new List <LeagueOddsFixture>();
            List <List_LeagueOddsFixture> Ret_LeagureOddsFixture = new List <List_LeagueOddsFixture>();

            //Get Json object Fixture
            string JsonStr_Fixture = GetFixtureByLeagueID(leagueID, date);
            List <RootObjectFixture> APIJsonObjectFixture = JsonConvert.DeserializeObject <List <RootObjectFixture> >(JsonStr_Fixture);

            if (APIJsonObjectFixture != null)
            {
                ListFixture = APIJsonObjectFixture[0].api.fixtures; //Json ก้อนใหญ่มีก้อนเดียวเสมอ
            }

            if (ListFixture.Count != 0)
            {
                //Get Json object Odds
                string JsonStr_Odds = GetOddByLeagueID(leagueID);
                List <RootObjectOdds> APIJsonObjectOdds = JsonConvert.DeserializeObject <List <RootObjectOdds> >(JsonStr_Odds);
                if (APIJsonObjectOdds != null)
                {
                    ListOdds = APIJsonObjectOdds[0].api.odds; //Json ก้อนใหญ่มีก้อนเดียวเสมอ
                }

                //====== Match FixtureID between "Fixture" & "Odds" =======
                foreach (var eachfixture in ListFixture)
                {
                    //หาตัวที่มี FixtureID เดียวกัน
                    var OddsMatch = ListOdds.FirstOrDefault(it => it.fixture.fixture_id == eachfixture.fixture_id);
                    if (OddsMatch != null)
                    {
                        double perHome = 0, perDraw = 0, perAway = 0;
                        string OddsMaker = "";
                        Value  oddHome   = new Value();
                        Value  oddDraw   = new Value();
                        Value  oddAway   = new Value();

                        foreach (var eachOdds in OddsMatch.bookmakers)
                        {
                            oddHome = eachOdds.bets[0].values.FirstOrDefault(it2 => it2.value == "Home");
                            oddDraw = eachOdds.bets[0].values.FirstOrDefault(it2 => it2.value == "Draw");
                            oddAway = eachOdds.bets[0].values.FirstOrDefault(it2 => it2.value == "Away");
                            if (oddHome != null && oddDraw != null && oddAway != null)
                            {
                                OddsMaker = eachOdds.bookmaker_name;
                                perHome   = (Convert.ToDouble(oddAway.odd) * 100) / (Convert.ToDouble(oddHome.odd) + Convert.ToDouble(oddDraw.odd) + Convert.ToDouble(oddAway.odd));
                                perDraw   = (Convert.ToDouble(oddDraw.odd) * 100) / (Convert.ToDouble(oddHome.odd) + Convert.ToDouble(oddDraw.odd) + Convert.ToDouble(oddAway.odd));
                                perAway   = (Convert.ToDouble(oddHome.odd) * 100) / (Convert.ToDouble(oddHome.odd) + Convert.ToDouble(oddDraw.odd) + Convert.ToDouble(oddAway.odd));
                                break;
                            }
                        }

                        var item = new LeagueOddsFixture
                        {
                            LeagueID      = eachfixture.league_id.ToString(),
                            LeagueName    = eachfixture.league.name,
                            LeagueCountry = eachfixture.league.country,
                            LeagueLogo    = eachfixture.league.logo,
                            LeagueFlag    = eachfixture.league.flag,
                            EventDate     = eachfixture.event_date,
                            MatchStatus   = eachfixture.status,
                            HometeamName  = eachfixture.homeTeam.team_name,
                            HometeamLogo  = eachfixture.homeTeam.logo,
                            HometeamScore = eachfixture.goalsHomeTeam,
                            AwayteamName  = eachfixture.awayTeam.team_name,
                            AwayteamLogo  = eachfixture.awayTeam.logo,
                            AwayteamScore = eachfixture.goalsAwayTeam,
                            OddsBookmaker = OddsMaker,
                            OddsLabal     = OddsMatch.bookmakers[0].bets[0].label_name,
                            OddsHome      = oddHome.odd,
                            OddsDraw      = oddDraw.odd,
                            OddsAway      = oddAway.odd,
                            PerHome       = perHome.ToString(),
                            PerDraw       = perDraw.ToString(),
                            PerAway       = perAway.ToString()
                        };
                        ListLeagueFixOdds.Add(item);
                    }
                }
                //Add into Return List
                var item2 = new List_LeagueOddsFixture
                {
                    LeagueID          = ListLeagueFixOdds[0].LeagueID,
                    LeagueName        = ListLeagueFixOdds[0].LeagueName,
                    LeagueFlag        = ListLeagueFixOdds[0].LeagueFlag,
                    LeagueCountry     = ListLeagueFixOdds[0].LeagueCountry,
                    LeagueOddsFixture = ListLeagueFixOdds.OrderBy(c => c.EventDate).ToList()
                };
                Ret_LeagureOddsFixture.Add(item2);
            }
            return(Ret_LeagureOddsFixture);
        }
コード例 #2
0
        private void GetData_LeagueOdds()
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US"));
            double perHome = 0, perDraw = 0, perAway = 0;
            string OddsMaker = "", JsonStr_Fixture = "", JsonStr_Odds = "";
            List <List_LeagueOddsFixture> Ret_LeagureOddsFixture = new List <List_LeagueOddsFixture>();

            //Get Json object Fixture
            JsonStr_Fixture = GetAllFixtureThisDay(date);
            List <RootObjectFixture> APIJsonObjectFixture = JsonConvert.DeserializeObject <List <RootObjectFixture> >(JsonStr_Fixture);

            if (APIJsonObjectFixture[0].api.fixtures.Count > 0)
            {
                //Group by leagueID in all Fixture
                var groupLeagueID = from lstfixture in APIJsonObjectFixture[0].api.fixtures
                                    group lstfixture by lstfixture.league_id into newGroup
                                    orderby newGroup.Key
                                    select newGroup.Key.ToString();

                foreach (string ID in groupLeagueID)
                {
                    List <LeagueOddsFixture> ListLeagueFixOdds = new List <LeagueOddsFixture>();

                    //Get Json object Odds
                    // List<RootObjectOdds> APIJsonObjectOdds = new List<RootObjectOdds>();
                    // APIJsonObjectOdds.AddRange(GetObjectOddLeagueIDAllPaging(ID));
                    JsonStr_Odds = GetObjectOddLeagueIDAllPaging(ID);
                    List <RootObjectOdds> APIJsonObjectOdds = JsonConvert.DeserializeObject <List <RootObjectOdds> >(JsonStr_Odds);

                    if (APIJsonObjectOdds[0].api.odds.Count > 0)
                    {
                        foreach (var eachLstOdds in APIJsonObjectOdds[0].api.odds)
                        {
                            //หาตัวที่มี FixtureID เดียวกัน
                            var FixtureMatch = APIJsonObjectFixture[0].api.fixtures.FirstOrDefault(it => it.fixture_id == eachLstOdds.fixture.fixture_id);
                            if (FixtureMatch != null)
                            {
                                perHome   = 0;
                                perDraw   = 0;
                                perAway   = 0;
                                OddsMaker = "";
                                Value oddHome = new Value();
                                Value oddDraw = new Value();
                                Value oddAway = new Value();

                                foreach (var eachOdds in eachLstOdds.bookmakers)
                                {
                                    oddHome = eachOdds.bets[0].values.FirstOrDefault(it2 => it2.value == "Home");
                                    oddDraw = eachOdds.bets[0].values.FirstOrDefault(it2 => it2.value == "Draw");
                                    oddAway = eachOdds.bets[0].values.FirstOrDefault(it2 => it2.value == "Away");
                                    if (oddHome != null && oddDraw != null && oddAway != null)
                                    {
                                        OddsMaker = eachOdds.bookmaker_name;
                                        perHome   = (Convert.ToDouble(oddAway.odd) * 100) / (Convert.ToDouble(oddHome.odd) + Convert.ToDouble(oddDraw.odd) + Convert.ToDouble(oddAway.odd));
                                        perDraw   = (Convert.ToDouble(oddDraw.odd) * 100) / (Convert.ToDouble(oddHome.odd) + Convert.ToDouble(oddDraw.odd) + Convert.ToDouble(oddAway.odd));
                                        perAway   = (Convert.ToDouble(oddHome.odd) * 100) / (Convert.ToDouble(oddHome.odd) + Convert.ToDouble(oddDraw.odd) + Convert.ToDouble(oddAway.odd));
                                        break;
                                    }
                                }

                                var item = new LeagueOddsFixture
                                {
                                    LeagueID      = FixtureMatch.league_id.ToString(),
                                    LeagueName    = FixtureMatch.league.name,
                                    LeagueCountry = FixtureMatch.league.country,
                                    LeagueLogo    = FixtureMatch.league.logo,
                                    LeagueFlag    = FixtureMatch.league.flag,
                                    EventDate     = FixtureMatch.event_date,
                                    MatchStatus   = FixtureMatch.status,
                                    HometeamName  = FixtureMatch.homeTeam.team_name,
                                    HometeamLogo  = FixtureMatch.homeTeam.logo,
                                    HometeamScore = FixtureMatch.goalsHomeTeam,
                                    AwayteamName  = FixtureMatch.awayTeam.team_name,
                                    AwayteamLogo  = FixtureMatch.awayTeam.logo,
                                    AwayteamScore = FixtureMatch.goalsAwayTeam,
                                    OddsBookmaker = OddsMaker,
                                    OddsLabal     = eachLstOdds.bookmakers[0].bets[0].label_name,
                                    OddsHome      = oddHome.odd,
                                    OddsDraw      = oddDraw.odd,
                                    OddsAway      = oddAway.odd,
                                    PerHome       = string.Format("{0:00.############}", perHome),
                                    PerDraw       = string.Format("{0:00.############}", perDraw),
                                    PerAway       = string.Format("{0:00.############}", perAway)
                                };
                                if (FixtureMatch.status.ToLower().IndexOf("postponed") < 0)
                                {
                                    ListLeagueFixOdds.Add(item);
                                }
                            }
                        }
                        if (ListLeagueFixOdds.Count > 0)
                        {
                            //Add into Return List
                            var item2 = new List_LeagueOddsFixture
                            {
                                LeagueID          = ListLeagueFixOdds[0].LeagueID,
                                LeagueName        = ListLeagueFixOdds[0].LeagueName,
                                LeagueFlag        = ListLeagueFixOdds[0].LeagueFlag,
                                LeagueCountry     = ListLeagueFixOdds[0].LeagueCountry,
                                LeagueOddsFixture = ListLeagueFixOdds.OrderBy(c => c.EventDate).ToList()
                            };
                            Ret_LeagureOddsFixture.Add(item2);
                        }
                    }
                }
            }
            // string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(Ret_LeagureOddsFixture);
            // FileStream fs_fixture = new FileStream(date + ".txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            // StreamWriter textWriter_fixture = new StreamWriter(fs_fixture);
            // textWriter_fixture.WriteLine(jsonString);
            // textWriter_fixture.Close();
            // fs_fixture.Close();

            Temp_List_LeagueOddsFixture = Ret_LeagureOddsFixture;
        }