Browse Source

Add script to parse ModernGov XML API election results data

tags/last-sinatra-version
Adrian Short 6 years ago
parent
commit
bdd48d43e4
3 changed files with 28 additions and 0 deletions
  1. +1
    -0
      Gemfile
  2. +4
    -0
      Gemfile.lock
  3. +23
    -0
      scripts/moderngovparser.rb

+ 1
- 0
Gemfile View File

@@ -11,6 +11,7 @@ gem 'rack-flash3'

group :development do
gem 'shotgun'
gem 'nokogiri'
end

group :production do


+ 4
- 0
Gemfile.lock View File

@@ -61,8 +61,11 @@ GEM
json (1.8.6)
json_pure (1.8.6)
kgio (2.11.2)
mini_portile2 (2.3.0)
multi_json (1.13.1)
mustermann (1.0.2)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
pg (1.0.0)
public_suffix (3.0.2)
rack (2.0.4)
@@ -93,6 +96,7 @@ DEPENDENCIES
data_mapper
dm-postgres-adapter
haml
nokogiri
pg
rack-flash3
shotgun


+ 23
- 0
scripts/moderngovparser.rb View File

@@ -0,0 +1,23 @@
# Convert ModernGov XML file to CSV
# eg https://moderngov.sutton.gov.uk/mgWebService.asmx/GetElectionResults?lElectionId=15
# The full API docs are at: https://moderngov.sutton.gov.uk/mgWebService.asmx

require 'nokogiri'
require 'csv'
require 'pp'

doc = File.open(ARGV.shift) { |f| Nokogiri::XML(f) }

csv_string = CSV.generate do |csv|
doc.search('candidates candidate').each do |cand|
row = []
row << cand.at('areatitle').inner_text
row << cand.at('candidatename').inner_text
row << cand.at('politicalpartytitle').inner_text
row << cand.at('numvotes').inner_text.to_i
row << cand.at('iselected').inner_text
csv << row
end
end

puts csv_string

Loading…
Cancel
Save