Created
October 28, 2019 07:51
-
-
Save vanous/9b49182475799d3630a64877716e1f00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import csv | |
import datetime | |
import sys | |
import sqlite3 | |
import random | |
#import script to get MiFit data into Gadgetbridge database | |
activity_file_name="ACTIVITY_MINUTE.csv" | |
hr_file_name1="HEARTRATE.csv" | |
hr_file_name2="HEARTRATE_AUTO.csv" | |
sleep_file_name="SLEEP.csv" | |
database="Gadgetbridge" | |
device_id=1 | |
user_id=1 | |
conn = sqlite3.connect(database) | |
cursor=conn.cursor() | |
#build HR dictionary | |
hr={} | |
data=csv.reader(open(hr_file_name1),delimiter=',') | |
#1572088219,81 | |
next(data) #skip header | |
for line in data: | |
hr[line[0]]=line[1] | |
data=csv.reader(open(hr_file_name2),delimiter=',') | |
#2017-07-14,23:35,54 | |
next(data) #skip header | |
for line in data: | |
date="{0},{1}".format(*line) | |
dt=datetime.datetime.strptime(date, '%Y-%m-%d,%H:%M') | |
#timestamp=dt.timestamp() | |
timestamp=dt.replace(tzinfo=datetime.timezone.utc).timestamp() | |
hr[timestamp]=line[2] | |
# steps | |
data=csv.reader(open(activity_file_name),delimiter=',') | |
#2017-07-04,13:18,11 | |
next(data) #skip header | |
for line in data: | |
#print(line) | |
date="{0},{1}".format(*line) | |
dt=datetime.datetime.strptime(date, '%Y-%m-%d,%H:%M') | |
#print(dt) | |
w={} | |
#timestamp=dt.timestamp() | |
timestamp=dt.replace(tzinfo=datetime.timezone.utc).timestamp() | |
w["timestamp"]=timestamp | |
r=cursor.execute("SELECT * from MI_BAND_ACTIVITY_SAMPLE where TIMESTAMP=$timestamp", (w )).fetchone() | |
if r: | |
#print("record exists", r,line[2]) | |
pass | |
else: | |
steps=int(line[2]) | |
heart_rate=hr.get(timestamp,255) | |
raw_intensity=random.randint(10,130) | |
if steps < 80: | |
raw_kind=1 #slow walking | |
elif 100 > steps > 80: | |
raw_kind=3 #fast walking | |
else: | |
raw_kind=4 #running | |
print("inserting", steps, heart_rate) | |
cursor.execute("INSERT INTO MI_BAND_ACTIVITY_SAMPLE VALUES (?,?,?,?,?,?,?)",(timestamp,device_id,user_id,raw_intensity,steps,raw_kind,heart_rate,)) | |
#sleep | |
data=csv.reader(open(sleep_file_name),delimiter=',') | |
#2017-07-05,1499317099,45,348,0,1499206440,1499230020 | |
next(data) #skip header | |
for line in data: | |
deep_sleep=int(line[2]) | |
light_sleep=int(line[3]) | |
timestamp=int(line[5]) | |
ts_to=int(line[6]) | |
#deep sleep | |
#timestamp=ts_from | |
for i in range(0,deep_sleep): | |
w["timestamp"]=timestamp | |
r=cursor.execute("SELECT * from MI_BAND_ACTIVITY_SAMPLE where TIMESTAMP=$timestamp", (w )).fetchone() | |
if r: | |
#print("record exists", r,line[2]) | |
pass | |
else: | |
heart_rate=hr.get(timestamp,255) | |
print("inserting sleep", timestamp) | |
steps=0 | |
raw_kind=122 | |
raw_intensity=random.choice([0,0,0,0,20,0,0,2,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) | |
cursor.execute("INSERT INTO MI_BAND_ACTIVITY_SAMPLE VALUES (?,?,?,?,?,?,?)",(timestamp,device_id,user_id,raw_intensity,steps,raw_kind,heart_rate,)) | |
timestamp=timestamp + 60 | |
for i in range(0,light_sleep): | |
w["timestamp"]=timestamp | |
r=cursor.execute("SELECT * from MI_BAND_ACTIVITY_SAMPLE where TIMESTAMP=$timestamp", (w )).fetchone() | |
if r: | |
#print("record exists", r,line[2]) | |
pass | |
else: | |
heart_rate=hr.get(timestamp,255) | |
print("inserting sleep", timestamp) | |
steps=0 | |
raw_kind=112 | |
raw_intensity=random.choice([0,0,0,0,20,0,0,2,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) | |
cursor.execute("INSERT INTO MI_BAND_ACTIVITY_SAMPLE VALUES (?,?,?,?,?,?,?)",(timestamp,device_id,user_id,raw_intensity,steps,raw_kind,heart_rate,)) | |
timestamp=timestamp + 60 | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment