Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created May 29, 2011 21:23
Show Gist options
  • Select an option

  • Save ashaw/998149 to your computer and use it in GitHub Desktop.

Select an option

Save ashaw/998149 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'faster_csv'
TIMEFLOW_HEADERS = ["StartDate", "EndDate", "Time", "Events", "Remarks", "Where", "Coordinates", "Source"]
TIMELINESETTER_HEADERS = ["date", "display_date", "description", "link", "series", "html"]
class TF2TS
attr_reader :tf
def initialize(tf_csv)
@filename = tf_csv
@tf = FasterCSV.read(tf_csv, :headers => true)
@@series_headers = @tf.headers - TIMEFLOW_HEADERS
@ts = []
end
def build_ts
csv_str = FasterCSV.generate do |csv|
csv << TIMELINESETTER_HEADERS
@tf.each do |row|
csv << [Time.parse("#{row['StartDate']} #{row['Time']}"), row['StartDate'], row['Events'],'',self.class.get_primary_series(row),'','']
end
end
File.open("#{@filename.gsub(/\.csv/,'')}_timelinesetter.csv", 'w+') do |f|
f.write csv_str
end
end
# TimeFlow allows multiple series per event.
# TimelineSetter only allows one, so we'll find the first series
# each TF row is part of.
def self.get_primary_series(row)
@@series_headers.each do |series|
if !row[series].nil?
return series
end
end
end
end
TF_CSV = ARGV[0]
if ARGV.nil?
puts "== please provide a CSV filename"
exit 1
end
t = TF2TS.new(TF_CSV)
t.build_ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment