Пример #1
0
        static void Main(string[] args)
        {
            ILoggerFactory loggerFactory = new LoggerFactory()
                                           .AddConsole()
                                           .AddDebug();
            ILogger logger = loggerFactory.CreateLogger <Program>();

            MySportsFeeds.Username = "******";
            MySportsFeeds.Password = "******";

            //Task<string> getStats = MySportsFeeds.FetchFeedAsync("nfl", "2016-2017-regular", "full_game_schedule");
            //string result = getStats.Result;
            //logger.LogInformation("This is a message");
            //var sched = JsonConvert.DeserializeObject<FullGameSchedule>(result);
            //GameSchedule gameSchedule = sched.fullgameschedule;
            //List<GameEntry> gameentry = gameSchedule.gameentry;
            //GameEntry entry1 = gameentry[0];
            //logger.LogInformation("stats: {0}", JsonConvert.SerializeObject(entry1));

            Task <string> getStats = MySportsFeeds.FetchFeedAsync("nfl", "2016-regular", "active_players");
            string        result   = getStats.Result;
            //logger.LogInformation("This is a message");
            var players = JsonConvert.DeserializeObject <ActivePlayers>(result);
            ActivePlayerList   playerList  = players.activeplayers;
            List <Playerentry> playerentry = playerList.playerentry;
            Playerentry        entry1      = playerentry[0];

            logger.LogInformation("player1: {0}", JsonConvert.SerializeObject(entry1));

            //GameSchedule gameSchedule = sched.fullgameschedule;
            //List<GameEntry> gameentry = gameSchedule.gameentry;
            //GameEntry entry1 = gameentry[0];
            //logger.LogInformation("stats: {0}", JsonConvert.SerializeObject(entry1));

            //Dictionary<string, string> Params = new Dictionary<string, string>();
            //Params.Add("fordate", "20161118");
            //Params.Add("player", "7861");
            //Task<string> getPlayerStats = MySportsFeeds.FetchFeedAsync("nfl", "2016-regular", "cumulative_player_stats");
            //string result = getPlayerStats.Result;
            //logger.LogInformation("result: {0}", result);
            //var sched = JsonConvert.DeserializeObject<CumulativePlayerStats>(result);
            //logger.LogInformation("stats: {0}", JsonConvert.SerializeObject(sched));
        }
Пример #2
0
        /// <summary>
        /// get "cumulative_player_stats.json" from MySportsFeeds NHL API and return it parse into entity list
        /// </summary>
        /// <returns></returns>
        public List <PlayoffPlayerInfoEntity> GetData()
        {
            var playerInfoEntities = new List <PlayoffPlayerInfoEntity>();

            MySportsFeeds apiResults = null;

            var request = new HttpClient();

            // possibly this 1st one
            request.BaseAddress = new Uri("https://www.mysportsfeeds.com/api/feed/pull/nhl/2017-playoff/");
            request.DefaultRequestHeaders.Accept.Clear();
            request.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                        "bmlja2dvb2Ryb3c6R29kcm81NQ=="
                                                                                        //Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{user}:{password}"))
                                                                                        );
            request.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                using (
                    //possibly this one instead..
                    var response = request.GetAsync("cumulative_player_stats.json?playerstats=G,A,Pts,PPPts,SHPts,GWG").ContinueWith((taskWithResponse) =>
                                                                                                                                     //var response = request.GetAsync("cumulative_player_stats.json?").ContinueWith((taskWithResponse) =>
                {
                    if (taskWithResponse != null)
                    {
                        if (taskWithResponse.Status != TaskStatus.RanToCompletion)
                        {
                            throw new Exception(
                                $"Server error (HTTP {taskWithResponse.Result?.StatusCode}: {taskWithResponse.Exception?.InnerException} : {taskWithResponse.Exception?.Message}).");
                        }
                        if (taskWithResponse.Result.IsSuccessStatusCode)
                        {
                            var jsonString = taskWithResponse.Result.Content.ReadAsStringAsync();
                            jsonString.Wait();
                            var data = JsonConvert.DeserializeObject <MySportsFeeds>(jsonString.Result);
                            apiResults = data;
                        }
                        else
                        {
                            var e = new Exception($"{taskWithResponse.Result.StatusCode} : {taskWithResponse.Result.ReasonPhrase}");
                            LogError.Write(e,
                                           "issue while retriving data from MySportsFeed -> GetData()");
                            throw e;
                        }
                    }
                }))
                {
                    response.Wait();

                    var resultCompleted = apiResults;

                    var listTeam = resultCompleted.cumulativeplayerstats.playerstatsentry;

                    if (listTeam.Length <= 0)
                    {
                        throw new ApiException("No data return from API");
                    }

                    var id = 0;
                    try
                    {
                        foreach (var playerstatsentry in listTeam)
                        {
                            id = Int32.Parse(playerstatsentry.player.ID);
                            var team     = playerstatsentry.team.Abbreviation;
                            var pos      = playerstatsentry.player.Position[0].ToString(); // only 1st char of string, e.g. LW => L
                            var name     = playerstatsentry.player.FirstName + " " + playerstatsentry.player.LastName;
                            var game     = Int32.Parse(playerstatsentry.stats.GamesPlayed.text);
                            var goal     = Int32.Parse(playerstatsentry.stats.stats.Goals.text);
                            var assist   = Int32.Parse(playerstatsentry.stats.stats.Assists.text);
                            var pts      = Int32.Parse(playerstatsentry.stats.stats.Points.text);
                            var ppp      = Int32.Parse(playerstatsentry.stats.stats.PowerplayPoints.text);
                            var shp      = Int32.Parse(playerstatsentry.stats.stats.ShorthandedPoints.text);
                            var gwg      = Int32.Parse(playerstatsentry.stats.stats.GameWinningGoals.text);
                            var isRookie = Boolean.Parse(playerstatsentry.player.IsRookie);

                            var playerInfo = new PlayoffPlayerInfoEntity(id, team, pos, name, game, goal, assist, pts, ppp, shp, gwg, isRookie);

                            playerInfoEntities.Add(playerInfo);
                        }

                        return(playerInfoEntities.OrderBy(x => x.C_Team).ToList());
                    }
                    catch (Exception e)
                    {
                        LogError.Write(e, $"Issue at this player id (if id is 0 means it happened on the 1st item.): ID :  {id}");
                    }
                }
            }
            catch (Exception ex)
            {
                LogError.Write(ex, "issue while retriving data from MySportsFeed -> GetData()");
            }
            return(playerInfoEntities);
        }