Election results in the London Borough of Sutton.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 lines
1.6 KiB

  1. require_relative '../models'
  2. require 'pp'
  3. # $ ruby scripts/import-results-tsv.rb [ELECTION ID] [TSV FILENAME]
  4. # Import a TSV file of election results for a specified election
  5. # The election for which you're importing data must already exist in the elections table
  6. # Run setpositions.rb after this importer to set candidacies.position and candidacies.elected
  7. @election = Election.get(ARGV.shift)
  8. ARGF.each do |line|
  9. district_name, candidate_name, party, votes, elected = line.split("\t").map{ |e| e.strip }
  10. # District
  11. @district = District.first(:name => district_name)
  12. # Candidate
  13. # Assumes that the candidate name is written "forename surname"
  14. # bits = candidate_name.split(" ")
  15. # candidate_forenames = bits[0..-2].join(" ")
  16. # candidate_surname = bits.last
  17. # Assumes that the candidate name is written "surname, forename(s)"
  18. bits = candidate_name.split(", ")
  19. candidate_forenames = bits[1]
  20. candidate_surname = bits[0]
  21. @candidate = Candidate.first_or_create(:forenames => candidate_forenames, :surname => candidate_surname)
  22. pp @candidate
  23. unless @candidate.saved?
  24. $stderr.puts "Couldn't save candidate #{@candidate}"
  25. exit 1
  26. end
  27. # Party
  28. @party = Party.first_or_create(:name => party)
  29. puts @party.name
  30. unless @party.saved?
  31. $stderr.puts "Couldn't save party #{@party}"
  32. exit 1
  33. end
  34. # Candidacy
  35. @candidacy = Candidacy.create(
  36. :election => @election,
  37. :candidate => @candidate,
  38. :party => @party,
  39. :district => @district,
  40. :votes => votes
  41. )
  42. unless @candidacy.saved?
  43. $stderr.puts "Couldn't save candidacy #{@candidacy}"
  44. exit 1
  45. end
  46. end