<jupyter_start><jupyter_text>Get data from Dexcom share<jupyter_code>%pylab inline
import os<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>Dev functions to fetch CGM glucose values<jupyter_code>import json
import requests
import pandas as pd
import datetime

DEFAULTS = {'applicationid': 'd89443d2-327c-4a6f-89e5-496bbb0317db',
        'agent': "Dexcom Share/3.0.2.11 CFNetwork/711.2.23 Darwin/14.0.0",
        'login_url': 'https://share1.dexcom.com/ShareWebServices/Services/General/LoginPublisherAccountByName',
        'latest_url': 'https://share1.dexcom.com/ShareWebServices/Services/Publisher/ReadPublisherLatestGlucoseValues',
        'accept': 'application/json',
        'content-type': 'application/json'}

#DIRECTIONS = {'NONE': 0, 'DoubleUp': 1, 'SingleUp': 2, 'FortyFiveUp': 3,
#              'Flat': 4, 'FortyFiveDown': 5, 'SingleDown': 6, 'DoubleDown': 7,
#              'NOT COMPUTABLE': 8, 'RATE OUT OF RANGE': 9}

def authorize(account_name, account_password):
    """
    Login to server
    
    Returns
    -------
    sessionid: str
        Session ID from the server
    """
    url = DEFAULTS['login_url']
    body = {'password': <PASSWORD>, 'applicationId': DEFAULTS['applicationid'], 'accountName': account_name}
    headers = {'User-Agent': DEFAULTS['agent'], 'Content-Type': DEFAULTS['content-type'],
               'Accept': DEFAULTS['accept']}
    
    response = requests.post(url, headers=headers, json=body)
    
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as e:
        print "HTTPError:", e.message
        pass
    
    return response.content[1:-1]

def fetch(sessionid, minutes=1440, maxcount=1):
    """
    Get data from server
    """
    url = DEFAULTS['latest_url']
    url += '?sessionID={:}&minutes={:}&maxCount={:}'\
        .format(sessionid, minutes, maxcount)
    headers = {'User-Agent': DEFAULTS['agent'],
                'Content-Type': DEFAULTS['content-type'],
                'Content-Length': 0, 'Accept': DEFAULTS['accept']}
    
    response = requests.post(url, headers=headers)
    
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as e:
        print "HTTPError:", e.message
        pass

    return json.loads(response.content)

def dex2pandas(dex):
    """
    Convert Dexcom server entries to a Pandas dataframe
    """
    date = [datetime.datetime.fromtimestamp(float(d['WT'][6:-2])/1000.) for d in dex]
    value = [d['Value'] for d in dex]
    trend = [d['Trend'] for d in dex]
    
    df = pd.DataFrame({'datetime': date, 'value': value, 'trend': trend})
    df['device'] = 'share2'
    df['type'] = 'cgm'
    
    return df
# user options (set here or set environment variables before launching notebook)
SHARE_ACCOUNT_NAME=os.environ['SHARE_ACCOUNT_NAME']
SHARE_PASSWORD=os.environ['SHARE_PASSWORD']

sid = authorize(SHARE_ACCOUNT_NAME, SHARE_PASSWORD)

dex = fetch(sid, maxcount=1000)

df = dex2pandas(dex)

df.plot(x='datetime', y='value')
df.hist(column='value')

df.describe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14483424525265529, 'fraction_numerical': 0.022207917605407145, 'mean_word_length': 3.537226277372263, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '475568', 'license': 'MIT', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1024, 'n_words': 222, 'path': 'fubetes/dev/dev_dexcom_api.ipynb', 'repo_name': 'esker1/fubetes', 'size': '3113'}