Exemple #1
0
        public static string PageSpeed()
        {
            string[] websites = { "https://www.hdfcergo.com", "https://www.reliancegeneral.co.in", "https://www.godigit.com", "https://www.icicilombard.com", "https://www.acko.com", "https://www.policybazaar.com" };
            //string[] websites = { "https://www.hdfcergo.com", "https://www.reliancegeneral.co.in", "https://www.policybazaar.com" };
            //string[] websites = { "https://www.hdfcergo.com/health-insurance/family-health-insurance" };
            string[] type = { "mobile", "desktop" };

            PageSpeedEntityList    response = new PageSpeedEntityList();
            List <PageSpeedEntity> PSEList  = new List <PageSpeedEntity>();

            for (int i = 0; i < websites.Length; i++)
            {
                int j;
                for (j = 0; j < type.Length; j++)
                {
                    using (WebClient wc = new WebClient())
                    {
                        try
                        {
                            PageSpeedEntity PSE = new PageSpeedEntity();
                            string          url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + websites[i].ToString() + "&strategy=" + type[j].ToString() + "&category=performance&apiKey";
                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine("Analysing Page Speed => " + type[j].ToString() + " => " + websites[i].ToString());
                            MailerPageSpeed.WriteToFile("Analysing Page Speed => " + type[j].ToString() + " => " + websites[i].ToString());
                            var     json = wc.DownloadString(url);
                            dynamic d    = JsonConvert.DeserializeObject(json);
                            PSE.FetchTime               = DateTime.Now.ToShortTimeString();
                            PSE.Webiste                 = websites[i].ToString();
                            PSE.Type                    = type[j].ToString();
                            PSE.PageScore               = d.lighthouseResult.categories.performance.score.Value * 100;
                            PSE.numberResources         = Convert.ToString(d.lighthouseResult.audits["resource-summary"].details.items.Count);
                            PSE.totalRequestBytes       = Convert.ToDouble(d.lighthouseResult.audits["resource-summary"].details.items[0].transferSize.Value / 1024) / 1024;
                            PSE.HTMLResponseTime        = Convert.ToDouble(d.lighthouseResult.audits["server-response-time"].numericValue.Value);
                            PSE.HTMLResponseTime        = TimeSpan.FromMilliseconds(PSE.HTMLResponseTime).TotalSeconds;
                            PSE.imageResponseBytes      = Convert.ToDouble(d.lighthouseResult.audits["resource-summary"].details.items[1].transferSize.Value / 1024) / 1024;
                            PSE.javascriptResponseBytes = Convert.ToDouble(d.lighthouseResult.audits["resource-summary"].details.items[2].transferSize.Value / 1024) / 1024;
                            PSE.cssResponseBytes        = Convert.ToDouble(d.lighthouseResult.audits["resource-summary"].details.items[3].transferSize.Value / 1024) / 1024;
                            PSE.numberJsResources       = Convert.ToString(d.lighthouseResult.audits["resource-summary"].details.items[2].requestCount.Value);
                            PSE.numberCssResources      = Convert.ToString(d.lighthouseResult.audits["resource-summary"].details.items[3].requestCount.Value);
                            PSEList.Add(PSE);
                            Console.WriteLine("Done => " + type[j].ToString() + " => " + websites[i].ToString());
                            MailerPageSpeed.WriteToFile("Done => " + type[j].ToString() + " => " + websites[i].ToString());
                        }
                        catch (Exception ex)
                        {
                            PSEList.Clear();
                            Console.WriteLine("Failed");
                            MailerPageSpeed.WriteToFile("Analysis => Failed => " + ex.ToString());
                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine("Trying Again . . .");
                            MailerPageSpeed.WriteToFile("Trying Again . . .");
                            PageSpeed();
                        }
                        response.PageSpeedEntityData = PSEList;
                    }
                }
                j = 0;
                Thread.Sleep(15000);
            }
            string html = MailerPageSpeed.getHtml(response);

            MailerPageSpeed.WriteToFile(html);
            MailerPageSpeed.Email(html);
            MailerPageSpeed.WriteToFile("succes");
            return("success");
        }
        public static string getHtml(PageSpeedEntityList input)
        {
            try
            {
                string messageBody        = "<div style=\"width:100%;text-align:center\"><h2 style=\"color:red\">Google PageSpeed Data Analysis as on " + DateTime.Now.ToShortDateString() + " </h2><br><br>";
                string htmlTableStart     = "<table style=\"border-collapse:collapse; text-align:center;margin:0 auto;width:90%;\" >";
                string htmlTableEnd       = "</table>";
                string htmlHeaderRowStart = "<tr style=\"background-color:#6FA1D2; color:#ffffff;\">";
                string htmlHeaderRowEnd   = "</tr>";
                string htmlTrStart        = "<tr style=\"color:#555555;\">";
                string htmlTrEnd          = "</tr>";
                string htmlTdStart        = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
                string htmlTdEnd          = "</td>";
                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "URL" + htmlTdEnd;
                messageBody += htmlTdStart + "DateTime" + htmlTdEnd;
                messageBody += htmlTdStart + "Device" + htmlTdEnd;
                messageBody += htmlTdStart + "Score" + htmlTdEnd;
                messageBody += htmlTdStart + "No.of CSS" + htmlTdEnd;
                messageBody += htmlTdStart + "CSS Size (MB)" + htmlTdEnd;
                messageBody += htmlTdStart + "No.of JS" + htmlTdEnd;
                messageBody += htmlTdStart + "JS Size (MB)" + htmlTdEnd;
                messageBody += htmlTdStart + "Images Size (MB)" + htmlTdEnd;
                messageBody += htmlTdStart + "HTML Response Time (MB)" + htmlTdEnd;
                messageBody += htmlTdStart + "Page Size (MB)" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
                //Loop all the rows from grid vew and added to html td
                for (int i = 0; i <= input.PageSpeedEntityData.Count - 1; i++)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + input.PageSpeedEntityData[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + input.PageSpeedEntityData[i].FetchTime + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + input.PageSpeedEntityData[i].Type + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + input.PageSpeedEntityData[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + input.PageSpeedEntityData[i].numberCssResources + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(input.PageSpeedEntityData[i].cssResponseBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + input.PageSpeedEntityData[i].numberJsResources + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(input.PageSpeedEntityData[i].javascriptResponseBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(input.PageSpeedEntityData[i].imageResponseBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(input.PageSpeedEntityData[i].HTMLResponseTime, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(input.PageSpeedEntityData[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                }
                messageBody = messageBody + htmlTableEnd;

                messageBody += "<h2 style=\"color:red;\">HDFCERGO with Competitor</h2><div style=\"display:flex;\"><div style=\"width:50%;text-align:center\"><h4 style=\"color:red;\">Desktop Page Speed Score</h4>" + htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "Rank" + htmlTdEnd;
                messageBody += htmlTdStart + "Company" + htmlTdEnd;
                messageBody += htmlTdStart + "Score" + htmlTdEnd;
                messageBody += htmlTdStart + "PageSize" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
                //Loop all the rows from grid vew and added to html td

                var PageSpeedEntityData = input.PageSpeedEntityData.OrderByDescending(x => x.PageScore).Where(x => x.Type == "desktop").ToList();
                int rank = 1;
                for (int i = 0; i <= PageSpeedEntityData.Count - 1; i++)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + rank + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(PageSpeedEntityData[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                    rank        = rank + 1;
                }
                messageBody = messageBody + htmlTableEnd + "</div>";

                messageBody += "<div style=\"width:50%;text-align:center\"><h4 style=\"color:red;\">Mobile Page Speed Score</h4>" + htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "Rank" + htmlTdEnd;
                messageBody += htmlTdStart + "Company" + htmlTdEnd;
                messageBody += htmlTdStart + "Score" + htmlTdEnd;
                messageBody += htmlTdStart + "PageSize" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
                //Loop all the rows from grid vew and added to html td
                var PageSpeedEntityData1 = input.PageSpeedEntityData.OrderByDescending(x => x.PageScore).Where(x => x.Type == "mobile").ToList();
                rank = 1;
                for (int i = 0; i <= PageSpeedEntityData1.Count - 1; i++)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + rank + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData1[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData1[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(PageSpeedEntityData1[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                    rank        = rank + 1;
                }

                messageBody = messageBody + htmlTableEnd + "</div></div>";

                messageBody += "<h2 style=\"color:red;\">HDFCERGO with Aggregators</h2><div style=\"display:flex;\"><div style=\"width:50%;text-align:center\"><h4 style=\"color:red;\">Desktop Page Speed Score</h4>" + htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "Rank" + htmlTdEnd;
                messageBody += htmlTdStart + "Company" + htmlTdEnd;
                messageBody += htmlTdStart + "Score" + htmlTdEnd;
                messageBody += htmlTdStart + "PageSize" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
                //Loop all the rows from grid vew and added to html td
                var PageSpeedEntityData2    = input.PageSpeedEntityData.OrderByDescending(x => x.PageScore).Where(x => x.Type == "desktop").ToList();
                int rankHDFC                = PageSpeedEntityData2.FindIndex(x => x.Webiste == "https://www.hdfcergo.com") + 1;
                var PageSpeedEntityDataHDFC = input.PageSpeedEntityData.Where(x => x.Type == "desktop").Where(x => x.Webiste == "https://www.hdfcergo.com").ToList();
                for (int i = 0; i < 1; i++)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + "1" + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData2[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData2[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(PageSpeedEntityData2[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + rankHDFC + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityDataHDFC[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityDataHDFC[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(PageSpeedEntityDataHDFC[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                }
                messageBody = messageBody + htmlTableEnd + "</div>";

                messageBody += "<div style=\"width:50%;text-align:center\"><h4 style=\"color:red;\">Mobile Page Speed Score</h4>" + htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "Rank" + htmlTdEnd;
                messageBody += htmlTdStart + "Company" + htmlTdEnd;
                messageBody += htmlTdStart + "Score" + htmlTdEnd;
                messageBody += htmlTdStart + "PageSize" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
                //Loop all the rows from grid vew and added to html td
                var PageSpeedEntityData3     = input.PageSpeedEntityData.OrderByDescending(x => x.PageScore).Where(x => x.Type == "mobile").ToList();
                int rankHDFC2                = PageSpeedEntityData3.FindIndex(x => x.Webiste == "https://www.hdfcergo.com") + 1;
                var PageSpeedEntityData2HDFC = input.PageSpeedEntityData.Where(x => x.Type == "mobile").Where(x => x.Webiste == "https://www.hdfcergo.com").ToList();
                for (int i = 0; i < 1; i++)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + "1" + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData3[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData3[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(PageSpeedEntityData3[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + rankHDFC2 + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData2HDFC[i].Webiste + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + PageSpeedEntityData2HDFC[i].PageScore + htmlTdEnd;
                    messageBody = messageBody + htmlTdStart + Math.Round(PageSpeedEntityData2HDFC[i].totalRequestBytes, 2) + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                }
                messageBody = messageBody + htmlTableEnd + "</div></div></div>";
                return(messageBody); // return HTML Table as string from this function
            }
            catch (Exception ex)
            {
                WriteToFile("getHtml() => " + ex.ToString());
                return(null);
            }
        }