@requires('jawboneup.access_token')
def sleeps(gauge_factory, config, logger):
    gauge = gauge_factory('jawbone.sleeps')
    access_token = config['jawboneup.access_token']

    headers = {'Authorization' : 'Bearer {0}'.format(access_token)}
    r = requests.get('https://jawbone.com/nudge/api/users/@me/sleeps',
                     headers=headers)

    for i in range(DAYS_BACK):
        day = today_utc() - timedelta(days=i)
        resp = r.json()
        sleeps = resp['data']['items']
        day_date = day.date()
        today_fmt = int(day_date.strftime('%Y%m%d'))
        today_sleeps = filter(lambda s: s['date'] == today_fmt, sleeps)
        
        duration = sum([s['details']['duration'] - s['details']['awake']
                       for s in today_sleeps]) / 60.0 # in minutes
        if duration == 0:
            logger.info('Sleeps not found on {0}, not saving.'.format(day_date))
        else:
            gauge.save(day, duration)
            logger.info('Saved {0}min sleep on {1}'.format(duration, day_date))
dataset/ETHPy150Open ahmetalpbalkan/personal-dashboard/tasks/jawboneup.py/sleeps/original