-
-
Save Artaud/4524fa0d45afd144a7ddb121b1ff0d93 to your computer and use it in GitHub Desktop.
Migrates SleepBot data to Sleep as Android's format. Updated by the author of Sleep as Android.
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
When exporting from mysleepbot.com: | |
-go to settings | |
-Change Date Format to Day/Month/Year | |
-Change Time Format to 24 hours | |
-Go to Entries | |
-Select from date that is older than your first sleepbot entry | |
-Select to date as todays date | |
-Export as CSV name file sleepbot-raw.csv | |
-Edit file and remove the first line | |
-SUPER IMPORTANT: In the source code, change timezone to your phone's current timezone. (Line 76 - change America/Chicago to your TZ - list of timezones is here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) | |
-Run this script on that file! | |
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' | |
# Parses CSV-exported data from SleepBot | |
def parse_export(row) | |
# Parse the wake up date | |
date_to = row[0].tr("'","").split('-') | |
date_to = Time.new(date_to[2].to_i, date_to[1], date_to[0]) | |
# Parse the wake up/sleep times | |
time_from = row[1].tr("'","").split(':') | |
time_to = row[2].tr("'","").split(':') | |
time_from = Time.new(1970, 1, 1, time_from[0], time_from[1]) | |
time_to = Time.new(1970, 1, 1, time_to[0], time_to[1]) | |
# Check whether we advanced a day. Our start date is one day before in that case. | |
is_from_prev_day = time_to < time_from | |
# Construct the sleep date/time and wake up date/time | |
time_to = Time.new(date_to.year, date_to.mon, date_to.day, time_to.hour, time_to.min) | |
time_from = Time.new(date_to.year, date_to.mon, date_to.day, time_from.hour, time_from.min) | |
time_from -= 1 * 24 * 60 * 60 if is_from_prev_day | |
row3 = row[3].tr("'","").tr(" min","").sub("hr",":") | |
hour = row3[0,2] | |
min = row3[3,2].to_i*100/60 | |
hours = (hour.to_s + "." + min.to_s).to_f | |
notes = row[4].tr("'","").to_f | |
puts notes | |
[time_from, time_to, hours, notes , ''] | |
end | |
# Parses raw SQLite database dumps from the SleepBot app. | |
def parse_raw(row) | |
# Java timestamps: Unix timestamps, with ns resolution | |
timestamp_from = row[1].to_f / 1000.0 | |
timestamp_to = row[2].to_f / 1000.0 | |
hours = (timestamp_to - timestamp_from) / 60 / 60 | |
notes = '' | |
rating = row[3] | |
[Time.at(timestamp_from), Time.at(timestamp_to), hours, rating, notes] | |
end | |
# Formates Date/Time objects in Sleep as Android format | |
def sleepandroid_time(time) | |
'%.2d. %.2d. %d %d:%.2d' % [time.day, time.mon, time.year, time.hour, time.min] | |
end | |
results = [] | |
parser = :parse_export | |
# Load the import file. | |
CSV.foreach('sleepbot-raw.csv') do |row| | |
date_to = row[0] | |
# Detect the type of the file we are importing. | |
parser = :parse_raw if date_to == '_id' | |
next if date_to == 'Date' || date_to == '_id' | |
unless row[0].nil? | |
a = send(parser, row) | |
# puts a.inspect | |
results << a | |
end | |
end | |
# Write the converted file in the Sleep as Android format. | |
CSV.open('./sleepbot-converted.csv', 'wb') do |csv| | |
results.each do |result| | |
# puts result | |
csv << ['Id', 'Tz', 'From', 'To', 'Sched', 'Hours', 'Rating', 'Comment', 'Framerate', 'Snore', 'Noise', 'Cycles', 'DeepSleep', 'LenAdjust', 'Geo', '%d:%d' % [result[1].hour, result[1].min], 'Event', 'Event'] | |
csv << [result[0].to_i * 1000, 'America/Los_Angeles', sleepandroid_time(result[0]), sleepandroid_time(result[1]), sleepandroid_time(result[1]), result[2], result[3], result[4], 10000, -1, -1, -1, -1, 0, '', 0, 'DEEP_START-' + (result[0].to_i * 1000).to_s, 'DEEP_END-' + (result[0].to_i * 1000).to_s] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a Python version which did the same thing but using the exported format from the app version of Sleepbot. Good for those who never use mysleepbot.com like me.
https://github.com/VeryCrazyDog/sleepbot-to-sleep_as_android