diff --git a/docs/2017/2017-general-election-results.csv b/docs/2017/2017-general-election-results.csv new file mode 100644 index 0000000..2ca5633 --- /dev/null +++ b/docs/2017/2017-general-election-results.csv @@ -0,0 +1,10 @@ +Carshalton and Wallington,Tom Brake,Liberal Democrats,20819,1 +Carshalton and Wallington,Matthew Joseph Maxwell Scott,Conservative Party,19450,0 +Carshalton and Wallington,Emine Ibrahim,Labour Party,9360,0 +Carshalton and Wallington,Shasha Khan,Green Party,501,0 +Carshalton and Wallington,Nick Mattey,Independent,434,0 +Carshalton and Wallington,Ashley Keith Dickenson,Christian Peoples Alliance Party,189,0 +Sutton and Cheam,Paul Stuart Scully,Conservative Party,26567,1 +Sutton and Cheam,Amna Ahmad,Liberal Democrats,13869,0 +Sutton and Cheam,Bonnie Craven,Labour Party,10663,0 +Sutton and Cheam,Claire Jackson-Prior,Green Party,871,0 diff --git a/scripts/import-results.rb b/scripts/import-results.rb new file mode 100755 index 0000000..b9c1f10 --- /dev/null +++ b/scripts/import-results.rb @@ -0,0 +1,67 @@ +require_relative '../models' +require 'pp' +# $ ruby scripts/import-results.rb [ELECTION ID] [CSV FILENAME] + +# Import a CSV file of election results for a specified election +# The election for which you're importing data must already exist in the elections table + +# Run setpositions.rb after this importer to set candidacies.position and candidacies.elected + +@election = Election.get(ARGV.shift) + +CSV.foreach(ARGV.shift) do |line| + district_name, candidate_name, party, votes, seats = line + + pp line + + # District + @district = District.first(:name => district_name) + + puts @district.name + + # Candidate + # Assumes that the candidate name is written "forename surname" + bits = candidate_name.split(" ") + candidate_forenames = bits[0..-2].join(" ") + candidate_surname = bits.last + + # Assumes that the candidate name is written "surname, forename(s)" + # bits = candidate_name.split(", ") + # candidate_forenames = bits[1] + # candidate_surname = bits[0] + + @candidate = Candidate.first_or_create(:forenames => candidate_forenames, :surname => candidate_surname) + + pp @candidate + + unless @candidate.saved? + $stderr.puts "Couldn't save candidate #{@candidate}" + exit 1 + end + + + # Party + @party = Party.first_or_create(:name => party) + + puts @party.name + unless @party.saved? + $stderr.puts "Couldn't save party #{@party}" + exit 1 + end + + # Candidacy + @candidacy = Candidacy.create( + :election => @election, + :candidate => @candidate, + :party => @party, + :district => @district, + :votes => votes, + :seats => seats + ) + + unless @candidacy.saved? + $stderr.puts "Couldn't save candidacy #{@candidacy}" + exit 1 + end + +end