Created
February 21, 2014 23:37
-
-
Save shaneshifflett/9145985 to your computer and use it in GitHub Desktop.
convert a google spreadsheet to a json file that can be read by timeline.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task :jsonify_timeline => :environment do | |
require 'csv' | |
require 'json' | |
require 'open-uri' | |
def scrub(text) | |
if text.include? '--' | |
text = text.gsub(/--/, '—') | |
end | |
return text | |
end | |
url = 'https://docs.google.com/spreadsheet/pub?key=URKEYE&output=csv' | |
page = open(url) | |
contents = page.read() | |
dump = { | |
:timeline => { | |
:headline => "", | |
:type => "default", | |
:text => "", | |
:asset => { | |
:media => "", | |
:credit => "", | |
:caption => "" | |
}, | |
:date => [] | |
} | |
} | |
CSV.parse(contents) do |row| | |
date_obj = { | |
:startDate => row[0], | |
:endDate => row[1], | |
:headline => row[2], | |
:text => scrub(row[3]), | |
:asset => { | |
:media => row[4], | |
:credit => row[5], | |
:caption => row[6] | |
} | |
} | |
dump[:timeline][:date].push(date_obj) | |
end | |
File.open("#{Rails.root}/timeline-data.json", 'w') { |file| file.write(dump.to_json) } | |
puts dump.to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment