Example #1
0
        public static List <FootballGameBM> ReadGamesFromFile()
        {
            List <FootballGameBM> allGames = new List <FootballGameBM>();
            string input;

            using (StreamReader reader = new StreamReader("../FootballAnalyzes.UpdateDatabase/all_games.txt"))
            {
                input = reader.ReadToEnd();
            }

            string[] allGamesFromFile = input.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var game in allGamesFromFile)
            {
                string[] gameParams = game.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                TeamBM homeTeamBM = new TeamBM()
                {
                    Name       = gameParams[6],
                    UniqueName = gameParams[7]
                };

                // Add away team in DB if it doesn't exist
                TeamBM awayTeamBM = new TeamBM()
                {
                    Name       = gameParams[8],
                    UniqueName = gameParams[9]
                };

                // Add current league in DB if it doesn't exist
                LeagueBM leagueBM = new LeagueBM()
                {
                    Country = gameParams[2],
                    Name    = gameParams[3].Substring(0, gameParams[3].LastIndexOf('-')),
                    Year    = gameParams[3].Substring(gameParams[3].LastIndexOf('-') + 1),
                    Stage   = gameParams[4],
                    Type    = gameParams[5]
                };

                string time      = gameParams[1] == "33:33" ? "00:59" : gameParams[1];
                var    matchDate = DateTime.ParseExact(gameParams[0] + time, "yyyyMMddHH:mm", CultureInfo.InvariantCulture);


                // Add full time result to DB and to the game
                var fullTimeResult = new GameResultBM()
                {
                    Result        = (ResultEnum)Enum.Parse(typeof(ResultEnum), gameParams[10], true),
                    HomeTeamGoals = int.Parse(gameParams[11]),
                    AwayTeamGoals = int.Parse(gameParams[12])
                };

                GameResultBM halfTimeResult = null;
                // Add first half result to DB and to the game IF EXCIST
                if (gameParams[14] != "-1" && gameParams[15] != "-1")
                {
                    halfTimeResult = new GameResultBM
                    {
                        Result        = (ResultEnum)Enum.Parse(typeof(ResultEnum), gameParams[13], true),
                        HomeTeamGoals = int.Parse(gameParams[14]),
                        AwayTeamGoals = int.Parse(gameParams[15])
                    };
                }

                GameStatisticBM gameStatistic = null;
                if (gameParams[16] != "-1" && gameParams[17] != "-1")
                {
                    gameStatistic = new GameStatisticBM()
                    {
                        HomeTeamCorners       = int.Parse(gameParams[16]),
                        AwayTeamCorners       = int.Parse(gameParams[17]),
                        HomeTeamShotsOnTarget = int.Parse(gameParams[18]),
                        AwayTeamShotsOnTarget = int.Parse(gameParams[19]),
                        HomeTeamShotsWide     = int.Parse(gameParams[20]),
                        AwayTeamShotsWide     = int.Parse(gameParams[21]),
                        HomeTeamFouls         = int.Parse(gameParams[22]),
                        AwayTeamFouls         = int.Parse(gameParams[23]),
                        HomeTeamOffsides      = int.Parse(gameParams[24]),
                        AwayTeamOffsides      = int.Parse(gameParams[25])
                    };
                }

                // Add game to DB
                FootballGameBM currentGame = new FootballGameBM
                {
                    MatchDate       = matchDate,
                    League          = leagueBM,
                    HomeTeam        = homeTeamBM,
                    AwayTeam        = awayTeamBM,
                    FullTimeResult  = fullTimeResult,
                    FirstHalfResult = halfTimeResult,
                    GameStatistic   = gameStatistic
                };

                allGames.Add(currentGame);
            }

            return(allGames);
        }
        private static void ReadGameInfo(string gameLink, DateTime currentDate)
        {
            try
            {
                string requestUri = $"http://int.soccerway.com{gameLink}/";

                var request = WebRequest.Create(requestUri);
                request.ContentType = "application/json; charset=utf-8";
                string text = "";

                Stopwatch responseTime = new Stopwatch();
                responseTime.Start();
                var response = (HttpWebResponse)request.GetResponse();
                responseTime.Stop();

                Console.WriteLine($"\t{counter}: {gameLink} - {responseTime.Elapsed}");
                counter++;

                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    text = sr.ReadToEnd();
                }


                // Parse both team
                var pattern = "class=\"thick\">\\s+<a\\shref=\"(?'TeamLink'[^\"]+)\">(?'TeamName'[^<]+)";

                Regex           rgx     = new Regex(pattern);
                MatchCollection matches = rgx.Matches(text);

                string homeTeamName       = matches[0].Groups["TeamName"].Value.Trim();
                string homeTeamUniqueName = matches[0].Groups["TeamLink"].Value.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[2];
                TeamBM homeTeam           = new TeamBM
                {
                    UniqueName = homeTeamUniqueName,
                    Name       = homeTeamName
                };

                string awayTeamName       = matches[1].Groups["TeamName"].Value.Trim();
                string awayTeamUniqueName = matches[1].Groups["TeamLink"].Value.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[2];
                TeamBM awayTeam           = new TeamBM
                {
                    UniqueName = awayTeamUniqueName,
                    Name       = awayTeamName
                };

                // Parse league
                pattern = "<dt>Competition<\\/dt>\\s+<dd><a\\shref=\"(?'LeagueLink'[^\"]+)";
                rgx     = new Regex(pattern);
                Match    leagueMatch   = rgx.Match(text);
                string[] leagueData    = leagueMatch.Groups["LeagueLink"].Value.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string   leagueType    = leagueData[0];
                string   leagueCountry = leagueData[1];
                string   leagueName    = leagueData[2];
                string   leagueYear    = leagueData[3];
                string   leagueStage   = leagueData[4];

                LeagueBM league = new LeagueBM
                {
                    Type    = leagueType,
                    Country = leagueCountry,
                    Name    = leagueName,
                    Year    = leagueYear,
                    Stage   = leagueStage
                };

                // Pars kick-off
                pattern = "<dt>Kick-off<\\/dt>\\s+<dd>\\s+<[^>]+>(?'KickOff'[^<]+)";
                rgx     = new Regex(pattern);
                Match  kickOffMatch = rgx.Match(text);
                string kickOff      = kickOffMatch.Groups["KickOff"].Value.Trim() != "" ? kickOffMatch.Groups["KickOff"].Value.Trim() : "00:59";
                currentDate = currentDate.AddHours(int.Parse(kickOff.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0]));
                currentDate = currentDate.AddMinutes(int.Parse(kickOff.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1]));


                // Create football game
                FootballGameBM game = new FootballGameBM
                {
                    League    = league,
                    MatchDate = currentDate,
                    HomeTeam  = homeTeam,
                    AwayTeam  = awayTeam
                };

                Console.WriteLine($"\t\t{game}");

                // Add game in collection
                allGames.Add(game);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                errors.Add($"{gameLink}\n{e}");
            }
        }
Example #3
0
        private static void ReadGameStatistic(string gameLink, DateTime currentDate)
        {
            try
            {
                string requestUri = $"http://int.soccerway.com{gameLink}/";

                var request = WebRequest.Create(requestUri);
                request.ContentType = "application/json; charset=utf-8";
                string text = "";

                Stopwatch responseTime = new Stopwatch();
                responseTime.Start();
                var response = (HttpWebResponse)request.GetResponse();
                responseTime.Stop();

                Console.WriteLine($"\t{counter}: {gameLink} - {responseTime.Elapsed}");
                counter++;

                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    text = sr.ReadToEnd();
                }


                // Parse both team
                var pattern = "class=\"thick\">\\s+<a\\shref=\"(?'TeamLink'[^\"]+)\">(?'TeamName'[^<]+)";

                Regex           rgx     = new Regex(pattern);
                MatchCollection matches = rgx.Matches(text);

                string homeTeamName       = matches[0].Groups["TeamName"].Value.Trim();
                string homeTeamUniqueName = matches[0].Groups["TeamLink"].Value.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[2];
                TeamBM homeTeam           = new TeamBM
                {
                    UniqueName = homeTeamUniqueName,
                    Name       = homeTeamName
                };

                string awayTeamName       = matches[1].Groups["TeamName"].Value.Trim();
                string awayTeamUniqueName = matches[1].Groups["TeamLink"].Value.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[2];
                TeamBM awayTeam           = new TeamBM
                {
                    UniqueName = awayTeamUniqueName,
                    Name       = awayTeamName
                };

                // Parse league
                pattern = "<dt>Competition<\\/dt>\\s+<dd><a\\shref=\"(?'LeagueLink'[^\"]+)";
                rgx     = new Regex(pattern);
                Match    leagueMatch   = rgx.Match(text);
                string[] leagueData    = leagueMatch.Groups["LeagueLink"].Value.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string   leagueType    = leagueData[0];
                string   leagueCountry = leagueData[1];
                string   leagueName    = leagueData[2];
                string   leagueYear    = leagueData[3];
                string   leagueStage   = leagueData[4];

                LeagueBM league = new LeagueBM
                {
                    Type    = leagueType,
                    Country = leagueCountry,
                    Name    = leagueName,
                    Year    = leagueYear,
                    Stage   = leagueStage
                };

                // Pars kick-off
                pattern = "<dt>Kick-off<\\/dt>\\s+<dd>\\s+<[^>]+>(?'KickOff'[^<]+)";
                rgx     = new Regex(pattern);
                Match    kickOffMatch = rgx.Match(text);
                string   kickOff      = kickOffMatch.Groups["KickOff"].Value.Trim() != "" ? kickOffMatch.Groups["KickOff"].Value.Trim() : "00:59";
                string   dateAsString = currentDate.ToString("yyyyMMdd");
                DateTime matchDate    = DateTime.ParseExact(dateAsString + kickOff, "yyyyMMddHH:mm", CultureInfo.InvariantCulture);

                // Parse full time goals
                pattern = "<dt>Full-time<\\/dt>\\s+<dd>(?'HomeTeamFullTimeGoals'\\d+)\\s+-\\s*(?'AwayTeamFullTimeGoals'\\d+)";
                rgx     = new Regex(pattern);
                Match        goalsMatch            = rgx.Match(text);
                int          homeTeamFullTimeGoals = int.Parse(goalsMatch.Groups["HomeTeamFullTimeGoals"].Value.Trim());
                int          awayTeamFullTimeGoals = int.Parse(goalsMatch.Groups["AwayTeamFullTimeGoals"].Value.Trim());
                ResultEnum   fullTimeResult        = homeTeamFullTimeGoals > awayTeamFullTimeGoals ? ResultEnum.H : (homeTeamFullTimeGoals < awayTeamFullTimeGoals ? ResultEnum.A : ResultEnum.D);
                GameResultBM fullTimeGameResult    = new GameResultBM
                {
                    Result        = fullTimeResult,
                    HomeTeamGoals = homeTeamFullTimeGoals,
                    AwayTeamGoals = awayTeamFullTimeGoals
                };

                // It's not sure we have half-time stats
                pattern    = "<dt>Half-time<\\/dt>\\s+<dd>(?'HomeTeamHalfTimeGoals'\\d+)\\s+-\\s*(?'AwayTeamHalfTimeGoals'\\d+)<\\/dd>\\s+";
                rgx        = new Regex(pattern);
                goalsMatch = rgx.Match(text);
                GameResultBM halfTimeGameResult = null;

                if (goalsMatch.Groups.Count == 3)
                {
                    int        homeTeamHalfTimeGoals = int.Parse(goalsMatch.Groups["HomeTeamHalfTimeGoals"].Value.Trim());
                    int        awayTeamHalfTimeGoals = int.Parse(goalsMatch.Groups["AwayTeamHalfTimeGoals"].Value.Trim());
                    ResultEnum halfTimeResult        = homeTeamHalfTimeGoals > awayTeamHalfTimeGoals ? ResultEnum.H : (homeTeamHalfTimeGoals < awayTeamHalfTimeGoals ? ResultEnum.A : ResultEnum.D);

                    halfTimeGameResult = new GameResultBM
                    {
                        Result        = halfTimeResult,
                        HomeTeamGoals = homeTeamHalfTimeGoals,
                        AwayTeamGoals = awayTeamHalfTimeGoals
                    };
                }

                // Create football game
                FootballGameBM game = new FootballGameBM
                {
                    League          = league,
                    MatchDate       = matchDate,
                    HomeTeam        = homeTeam,
                    AwayTeam        = awayTeam,
                    FullTimeResult  = fullTimeGameResult,
                    FirstHalfResult = halfTimeGameResult
                };

                // Parse game statistics
                GameStatisticBM stats = ExtractGameStatistics(text);
                game.GameStatistic = stats;

                Console.WriteLine($"\t\t{game}");

                // Add game in collection
                allGames.Add(game);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                errors.Add($"{gameLink}\n{e}");
            }
        }