diff --git a/.gitignore b/.gitignore index ba74660..b21d6f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,57 +1,3 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ *.py[cod] - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover - -# Translations -*.mo -*.pot - -# Django stuff: -*.log - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ +*.egg-info +venv diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..9d269f0 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +recursive-include gfit2mfp *.py diff --git a/gfit2mfp/__init__.py b/gfit2mfp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gfit2mfp/main.py b/gfit2mfp/main.py new file mode 100644 index 0000000..36d6fee --- /dev/null +++ b/gfit2mfp/main.py @@ -0,0 +1,54 @@ +from datetime import datetime, timedelta + +import httplib2 + +from googleapiclient.discovery import build +from oauth2client.file import Storage +from oauth2client.client import OAuth2WebServerFlow +from oauth2client.tools import run + +from . import settings + +FIT_URL_STRING = 'https://www.googleapis.com/fitness/v1/users/{userId}/dataSources/{dataSourceId}/datasets/{datasetId}' +DEV_ID = 'me' + +def login(): + storage = Storage('user_credentials') + credentials = storage.get() + + if credentials is None or credentials.invalid: + flow = OAuth2WebServerFlow( + settings.CLIENT_ID, + settings.CLIENT_SECRET, + settings.API_SCOPE + ) + credentials = run(flow, storage) + + http = credentials.authorize(httplib2.Http()) + api = build('fitness', 'v1', http=http) + return api + +def get_time_range_str(start, end): + # google accepts timestamps in nanoseconds + start = int(start.timestamp() * 1e9) + end = int(end.timestamp() * 1e9) + return '{s}-{e}'.format(s=start, e=end) + +def get_fit_data(): + end = datetime.now() + start = end - timedelta(days=5) + url = FIT_URL_STRING.format( + userId='me', + dataSourceId='derived:com.google.calories.expended:com.google.android.gms:from_activities', + datasetId=get_time_range_str(start, end) + ) + request = api.users(userId='me').dataSources().list() + + while request is not None: + response = request.execute() + import pdb + pdb.set_trace() + +if __name__ == '__main__': + api = login() + get_fit_data(api) diff --git a/gfit2mfp/settings.py b/gfit2mfp/settings.py new file mode 100644 index 0000000..e7fbdaa --- /dev/null +++ b/gfit2mfp/settings.py @@ -0,0 +1,4 @@ +CLIENT_ID = '' +CLIENT_SECRET = '' +API_SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read' + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..69287ba --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup, find_packages +setup( + name = 'gfit2mfp', + version='0.1', + description='Google Fit -> My Fitness Pal', + author='Leo Hemsted', + author_email='leohemsted@gmail.com', + url='https://github.com/leohemsted/gfit2mfp/', + packages = find_packages(), + install_requires=[ + 'requests>=2.7.0', + 'google-api-python-client>=1.4.0', + 'oauth2client>=1.4.6', + 'httplib2>=0.9.1', + ] +)