Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
api.ant.sr
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anton Sarukhanov
api.ant.sr
Commits
ca1a5ffb
Commit
ca1a5ffb
authored
8 years ago
by
Anton Sarukhanov
Browse files
Options
Downloads
Patches
Plain Diff
Minimal support for fetching and storing activities from Strava.
parent
140afe10
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/app.py
+2
-1
2 additions, 1 deletion
api/app.py
api/models.py
+11
-0
11 additions, 0 deletions
api/models.py
api/strava.py
+17
-5
17 additions, 5 deletions
api/strava.py
migrations/versions/dcca58ee34e4_.py
+39
-0
39 additions, 0 deletions
migrations/versions/dcca58ee34e4_.py
with
69 additions
and
6 deletions
api/app.py
+
2
−
1
View file @
ca1a5ffb
...
@@ -33,12 +33,13 @@ def api_strava():
...
@@ -33,12 +33,13 @@ def api_strava():
s
=
Strava
()
s
=
Strava
()
r
=
app
.
make_response
(
json
.
dumps
([{
r
=
app
.
make_response
(
json
.
dumps
([{
'
name
'
:
a
.
name
,
'
name
'
:
a
.
name
,
'
description
'
:
a
.
description
,
'
type
'
:
a
.
type
,
'
type
'
:
a
.
type
,
'
moving_time
'
:
a
.
moving_time
.
seconds
,
'
moving_time
'
:
a
.
moving_time
.
seconds
,
'
date
'
:
str
(
a
.
start_date
),
'
date
'
:
str
(
a
.
start_date
),
'
distance
'
:
unithelper
.
miles
(
a
.
distance
).
num
,
'
distance
'
:
unithelper
.
miles
(
a
.
distance
).
num
,
'
athlete_id
'
:
a
.
athlete
.
id
,
'
athlete_id
'
:
a
.
athlete
.
id
,
'
url
'
:
'
https://strava.com/activit
y
/{}
'
.
format
(
a
.
id
)
'
url
'
:
'
https://strava.com/activit
ies
/{}
'
.
format
(
a
.
id
)
}
for
a
in
s
.
activities
]))
}
for
a
in
s
.
activities
]))
r
.
mimetype
=
'
application/json
'
r
.
mimetype
=
'
application/json
'
return
r
return
r
...
...
This diff is collapsed.
Click to expand it.
api/models.py
+
11
−
0
View file @
ca1a5ffb
...
@@ -6,6 +6,17 @@ from flask import current_app
...
@@ -6,6 +6,17 @@ from flask import current_app
db
=
SQLAlchemy
()
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
):
class
StravaApiToken
(
db
.
Model
):
"""
API token for Strava
"""
"""
API token for Strava
"""
__tablename__
=
"
strava_api_token
"
__tablename__
=
"
strava_api_token
"
...
...
This diff is collapsed.
Click to expand it.
api/strava.py
+
17
−
5
View file @
ca1a5ffb
...
@@ -2,7 +2,7 @@ from flask import request, redirect, current_app
...
@@ -2,7 +2,7 @@ from flask import request, redirect, current_app
from
stravalib.client
import
Client
from
stravalib.client
import
Client
from
requests.exceptions
import
HTTPError
from
requests.exceptions
import
HTTPError
from
sqlalchemy.orm.exc
import
NoResultFound
from
sqlalchemy.orm.exc
import
NoResultFound
from
api.models
import
db
,
StravaApiToken
from
api.models
import
db
,
Activity
,
StravaApiToken
class
Strava
(
Client
):
class
Strava
(
Client
):
"""
Wrapper for stavalib Client, which is a Strava API client.
"""
Wrapper for stavalib Client, which is a Strava API client.
...
@@ -60,7 +60,19 @@ class Strava(Client):
...
@@ -60,7 +60,19 @@ class Strava(Client):
db
.
session
.
commit
()
db
.
session
.
commit
()
return
redirect
(
request
.
referrer
)
if
request
.
referrer
else
"
Deleted Strava auth token.
"
return
redirect
(
request
.
referrer
)
if
request
.
referrer
else
"
Deleted Strava auth token.
"
@property
def
fetch_activities
(
self
):
def
activities
(
self
):
"""
Get list of activities from Strava and store them.
"""
"""
Get list of activities from Strava API.
"""
db_activities
=
[
a
.
strava_id
for
a
in
db
.
session
.
query
(
Activity
.
strava_id
).
all
()]
return
self
.
get_activities
()
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
()
This diff is collapsed.
Click to expand it.
migrations/versions/dcca58ee34e4_.py
0 → 100644
+
39
−
0
View file @
ca1a5ffb
"""
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 ###
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment