Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise getdata.py #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions getdata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import requests, json, arrow, hashlib, urllib, datetime
from secret import USERNAME, PASSWORD, NS_URL, NS_SECRET

Expand All @@ -19,7 +20,7 @@ def get_entries(login):
auth_code = login.json()['token']
print("Loading entries...")
entries = requests.post('https://analytics.diabetes-m.com/api/v1/diary/entries/list',
cookies=login.cookies,
cookies=login.cookies,
headers={
'origin': 'https://analytics.diabetes-m.com',
'authorization': 'Bearer '+auth_code
Expand All @@ -37,26 +38,54 @@ def to_mgdl(mmol):

def convert_nightscout(entries, start_time=None):
out = []

for entry in entries:
bolus = entry["carb_bolus"] + entry["correction_bolus"]
time = arrow.get(int(entry["entry_time"])/1000).to(entry["timezone"])

#Fill notes varable
notes = entry["notes"]

#save notes data to a variable used to skip uploads from nightscoute
noteskip = entry["notes"]

# Check if there is any data for basal, proteins or fats and if notes are empty
if (entry["basal"]+ entry["proteins"]+ entry["fats"]) != 0 and len(notes)!=0:

#Build the string of additional data to be pushed into notes field on nightscout if notes there are notes
notes = "%s Basal: %s Proteins: %s Fats: %s" % (entry["notes"], entry["basal"], entry["proteins"], entry["fats"])

# Check if there is any data for basal, proteins or fats and if notes are not empty
if (entry["basal"]+ entry["proteins"]+ entry["fats"]) != 0 and len(notes) == 0:

#Build the string of additional data to be pushed into notes field on nightscout of there are no notes
notes = "Basal: %s Proteins: %s Fats: %s" % (entry["basal"], entry["proteins"], entry["fats"])

if start_time and start_time >= time:

continue

author = NS_AUTHOR

# if from nightscout, skip
if noteskip == "[Nightscout]":
continue

# You can do some custom processing here, if necessary

dat = {
"eventType": "Meal Bolus",
"created_at": time.format(),
"carbs": entry["carbs"],
"fats": entry["fats"],
"proteins": entry["proteins"],
"basal": entry["basal"],
"insulin": bolus,
"notes": notes,
"enteredBy": author
}


if entry["glucose"]:
bgEvent = {
"eventType": "BG Check",
Expand All @@ -71,13 +100,13 @@ def convert_nightscout(entries, start_time=None):
# this is due to a UI display issue with Nightscout (it will show mg/dL units always for
# bg-only readings, but convert to the NS default unit otherwise)
if unit_mmol and not (entry["carbs"] or bolus):
bgEvent["units"] = "mmol"
# save the mmol/L value from DB-M
bgEvent["glucose"] = entry["glucose"]
else:
bgEvent["units"] = "mg/dL"
# convert mmol/L -> mg/dL
bgEvent["glucose"] = to_mgdl(entry["glucose"])
else:
bgEvent["units"] = "mmol"
# save the mmol/L value from DB-M
bgEvent["glucose"] = entry["glucose"]

dat.update(bgEvent)

Expand Down Expand Up @@ -126,4 +155,3 @@ def main():

if __name__ == '__main__':
main()