From bdd48d43e48b548cc5957b13faac5c072da61d0c Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Sat, 18 Aug 2018 16:31:02 +0100 Subject: [PATCH] Add script to parse ModernGov XML API election results data --- Gemfile | 1 + Gemfile.lock | 4 ++++ scripts/moderngovparser.rb | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 scripts/moderngovparser.rb diff --git a/Gemfile b/Gemfile index 124c28f..ca4a53f 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem 'rack-flash3' group :development do gem 'shotgun' + gem 'nokogiri' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index e167eed..d106f7d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/scripts/moderngovparser.rb b/scripts/moderngovparser.rb new file mode 100644 index 0000000..287e376 --- /dev/null +++ b/scripts/moderngovparser.rb @@ -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