Created
June 18, 2016 21:08
-
-
Save jotto/06d3deb59a8628d545ca4fbd6772340e to your computer and use it in GitHub Desktop.
Convert ExportReport_CSV.txt from omronwellness.com to a CSV file healthmate.withings.com can read
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
require 'csv' | |
# example usage: | |
# ruby omron-to-withings.rb ExportReport_CSV.txt | |
# and it outputs a new CSV file that withings can read | |
if ARGV[0].to_s == "" | |
puts "first arg must be path to omron csv file for blood pressure readings" | |
exit(1) | |
end | |
file = File.open(ARGV[0], "rb") | |
contents = file.read | |
results = {} | |
contents.split("\n")[4..-1].each do |line| | |
next if line.strip == "" | |
line = line.gsub(/,\s+\"/,',"') # strip spaces before quotes otherwise CSV.parse_line will fail | |
parsed = CSV.parse_line(line) | |
date = DateTime.parse(parsed[1]).to_time.utc.strftime("%F %T") | |
results[date] ||= {} | |
results[date][:real_date] = DateTime.parse(parsed[1]) | |
type = parsed[0] | |
if type == "Blood Pressure" | |
results[date][:systole], results[date][:diastole] = parsed[2].split(/\//).collect{ |v| v.gsub(/\D/,'') } | |
elsif type == "Pulse" | |
results[date][:bpm] = parsed[2].gsub(/\D/,'') | |
end | |
end | |
last_date = results.collect{|k,v|v[:real_date]}.sort.last.to_date | |
file_name = ARGV[0].gsub(/\.txt$/,"_withings_#{last_date.to_s}.csv") | |
puts file_name | |
CSV.open(file_name, "wb") do |csv| | |
results.each do |date, results_hash| | |
csv << [date, results_hash[:bpm], results_hash[:systole], results_hash[:diastole]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment