python-fitbit

repository·master·Indexed 20 days ago

https://github.com/orcasgit/python-fitbit

A Python client implementation for the Fitbit API. It provides the fitbit.Fitbit class to programmatically interact with Fitbit services, supporting both unauthorized interfaces for public data and OAuth 2.0 authorization for private user data. The library allows for retrieving health and activity data, deleting logs, and managing API parameter conventions for user IDs and dates.

Tokens
1.3K
Snippets
7
Records
9
Agent score
21%

What's inside python-fitbit

  1. Understand Fitbit API parameter conventions

    master

    When calling methods on the fitbit.Fitbit class, keep the following parameter behaviors in mind:

    • user_id=None: If set to None, the library assumes the current user associated with the provided credentials and passes a '-' through to the Fitbit API.
    • date=None: This parameter accepts None, a date object, a datetime object (any object with a proper strftime method), or a string formatted as %Y-%m-%d.
  2. Install python-fitbit runtime requirements

    master

    To use the library in your project, install the base runtime requirements using the provided requirements file. This ensures all necessary dependencies like python-dateutil and requests-oauthlib are present.

    sudo pip install -r requirements/base.txt
  3. Install developer requirements for python-fitbit

    master

    If you intend to modify the library or run its internal tests, install the developer requirements. This includes tools like Sphinx for documentation and tox for testing.

    sudo pip install -r requirements/dev.txt
  4. Authorize with OAuth 2.0

    master

    To access private user data, you must provide an access_token and a refresh_token when initializing the fitbit.Fitbit client. You can obtain these tokens via the OAuth 2.0 flow (e.g., using the provided ./gather_keys_oauth2.py script).

    # You'll have to gather the tokens on your own, or use
    # ./gather_keys_oauth2.py
    authd_client = fitbit.Fitbit('<consumer_key>', '<consumer_secret>',
                                 access_token='<access_token>', refresh_token='<refresh_token>')
    authd_client.sleep()
  5. Use the unauthorized interface for public data

    master

    If you only need to retrieve data that does not require user authorization (public data), you can instantiate a fitbit.Fitbit client using only your consumer credentials. This interface allows access to certain methods like food_units() without requiring access or refresh tokens.

    import fitbit
    unauth_client = fitbit.Fitbit('<consumer_key>', '<consumer_secret>')
    # certain methods do not require user keys
    unauth_client.food_units()
  6. Delete Fitbit logs

    master

    You can delete specific logs by providing their unique log_id to the corresponding delete methods.

    client.delete_body(log_id)
    client.delete_activities(log_id)
    client.delete_foods_log(log_id)
    client.delete_foods_log_water(log_id)
    client.delete_sleep(log_id)
    client.delete_heart(log_id)
    client.delete_bp(log_id)
  7. Retrieve Fitbit user data

    master

    The fitbit.Fitbit class provides several methods to retrieve different types of health and activity data. Most retrieval methods accept date=None and user_id=None parameters.

    # Example retrieval methods
    client.body(date=None, user_id=None, data=None)
    client.activities(date=None, user_id=None, data=None)
    client.foods_log(date=None, user_id=None, data=None)
    client.foods_log_water(date=None, user_id=None, data=None)
    client.sleep(date=None, user_id=None, data=None)
    client.heart(date=None, user_id=None, data=None)
    client.bp(date=None, user_id=None, data=None)
    
    # Food-specific retrieval
    client.recent_foods(user_id=None, qualifier='')
    client.frequent_foods(user_id=None, qualifier='')
    client.favorite_foods(user_id=None, qualifier='')
    
    # Activity-specific retrieval
    client.recent_activities(user_id=None, qualifier='')
    client.frequent_activities(user_id=None, qualifier='')
    client.favorite_activities(user_id=None, qualifier='')
  8. Python-fitbit system requirements

    master

    The following dependencies and environments are required for python-fitbit:

    • Python: 2.7 or higher
    • Core Dependencies:
      • python-dateutil
      • requests-oauthlib
    • Development/Testing Tools (optional/conditional):
      • Sphinx (for documentation generation)
      • tox (for running tests)
      • coverage (for test coverage reports)