Skip to content

Instantly share code, notes, and snippets.

@jmoreno
Created November 6, 2016 09:21
Show Gist options
  • Select an option

  • Save jmoreno/5357e41265d778f484621141801656dd to your computer and use it in GitHub Desktop.

Select an option

Save jmoreno/5357e41265d778f484621141801656dd to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'csv'
doc = File.open("exportación.xml") {|f| Nokogiri::XML(f)}
CSV.open('HealthData.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["Locale"]
doc.css("HealthData").each do |healthData|
csvfile << [healthData["locale"]]
end
end
CSV.open('ExportDate.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["value"]
doc.css("HealthData ExportDate").each do |exportDate|
csvfile << [exportDate["value"]]
end
end
CSV.open('Me.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["HKCharacteristicTypeIdentifierDateOfBirth", "HKCharacteristicTypeIdentifierBiologicalSex", "HKCharacteristicTypeIdentifierBloodType", "HKCharacteristicTypeIdentifierFitzpatrickSkinType"]
doc.css("HealthData Me").each do |me|
csvfile << [me["HKCharacteristicTypeIdentifierDateOfBirth"], me["HKCharacteristicTypeIdentifierBiologicalSex"], me["HKCharacteristicTypeIdentifierBloodType"], me["HKCharacteristicTypeIdentifierFitzpatrickSkinType"]]
end
end
CSV.open('Record.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["type", "unit", "value", "sourceName", "sourceVersion", "device", "creationDate", "startDate", "endDate"]
doc.css("HealthData Record").each do |record|
csvfile << [record["type"], record["unit"], record["value"], record["sourceName"], record["sourceVersion"], record["device"], record["creationDate"], record["startDate"], record["endDate"]]
end
end
CSV.open('Correlation.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["type", "sourceName", "sourceVersion", "device", "creationDate", "startDate", "endDate"]
doc.css("HealthData Correlation").each do |correlation|
csvfile << [correlation["type"], correlation["sourceName"], correlation["sourceVersion"], correlation["device"], correlation["creationDate"], correlation["startDate"], correlation["endDate"]]
end
end
CSV.open('Workout.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["workoutActivityType", "duration", "durationUnit", "totalDistance", "totalDistanceUnit", "totalEnergyBurned", "totalEnergyBurnedUnit", "sourceName", "sourceVersion", "device", "creationDate", "startDate", "endDate"]
doc.css("HealthData Workout").each do |workout|
csvfile << [workout["workoutActivityType"], workout["duration"], workout["durationUnit"], workout["totalDistance"], workout["totalDistanceUnit"], workout["totalEnergyBurned"], workout["totalEnergyBurnedUnit"], workout["sourceName"], workout["sourceVersion"], workout["device"], workout["creationDate"], workout["startDate"], workout["endDate"]]
end
end
CSV.open('WorkoutEvent.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["type", "date"]
doc.css("HealthData WorkoutEvent").each do |workoutEvent|
csvfile << [workoutEvent["type"], workoutEvent["date"]]
end
end
CSV.open('ActivitySummary.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["dateComponents", "activeEnergyBurned", "activeEnergyBurnedGoal", "activeEnergyBurnedUnit", "appleExerciseTime", "appleExerciseTimeGoal", "appleStandHours", "appleStandHoursGoal"]
doc.css("HealthData ActivitySummary").each do |activitySummary|
csvfile << [activitySummary["dateComponents"], activitySummary["activeEnergyBurned"], activitySummary["activeEnergyBurnedGoal"], activitySummary["activeEnergyBurnedUnit"], activitySummary["appleExerciseTime"], activitySummary["appleExerciseTimeGoal"], activitySummary["appleStandHours"], activitySummary["appleStandHoursGoal"]]
end
end
CSV.open('MetadataEntry.csv', 'wb', col_sep: ";") do |csvfile|
csvfile << ["key", "value"]
doc.css("HealthData MetadataEntry").each do |metadataEntry|
csvfile << [metadataEntry["key"], metadataEntry["value"]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment