Exemple #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            //ServiceBase[] ServicesToRun;
            //ServicesToRun = new ServiceBase[]
            //{
            //    new Service1()
            //};
            //ServiceBase.Run(ServicesToRun);

            //This Method is used to store Master Draw (Pool List) into Sheet
            //PoolHandler.GetPoolData();

            //This method is used to store Field Metrix in sheet
            PoolHandler.GetFieldMatrix();
        }
        public static void GetPoolData()
        {
            string  response  = string.Empty;
            Boolean header    = true;
            string  sheetName = "Sheet_17_04_2018_12_25";

            var service = CreateGoogleSheets(sheetName);

            List <KeyValuePair <string, string> > competitionList = new List <KeyValuePair <string, string> >();

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://websites.sportstg.com/comp_info.cgi?round=1&a=ROUND&client=0-11971-0-487287-0&pool=1");
                webRequest.Method    = "GET";
                webRequest.Host      = webRequest.RequestUri.Host;
                webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";

                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                StreamReader    stream      = new StreamReader(webResponse.GetResponseStream());
                response = stream.ReadToEnd();
                stream.Dispose();

                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(response);

                HtmlNodeCollection competitionNodeList = htmlDoc.DocumentNode.SelectNodes("//select[@id='compselectbox']");

                if (competitionNodeList != null)
                {
                    if (competitionNodeList.Count() > 0)
                    {
                        for (int x = 0; x < competitionNodeList.Count(); x++)
                        {
                            HtmlNodeCollection subCompetitionNodeList = competitionNodeList[x].SelectNodes(".//option");

                            for (int y = 0; y < subCompetitionNodeList.Count(); y++)
                            {
                                string competitionName = subCompetitionNodeList[y].InnerText.Replace("&nbsp;", "").Trim().ToString();
                                string competitionCode = subCompetitionNodeList[y].Attributes["value"].Value.Trim().ToString();
                                if (competitionCode != "")
                                {
                                    string teamUrl = "http://websites.sportstg.com/comp_info.cgi?c=" + competitionCode + "&a=ROUND#";
                                    competitionList.Add(new KeyValuePair <string, string>(competitionName, teamUrl));
                                }
                            }
                        }
                    }
                }

                if (competitionList.Count() > 0)
                {
                    foreach (var item in competitionList)
                    {
                        if (item.Value != "")
                        {
                            List <KeyValuePair <string, string> > poolList  = new List <KeyValuePair <string, string> >();
                            List <KeyValuePair <string, string> > roundList = new List <KeyValuePair <string, string> >();
                            string poolResponse = PoolHandler.GetResponseFromUrl(item.Value);

                            HtmlAgilityPack.HtmlDocument htmlnewDoc = new HtmlAgilityPack.HtmlDocument();
                            htmlnewDoc.LoadHtml(poolResponse);

                            HtmlNodeCollection htmlPools = htmlnewDoc.DocumentNode.SelectNodes("//div[@class='fixoptions']//div[@class='nonactpool-wrap']//a");

                            #region Retrive Pools List
                            if (htmlPools != null)
                            {
                                if (htmlPools.Count() > 0)
                                {
                                    for (int j = 0; j < htmlPools.Count(); j++)
                                    {
                                        string poolTitle = htmlPools[j].InnerText.Trim().ToString();
                                        string poolUrl   = htmlPools[j].Attributes["href"].Value.ToString();
                                        if (poolTitle != "Final" && poolUrl != "#")
                                        {
                                            poolList.Add(new KeyValuePair <string, string>(poolTitle, "http://websites.sportstg.com/" + poolUrl.Replace("amp;", "").Trim().ToString()));
                                        }
                                    }
                                }
                            }
                            #endregion

                            if (poolList.Count() > 0)
                            {
                                foreach (var poolItem in poolList)
                                {
                                    string pool_Name = poolItem.Key;
                                    if (!string.IsNullOrEmpty(poolItem.Value))
                                    {
                                        string subPoolResponse = PoolHandler.GetResponseFromUrl(poolItem.Value);

                                        HtmlAgilityPack.HtmlDocument htmlnewDocA = new HtmlAgilityPack.HtmlDocument();
                                        htmlnewDocA.LoadHtml(subPoolResponse);

                                        HtmlNodeCollection currentRoundNode = htmlnewDocA.DocumentNode.SelectNodes("//div[@class='roundlist']//span[@data-rd]");
                                        HtmlNodeCollection roundsNodes      = htmlnewDocA.DocumentNode.SelectNodes("//div[@class='roundlist']//a[@data-rd]");

                                        if (currentRoundNode != null)
                                        {
                                            if (currentRoundNode.Count() > 0)
                                            {
                                                string roundValue = currentRoundNode[0].Attributes["data-rd"].Value.Trim().ToString();
                                                string roundUrl   = poolItem.Value;
                                                if (roundValue != "")
                                                {
                                                    roundList.Add(new KeyValuePair <string, string>("Round " + roundValue, roundUrl.Replace("round=0", "round=" + roundValue).Replace("action=ROUND", "a=ROUND")));
                                                }
                                            }
                                        }

                                        if (roundsNodes != null)
                                        {
                                            if (roundsNodes.Count() > 0)
                                            {
                                                for (int rndCount = 0; rndCount < roundsNodes.Count(); rndCount++)
                                                {
                                                    if (roundsNodes[rndCount].Attributes.Contains("data-rd"))
                                                    {
                                                        string roundValue = roundsNodes[rndCount].Attributes["data-rd"].Value.Trim().ToString();
                                                        string roundUrl   = roundsNodes[rndCount].Attributes["href"].Value.Trim().Replace("amp;", "").Trim().ToString();
                                                        if (!string.IsNullOrEmpty(roundValue))
                                                        {
                                                            roundList.Add(new KeyValuePair <string, string>("Round " + roundValue, "http://websites.sportstg.com/" + roundUrl));
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        if (roundList.Count > 0)
                                        {
                                            foreach (var roundItem in roundList.OrderBy(x => x.Key))
                                            {
                                                string roundResponse = PoolHandler.GetResponseFromUrl(roundItem.Value);

                                                int startIndex = roundResponse.IndexOf("var matches =");
                                                int endIndex   = roundResponse.LastIndexOf("];</script>");

                                                if (startIndex > 0 && endIndex > 0)
                                                {
                                                    string jsonString = roundResponse.Substring(startIndex, endIndex - startIndex + 5);
                                                    jsonString = jsonString.Replace("var matches =", "").Replace(";</s", "").Trim().ToString();

                                                    if (!string.IsNullOrEmpty(jsonString))
                                                    {
                                                        List <RootObject>       teamsList  = new List <RootObject>();
                                                        List <MatchMasterModel> masterList = new List <MatchMasterModel>();
                                                        teamsList = JsonConvert.DeserializeObject <List <RootObject> >(jsonString);

                                                        if (teamsList.Count() > 0)
                                                        {
                                                            masterList = teamsList.Select(x => new MatchMasterModel()
                                                            {
                                                                matchDate    = x.TimeDateRaw,
                                                                roundno      = x.Round,
                                                                fieldno      = x.VenueName.Replace("Field", "").Replace("&nbsp;", "").Trim().ToString(),
                                                                divisionName = x.CompName,
                                                                poolName     = pool_Name.Replace("Pool", "").Trim().ToString(),
                                                                teamA        = x.HomeName,
                                                                versus       = "V",
                                                                teamB        = x.AwayName,
                                                                halveA       = string.Empty,
                                                                halveB       = string.Empty
                                                            }).ToList();

                                                            List <IList <Object> > objNewRecords = new List <IList <Object> >();

                                                            IList <Object> obj = new List <Object>();

                                                            if (header == true)
                                                            {
                                                                obj.Add("Time");
                                                                obj.Add("Round");
                                                                obj.Add("Field");
                                                                obj.Add("Division");
                                                                obj.Add("Pool");
                                                                obj.Add("Team");
                                                                obj.Add("v");
                                                                obj.Add("Team");
                                                                obj.Add("Halve 1");
                                                                obj.Add("Halve 2");
                                                                objNewRecords.Add(obj);
                                                            }
                                                            foreach (var a in masterList)
                                                            {
                                                                obj = new List <Object>();
                                                                obj = GenerateData(a, service);
                                                                if (obj != null)
                                                                {
                                                                    objNewRecords.Add(obj);
                                                                }
                                                            }

                                                            string newRange = GetRange(service, sheetName);

                                                            AppendGoogleSheetinBatch(objNewRecords, spreadSheetId, newRange, service);

                                                            if (header == true)
                                                            {
                                                                //The formatHeaderField method will format the header as user wants to.
                                                                formatHeaderField(service, sheetName);
                                                                header = false;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }