Esempio n. 1
0
        private static ConstituencyResult GetConstituencyResult(List <ConstituencyResult> ConstituencyResults, HtmlDocument docInner1, Constituency c)
        {
            ConstituencyResult conR = new ConstituencyResult();

            const string xpathWinningParty =
                "//span[@class='ge2019-constituency-result-turnout__block ge2019-constituency-result-turnout__majority']"
                + "/span[@class='ge2019-constituency-result-turnout__label']";

            var winningParty =
                docInner1.DocumentNode.SelectNodes(xpathWinningParty)
                .FirstOrDefault()
                .InnerText;

            conR.winningParty = winningParty.Substring(0, winningParty.IndexOf(" "));

            const string xpathHeadline = "//p[@class='ge2019-constituency-result-headline__text']";

            var headline =
                docInner1.DocumentNode.SelectNodes(xpathHeadline)
                .FirstOrDefault()
                .InnerText;

            conR.headline = headline.Substring(conR.winningParty.Length + 1);

            const string xpathMajority =
                "//span[@class='ge2019-constituency-result-turnout__block ge2019-constituency-result-turnout__majority']"
                + "/span[@class='ge2019-constituency-result-turnout__value']";

            var majority =
                docInner1.DocumentNode.SelectNodes(xpathMajority)
                .FirstOrDefault()
                .InnerText;

            conR.majority = Convert.ToInt32(majority.Replace(",", ""));

            const string xpathElectorate =
                "//span[@class='ge2019-constituency-result-turnout__block ge2019-constituency-result-turnout__electorate']"
                + "/span[@class='ge2019-constituency-result-turnout__value']";

            var electorate =
                docInner1.DocumentNode.SelectNodes(xpathElectorate)
                .FirstOrDefault()
                .InnerText;

            conR.electorate = Convert.ToInt32(electorate.Replace(",", ""));


            const string xpathTurnout =
                "//span[@class='ge2019-constituency-result-turnout__block ge2019-constituency-result-turnout__percentage']"
                + "/span[@class='ge2019-constituency-result-turnout__value']";

            var turnout =
                docInner1.DocumentNode.SelectNodes(xpathTurnout)
                .FirstOrDefault()
                .InnerText;

            turnout      = turnout.Substring(0, turnout.Length - 1);
            conR.turnout = System.Convert.ToDecimal(turnout);


            const string xpathTurnoutChange =
                "//span[@class='ge2019-constituency-result-turnout__block ge2019-constituency-result-turnout__change']"
                + "/span[@class='ge2019-constituency-result-turnout__value']";

            var turnoutChange =
                docInner1.DocumentNode.SelectNodes(xpathTurnoutChange)
                .FirstOrDefault()
                .InnerText;

            conR.turnoutChange = System.Convert.ToDecimal(turnoutChange);


            conR.conID      = c.conID;
            conR.electionID = 3;

            ConstituencyResults.Add(conR);

            Console.WriteLine("CONSTITUENCY");
            Console.WriteLine("============");
            Console.WriteLine(c.ons);
            Console.WriteLine(c.conName);
            Console.WriteLine(c.conID);
            Console.WriteLine("Winner: " + conR.winningParty + " " + conR.headline);
            Console.WriteLine("Majority: " + conR.majority);
            Console.WriteLine("Electorate: " + conR.electorate);
            Console.WriteLine("Turnout: " + conR.turnout);
            Console.WriteLine("Turnout change: " + conR.turnoutChange);
            Console.WriteLine("              CANDIDATES");
            Console.WriteLine("              **********");


#if DATABASE
            // write constituency result record
            try
            {
                using (SqlConnection cn = new SqlConnection(DataAccess.connectionString))
                {
                    cn.Open();
                    SqlCommand cmd = new SqlCommand("add_constituency_result", cn);

                    cmd.CommandType    = CommandType.StoredProcedure;
                    cmd.CommandTimeout = 600;
                    cmd.Parameters.Add(new SqlParameter("@con_id",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@election_id",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@winning_party",
                                                        SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("@headline",
                                                        SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("@majority",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@electorate",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@turnout",
                                                        SqlDbType.Decimal));
                    cmd.Parameters.Add(new SqlParameter("@turnout_change",
                                                        SqlDbType.Decimal));

                    cmd.Parameters[0].Value = c.conID;
                    cmd.Parameters[1].Value = conR.electionID;
                    cmd.Parameters[2].Value = conR.winningParty;
                    cmd.Parameters[3].Value = conR.headline;
                    cmd.Parameters[4].Value = conR.majority;
                    cmd.Parameters[5].Value = conR.electorate;
                    cmd.Parameters[6].Value = conR.turnout;
                    cmd.Parameters[7].Value = conR.turnoutChange;

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException ex)
            {
                DataAccess.SqlExceptionHandler(ex);
            }
#endif
            return(conR);
        }
Esempio n. 2
0
        private static void GetCandidateResult(List <CandidateResult> CandidateResults, Constituency c, ConstituencyResult conR, HtmlNode candidateListItem)
        {
            CandidateResult canR = new CandidateResult();

            // get each item of data for candidate

            const string xpathPartyCode =
                ".//div[@class='ge2019-constituency-result__row']" +
                "/div[@class='ge2019-constituency-result__party']" +
                "/span[@class='ge2019-constituency-result__party-code']";

            canR.partyCode =
                candidateListItem.SelectNodes(xpathPartyCode)
                .FirstOrDefault()
                .InnerText;

            const string xpathPartyName =
                ".//div[@class='ge2019-constituency-result__row']" +
                "/div[@class='ge2019-constituency-result__party']" +
                "/span[@class='ge2019-constituency-result__party-name']";

            canR.partyName =
                candidateListItem.SelectNodes(xpathPartyName)
                .FirstOrDefault()
                .InnerText;

            const string xpathCandidateName =
                ".//div[@class='ge2019-constituency-result__row']" +
                "/div[@class='ge2019-constituency-result__candidate']" +
                "/span[@class='ge2019-constituency-result__candidate-name']";

            canR.candidateName =
                candidateListItem.SelectNodes(xpathCandidateName)
                .FirstOrDefault()
                .InnerText;

            // 3 list items not individually named so loop through them

            const string xpathDetails =
                ".//div[@class='ge2019-constituency-result__details']" +
                "/ul[@class='ge2019-constituency-result__details-list']" +
                "/li[@class='ge2019-constituency-result__details-item']" +
                "/span[@class='ge2019-constituency-result__text-wrapper']" +
                "/span[@class='ge2019-constituency-result__details-value']";

            var voteNodes =
                candidateListItem.SelectNodes(xpathDetails);

            var a = 0;

            foreach (var v in voteNodes)
            {
                a++;
                if (a == 1)
                {
                    canR.votes = Convert.ToInt32(v.InnerText.Replace(",", ""));
                }
                else if (a == 2)
                {
                    canR.voteShare = System.Convert.ToDecimal(v.InnerText);
                }
                else if (a == 3)
                {
                    canR.voteShareChange = System.Convert.ToDecimal(v.InnerText);
                }
            }


            var space = "              ";

            Console.WriteLine(space + canR.partyCode);
            Console.WriteLine(space + canR.partyName);
            Console.WriteLine(space + canR.candidateName);
            Console.WriteLine(space + canR.votes);
            Console.WriteLine(space + canR.voteShare);
            Console.WriteLine(space + canR.voteShareChange);

            canR.conID      = c.conID;
            canR.electionID = conR.electionID;

            CandidateResults.Add(canR);


            // write candidate record

#if DATABASE
            try
            {
                using (SqlConnection cn = new SqlConnection(DataAccess.connectionString))
                {
                    cn.Open();
                    SqlCommand cmd = new SqlCommand("add_candidate_result", cn);

                    cmd.CommandType    = CommandType.StoredProcedure;
                    cmd.CommandTimeout = 600;
                    cmd.Parameters.Add(new SqlParameter("@con_id",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@election_id",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@party_code",
                                                        SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("@party",
                                                        SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("@candidate",
                                                        SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("@votes",
                                                        SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@vote_share",
                                                        SqlDbType.Decimal));
                    cmd.Parameters.Add(new SqlParameter("@vote_share_change",
                                                        SqlDbType.Decimal));

                    cmd.Parameters[0].Value = c.conID;
                    cmd.Parameters[1].Value = canR.electionID;
                    cmd.Parameters[2].Value = canR.partyCode;
                    cmd.Parameters[3].Value = canR.partyName;
                    cmd.Parameters[4].Value = canR.candidateName;
                    cmd.Parameters[5].Value = canR.votes;
                    cmd.Parameters[6].Value = canR.voteShare;
                    cmd.Parameters[7].Value = canR.voteShareChange;

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException ex)
            {
                DataAccess.SqlExceptionHandler(ex);
            }
#endif
#if !DATABASE && !CSV
            Console.ReadLine();
#endif
        }