Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typos #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pipenv install git+https://github.com/sladkovm/stravaio.git#egg=stravaio
```

## Before use
You need `STRAVA_ACCESS_TOKEN` with activity level permissions to make use of this package.
You need `STRAVA_ACCESS_TOKEN` with activity level permissions to make use of this package.

The easiest way to get the token is to use the *stravaio* library itself:

Expand All @@ -35,13 +35,13 @@ from stravaio import strava_oauth2
strava_oauth2(client_id=STRAVA_CLIENT_ID, client_secret=STRAVA_CLIENT_SECRET)
```

You will be redirected to the default system browser, where the authorization flow must be completed. In the background the local webserver will be running and listening to the data returned by Strava.
You will be redirected to the default system browser, where the authorization flow must be completed. In the background the local web server will be running and listening to the data returned by Strava.

Both `STRAVA_CLIENT_ID` and `STRAVA_CLIENT_SECRET` are optional arguments if they are set as the environment variables.

Another way is to head to the [strava-oauth](https://github.com/sladkovm/strava-oauth) library for help. There you will find a link to the public webserver that can be used for completing the Strava authorizatio flow.
Another way is to head to the [strava-oauth](https://github.com/sladkovm/strava-oauth) library for help. There you will find a link to the public web server that can be used for completing the Strava authorization flow.

When the token is fetched it is handy to store it as an environment variable. Otherwise it should be passed explicitely to the StravaIO constructor.
When the token is fetched it is handy to store it as an environment variable. Otherwise it should be passed explicitly to the StravaIO constructor.

```bash
export STRAVA_ACCESS_TOKEN=<strava_access_token>
Expand All @@ -52,7 +52,7 @@ export STRAVA_ACCESS_TOKEN=<strava_access_token>
```python
from stravaio import StravaIO

# If the token is stored as an environment varible it is not neccessary
# If the token is stored as an environment variable it is not necessary
# to pass it as an input parameters
client = StravaIO(access_token=STRAVA_ACCESS_TOKEN)
```
Expand All @@ -68,7 +68,7 @@ athlete = client.get_logged_in_athlete()
# Dump athlete into a JSON friendly dict (e.g. all datetimes are converted into iso8601)
athlete_dict = athlete.to_dict()

# Store athlete infor as a JSON locally (~/.stravadata/athlete_<id>.json)
# Store athlete info as a JSON locally (~/.stravadata/athlete_<id>.json)
athlete.store_locally()

# Get locally stored athletes (returns a generator of dicts)
Expand All @@ -79,7 +79,7 @@ local_athletes = client.local_athletes()

### Activities
```python
# Returns a stravaio.Activity object that wraps the
# Returns a stravaio.Activity object that wraps the
# [Strava DetailedActivity](https://developers.strava.com/docs/reference/#api-models-DetailedActivity)
activity = client.get_activity_by_id(2033203247)

Expand All @@ -105,7 +105,7 @@ activities = client.local_activities(athlete_id=1202065)

### Streams
```python
# Returns a stravaio.Streams object that wraps the
# Returns a stravaio.Streams object that wraps the
# [Strava StreamSet](https://developers.strava.com/docs/reference/#api-models-StreamSet)
streams = client.get_activity_streams(2033203247)

Expand Down
59 changes: 31 additions & 28 deletions stravaio.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, access_token=None):

def get_logged_in_athlete(self):
"""Get logged in athlete

Returns
-------
athlete: Athlete object
Expand All @@ -39,18 +39,18 @@ def get_logged_in_athlete(self):
rv = Athlete(self.athletes_api.get_logged_in_athlete())
except ApiException as e:
logger.error(f""""
Error in strava_swagger_client.AthletesApi!
Error in strava_swagger_client.AthletesApi!
STRAVA_ACCESS_TOKEN is likely out of date!
Check the https://github.com/sladkovm/strava-oauth for help.
Returning None.
Original Error:
{e}""")
rv = None
return rv
return rv

def local_athletes(self):
"""List local athletes

Returns
-------
athletes: generator of JSON friendly dicts
Expand All @@ -69,7 +69,7 @@ def get_activity_by_id(self, id, include_all_efforts=False):
activity_id
include_all_efforts: bool (default=False)
Include all segment efforts in the response

Returns
-------
activity: Activity ojbect
Expand All @@ -78,7 +78,7 @@ def get_activity_by_id(self, id, include_all_efforts=False):

def get_logged_in_athlete_activities(self, after=0, list_activities=None):
"""List all activities after a given date

Parameters
----------
after: int, str or datetime object
Expand All @@ -96,21 +96,21 @@ def get_logged_in_athlete_activities(self, after=0, list_activities=None):
after = date_to_epoch(after)
_fetched = self.activities_api.get_logged_in_athlete_activities(after=after)
if len(_fetched) > 0:
print(f"Fetched {len(_fetched)}, the latests is on {_fetched[-1].start_date}")
print(f"Fetched {len(_fetched)}, the latest is on {_fetched[-1].start_date}")
list_activities.extend(_fetched)
if len(_fetched) == 30:
last_after = list_activities[-1].start_date
return self.get_logged_in_athlete_activities(after=last_after, list_activities=list_activities)
else:
print("empty list")

return list_activities



def local_activities(self, athlete_id):
"""List local activities

Parameters
----------
athlete_id: int
Expand All @@ -127,7 +127,7 @@ def local_activities(self, athlete_id):

def local_streams(self, athlete_id):
"""List local streams

Parameters
----------
athlete_id: int
Expand All @@ -143,19 +143,19 @@ def local_streams(self, athlete_id):

def get_activity_streams(self, id, athlete_id, local=True):
"""Get activity streams by ID

Parameters
----------
id: int
activity_id
athlete_id: int
athlete_id
local: bool (default=True)
if the streams is already storred, return the local version
if the Streams object is already stored, return the local version

Returns
-------
streams: Streams ojbect (remote) or pd.Dataframe (local)
streams: Streams object (remote) or pd.Dataframe (local)
"""
if local:
dir_streams = os.path.join(dir_stravadata(), f"streams_{athlete_id}")
Expand Down Expand Up @@ -254,7 +254,7 @@ def to_dict(self):
latlng = r.pop('latlng')
_r = list(zip(*latlng))
r.update({'lat': list(_r[0])})
r.update({'lng': list(_r[1])})
r.update({'lng': list(_r[1])})
return r

def store_locally(self):
Expand Down Expand Up @@ -301,7 +301,7 @@ def grade_smooth(self):
@property
def moving(self):
return self._get_stream_by_name('moving')

@property
def lat(self):
return self._get_stream_by_name('lat')
Expand All @@ -312,10 +312,10 @@ def lng(self):


def _get_stream_by_name(self, key):

if key not in self.ACCEPTED_KEYS:
raise KeyError(f"key must be one of {self.ACCEPTED_KEYS}")

try:
rv = self.to_dict()[key]
except KeyError:
Expand All @@ -325,15 +325,18 @@ def _get_stream_by_name(self, key):


def strava_oauth2(client_id=None, client_secret=None):
"""Run strava authorization flow. This function will open a default system
browser alongside starting a local webserver. The authorization procedure will be completed in the browser.
"""Run strava authorization flow.

This function will open a default system browser alongside starting a local
web server. The authorization procedure will be completed in the browser.

The access token will be returned in the browser in the format ready to copy
to the .env file.

The access token will be returned in the browser in the format ready to copy to the .env file.

Parameters:
-----------
client_id: int, if not provided will be retrieved from the STRAVA_CLIENT_ID env viriable
client_secret: str, if not provided will be retrieved from the STRAVA_CLIENT_SECRET env viriable
client_id: int, if not provided will be retrieved from the STRAVA_CLIENT_ID env variable
client_secret: str, if not provided will be retrieved from the STRAVA_CLIENT_SECRET env variable
"""
if client_id is None:
client_id = os.getenv('STRAVA_CLIENT_ID', None)
Expand All @@ -343,7 +346,7 @@ def strava_oauth2(client_id=None, client_secret=None):
client_secret = os.getenv('STRAVA_CLIENT_SECRET', None)
if client_secret is None:
raise ValueError('client_secret is None')

port = 8000
_request_strava_authorize(client_id, port)

Expand Down Expand Up @@ -392,9 +395,9 @@ def run_server_and_wait_for_token(port, client_id, client_secret):

request = request_bytes.decode('utf-8')
status_line = request.split('\n', 1)[0]

method, raw_url, protocol_version = status_line.split(' ')

url = urllib.parse.urlparse(raw_url)
query_string = url.query
query_params = urllib.parse.parse_qs(query_string, keep_blank_values=True)
Expand All @@ -413,7 +416,7 @@ def run_server_and_wait_for_token(port, client_id, client_secret):
logger.debug(f"Authorized athlete: {data.get('access_token', 'Oeps something went wrong!')}")
else:
data = url.path.encode()

return data


Expand Down