Code Monkey home page Code Monkey logo

withingsapi_archive's Introduction

Withings Web App

This is a web-app to host API requests for Withings data. A large amount of inspiration/initial learnings came from from Miguel Grinberg's microblog api - so if anything makes absolutely zero sense, check there for a likely better explanation. The code to call the data is simple enough, but doing this often or in bulk is cumbersome. Thus, this app provides a very simple GUI & database structure that can make things a little easier (though a nicer version is in the works).

In terms of simply getting data through the Withings API, most of the code you'd be interested in can be found in app/routes.py & app/tasks.py. Documentation for the Withings API can be found at https://developer.withings.com/api-reference.

This was my first genuine dive into Python programming, so my humblest apologies for the state of the code. I have learnt a lot since this project was started, and I cringe looking back at some of code - but it does work, so I don't cringe too hard.
That being said, please reach out if you have any questions or problems.

Setup

This project requires a few things to get working. Make sure to check and update the config.py where necessary - this should be intuitive while following the below steps.

Python Script

This was created in Python v3.7. Project requirements can be installed using
pip install -r requirements.txt

Withings App

A Withings App must be registered on the Withings Public Cloud at https://developer.withings.com/dashboard/welcome. This will provide you with a Client ID and Client/Customer Secret, which will need to be included in config.py or as an environment variable.

You will also have to define a Callback URL ('withings_CALLBACK_URI' in config.py). This is where authentication requests will be redirected. Importantly: If you redirect to localhost, you will be limited to only 10 linked users. In theory you can unlink these before requesting more, ad infinitum, but in practise I had trouble getting this to work in any convenient way.

Web Server

Frustratingly, there is only a very small component of this project that demands an online component.
Specifically, to avoid the above problem of being limited to 10 users, the callback URL must be hosted at a secure endpoint (i.e., https://...).

This app uses the free package of the web-hosting service Heroku, though this can be done with any hosting service.
Although inefficient, the simplest setup is to clone this entire project and host it both locally and online. Heroku is configured using the Procfile, see microblog-api for more information.

The only code that requires being online is in app/routes.py, found below:

@app.route("/get_token", methods = ['GET', 'POST'])
def get_token():
    code = request.args.get('code')
    state = request.args.get('state')

    parameters = dict(code=code, state=state)
    from app import ip
    local_url = ('http://localhost:5000/save_token')

    redirect_url = local_url + ('?' + urlencode(parameters) if parameters else "")

    return redirect(redirect_url)

In sum; all this is doing is redirecting the code that has been included in the url back to localhost.
The rest of the web interface was simply out of convenience and to begin prototyping for a proper research dashboard, which is under development.

SQL Database

This project uses a simple SQL database package (Flask-SQLalchemy to track linked participants/studies/app-users. Changing this database properly (i.e., without deleting entries) is done with the Flask-Migrate package. Each package has good documentation, but the microblog-api has a great section on getting this set up.

That being said you should be able to create/change the database by just running the following.
This includes creation of an admin account that will be required if using the web app as-is.

set FLASK_APP=app.py
flask db init

python
>>> from app import db
>>> db.create_all()
>>>
>>> from app.models import User

>>> # The below is adding an admin user which you'll need for the web-app
>>> u = User(email='[email protected]', admin=True, confirmed=True) 
>>> User.set_password(u, 'admin')
>>> db.session.add(u)
>>> db.session.commit()


# And if you need to upgrade/change the database at any time...

# flask db migrate
# flask db upgrade

Alternatively, this can all be done on other database software, including excel/csv. A proper database structure was, again, largely done for prototyping/learning purposes.

Using the App

To run the app, once everything is installed, simply run the app.py script using flask run.

You should then be able to view the interface in your web-browser at http://localhost:5000/.
If you need to login, use the credentials that you added previously (email='[email protected]', password='admin').
Most of the functionality of interest is in the 'Withings' dropdown menu.

Note-1: You'll have to add a study first - this is just a categorization of participants. This is linked to an account - just use the attached admin account.
Note-2: Downloads can be found in app/static/
Note-3: Downloads are in .json format. You can open these in any text editor, though I'd recommend something like https://jsonformatter.org/json-pretty-print to make it more readable. Alternatively you can can use code to convert them to a more palatable format like .csv or .excel - let me know if you need help with this.

withingsapi_archive's People

Contributors

jackmanners avatar

Stargazers

Russell Jarvis avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.