<commit_before>#!/usr/bin/env python
"""
This is an example script to dump the fitbit data for the previous day.
This can be set up in a cronjob to dump data daily.

Create a config file at ~/.fitbit.conf with the following:

[fitbit]
user_id: 12XXX 
sid: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
uid: 123456
uis: XXX%3D
dump_dir: ~/Dropbox/fitbit
"""
import time
import os
import ConfigParser

import fitbit

CONFIG = ConfigParser.ConfigParser()
CONFIG.read(["fitbit.conf", os.path.expanduser("~/.fitbit.conf")])

DUMP_DIR=os.path.expanduser(CONFIG.get('fitbit', 'dump_dir'))

def client():
    return fitbit.Client(CONFIG.get('fitbit', 'user_id'), CONFIG.get('fitbit', 'sid'), CONFIG.get('fitbit', 'uid'), CONFIG.get('fitbit', 'uis'))

def dump_to_str(data):
    return "\n".join(["%s,%s" % (str(ts), v) for ts, v in data])

def dump_to_file(data_type, date, data):
    directory = "%s/%s" % (DUMP_DIR, data_type)
    if not os.path.isdir(directory):
        os.makedirs(directory)
    with open("%s/%s.csv" % (directory, str(date)), "w") as f:
        f.write(dump_to_str(data))

def dump_day(date):
    c = client()

    dump_to_file("steps", date, c.intraday_steps(date))
    time.sleep(5)    
    dump_to_file("calories", date, c.intraday_calories_burned(date))
    time.sleep(5)
    dump_to_file("active_score", date, c.intraday_active_score(date))
    time.sleep(5)
    dump_to_file("sleep", date, c.intraday_sleep(date))
    time.sleep(5)

if __name__ == '__main__':
    #import logging
    #logging.basicConfig(level=logging.DEBUG)
    import datetime
    dump_day((datetime.datetime.now().date() - datetime.timedelta(days=1)))<commit_msg>Accept username and password on the command line. Dump as much data as we can.<commit_after>#!/usr/bin/env python
"""
This is an example script to dump all available fitbit data.
This can be set up in a cronjob to dump data daily.

Run with the following parameters:
$ python dump.py <email> <password> <directory>
"""
import datetime
import os
import sys
import time

import fitbit

EMAIL=sys.argv[1]
PASSWORD=sys.argv[2]
DUMP_DIR=sys.argv[3]

def dump_to_str(data):
    return "\n".join(["%s,%s" % (str(ts), v) for ts, v in data])

def dump_to_file(data_type, date, data):
    directory = "%s/%i/%s" % (DUMP_DIR, date.year, date)
    if not os.path.isdir(directory):
        os.makedirs(directory)
    with open("%s/%s.csv" % (directory, data_type), "w") as f:
        f.write(dump_to_str(data))
    time.sleep(1)

def previously_dumped(date):
    return os.path.isdir("%s/%i/%s" % (DUMP_DIR, date.year, date))

def dump_day(c, date):
    steps = c.intraday_steps(date)
    # Assume that if no steps were recorded then there is no data
    if sum([s[1] for s in steps]) == 0:
        return False

    dump_to_file("steps", date, steps)
    dump_to_file("calories", date, c.intraday_calories_burned(date))
    dump_to_file("active_score", date, c.intraday_active_score(date))
    dump_to_file("sleep", date, c.intraday_sleep(date))

    return True

if __name__ == '__main__':
    #import logging
    #logging.basicConfig(level=logging.DEBUG)
    client = fitbit.Client.login(EMAIL, PASSWORD)

    date = datetime.date.today()

    # Look for the most recent sync
    while (datetime.date.today() - date).days < 365:
        r = dump_day(client, date)
        date -= datetime.timedelta(days=1)
        if r:
            break

    if (datetime.date.today() - date).days > 365:
        # No sync in the last year.
        sys.exit(1)

    while not previously_dumped(date):
        r = dump_day(client, date)
        date -= datetime.timedelta(days=1)
        if not r:
            break

    # Always redump the last dumped day because we may have dumped it before the day was finished.
    dump_day(client, date)
{'fraction_non_alphanumeric': 0.14012230789683594, 'fraction_numerical': 0.007710715235309758, 'mean_word_length': 3.6159509202453988, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5783968', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1314, 'n_words': 346}