From ca1a5ffbf822a55c9d0537b6fec7ea88e6bf43e8 Mon Sep 17 00:00:00 2001 From: Anton Sarukhanov <code@ant.sr> Date: Fri, 4 Nov 2016 17:36:28 -0400 Subject: [PATCH] Minimal support for fetching and storing activities from Strava. --- api/app.py | 3 ++- api/models.py | 11 ++++++++ api/strava.py | 22 ++++++++++++---- migrations/versions/dcca58ee34e4_.py | 39 ++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 migrations/versions/dcca58ee34e4_.py diff --git a/api/app.py b/api/app.py index 59bfffd..df6d276 100644 --- a/api/app.py +++ b/api/app.py @@ -33,12 +33,13 @@ def api_strava(): s = Strava() r = app.make_response(json.dumps([{ 'name': a.name, + 'description': a.description, 'type': a.type, 'moving_time': a.moving_time.seconds, 'date': str(a.start_date), 'distance': unithelper.miles(a.distance).num, 'athlete_id': a.athlete.id, - 'url': 'https://strava.com/activity/{}'.format(a.id) + 'url': 'https://strava.com/activities/{}'.format(a.id) } for a in s.activities])) r.mimetype = 'application/json' return r diff --git a/api/models.py b/api/models.py index 7922f80..faf3eed 100644 --- a/api/models.py +++ b/api/models.py @@ -6,6 +6,17 @@ from flask import current_app db = SQLAlchemy() +class Activity(db.Model): + """Athletic activity""" + __tablename__ = "activity" + id = db.Column(db.Integer, primary_key=True) + strava_id = db.Column(db.Integer, index=True) + type = db.Column(db.Text, index=True) + name = db.Column(db.String) + description = db.Column(db.Text) + moving_time = db.Column(db.Interval) + distance = db.Column(db.Float) + class StravaApiToken(db.Model): """API token for Strava""" __tablename__ = "strava_api_token" diff --git a/api/strava.py b/api/strava.py index 2fb908d..974df5a 100644 --- a/api/strava.py +++ b/api/strava.py @@ -2,7 +2,7 @@ from flask import request, redirect, current_app from stravalib.client import Client from requests.exceptions import HTTPError from sqlalchemy.orm.exc import NoResultFound -from api.models import db, StravaApiToken +from api.models import db, Activity, StravaApiToken class Strava(Client): """Wrapper for stavalib Client, which is a Strava API client. @@ -60,7 +60,19 @@ class Strava(Client): db.session.commit() return redirect(request.referrer) if request.referrer else "Deleted Strava auth token." - @property - def activities(self): - """Get list of activities from Strava API.""" - return self.get_activities() + def fetch_activities(self): + """Get list of activities from Strava and store them.""" + db_activities = [a.strava_id for a in db.session.query(Activity.strava_id).all()] + activities = self.get_activities() + for a in activities: + if a.id in db_activities: + continue + activity = self.get_activity(a.id) + db.session.add(Activity( + strava_id=activity.id, + name=activity.name, + description=activity.description, + type=activity.type, + moving_time=activity.moving_time, + distance=activity.distance.get_num())) + db.session.commit() diff --git a/migrations/versions/dcca58ee34e4_.py b/migrations/versions/dcca58ee34e4_.py new file mode 100644 index 0000000..72ed893 --- /dev/null +++ b/migrations/versions/dcca58ee34e4_.py @@ -0,0 +1,39 @@ +"""empty message + +Revision ID: dcca58ee34e4 +Revises: f640a1d27e31 +Create Date: 2016-11-04 16:18:52.190876 + +""" + +# revision identifiers, used by Alembic. +revision = 'dcca58ee34e4' +down_revision = 'f640a1d27e31' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_table('activity', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('strava_id', sa.Integer(), nullable=True), + sa.Column('type', sa.Text(), nullable=True), + sa.Column('name', sa.String(), nullable=True), + sa.Column('description', sa.Text(), nullable=True), + sa.Column('moving_time', sa.Interval(), nullable=True), + sa.Column('distance', sa.Float(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_activity_strava_id'), 'activity', ['strava_id'], unique=False) + op.create_index(op.f('ix_activity_type'), 'activity', ['type'], unique=False) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_activity_type'), table_name='activity') + op.drop_index(op.f('ix_activity_strava_id'), table_name='activity') + op.drop_table('activity') + ### end Alembic commands ### -- GitLab