Monday, November 18, 2013

Extracting blood glucose data from ManageBGL

ManageBGL is a complete diabetes logging, sharing, coaching and PREDICTION system, that integrates data from multiple diabetic devicesin the cloud (Meters, CGMS and Pumps).  (http://www.managebgl.com/)

Task : Extract blood glucose data/log using ManageBGL REST API


URL Structure:

  • To obtain authentication token for future requests;
    https://www.ManageBGL.com/api/1.0/login?email=email&password=password
  • To extract data/log for specified range;
    https://www.ManageBGL.com/api/1.0/extract?token=token&start_date=start_date&end_date=end_date

Application:
First, import required modules:
 from datetime import datetime, timedelta
 from xmltodict import parse
 import urllib2, urllib

Define the method:
 BGL_URL = "https://www.managebgl.com/api/1.0"
 
 def extract_bloodglucose_data():
      # obtain the token
      token_response = urllib2.urlopen("%s/login.xml?email=%s&password=%s" % (BGL_URL, email, password))
      token_parse = parse(token_response)
     
      # check if it returns the token
      if token_parse.has_key('response') and token_parse['response']['result'] == '1':
           token = token_parse['response']['token']
 

Extracting logs:
      # define parameters
      yesterday = str(datetime.now().date() - timedelta(1))
      param = (
           BGL_URL,
           token,
           yesterday + urllib.quote_plus(" 00:00:00")
           yesterday + urllib.quote_plus(" 23:59:59")
           )
     
     # using XML format
     extract_response = urllib2.urlopen("%s/extract.xml?token=%s&start_date=%s&end_date=%s" % (param))
     extract_parse = parse(extract_response)
   
     # check if it returns logs
     if extract_parse.has_key('response') and extract_parse['response']['rows'] != '0':
          for log in extract_parse['response']['logs']:
               # Log type for the data needed;
               #      Insulin = 2
               #      hbalc = 21
               #      c_peptide = 42
               #      fasting_plasma_glucose_test = 43
               #      oral_glucose_tolerance_test = 44
               #      random_plasma_glucose_test = 45
               #      triglyceride = 46
               # 
               # Assigning the values:
               if log['logtype_id'] == '2':
                    insulin = log['value']
               elif log['logtype_id'] == '46':
                    triglyceride = log['value']
               ....


Cheers! :)



"Life is short use Python"








No comments:

Post a Comment