Skip to content

Commit

Permalink
Upgrade stravalib from 1.2 to 2.1
Browse files Browse the repository at this point in the history
Upgrade `stravalib` from version 1.2 to 2.1 and refactor the code accordingly.

* **requirements.txt**
  - Update `stravalib` version to `2.1.0`.

* **freezing/sync/data/activity.py**
  - Update import statement for `Activity` to `DetailedActivity`.
  - Replace `Activity` with `DetailedActivity` in `update_ride_basic`, `write_ride_efforts`, `write_ride_photo_primary`, `update_ride_complete`, `check_activity`, `list_rides`, and `write_ride` methods.

* **freezing/sync/data/streams.py**
  - Update import statement for `Stream` to `StreamSet`.
  - Replace `Stream` with `StreamSet` in `write_ride_streams` method.

* **freezing/sync/utils/cache.py**
  - Update import statement for `Activity` to `DetailedActivity`.
  - Replace `Activity` with `DetailedActivity` in `CachingActivityFetcher` class.

Resolves #64
Resolves #45

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/freezingsaddles/freezing-sync?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
obscurerichard committed Jan 1, 2025
1 parent 9a9058e commit 1eb7207
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
22 changes: 11 additions & 11 deletions freezing/sync/data/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sqlalchemy.orm import joinedload
from stravalib import unithelper
from stravalib.exc import AccessUnauthorized, Fault, ObjectNotFound
from stravalib.model import Activity, ActivityPhotoPrimary
from stravalib.model import DetailedActivity, ActivityPhotoPrimary

from freezing.sync.config import config, statsd
from freezing.sync.exc import (
Expand All @@ -29,7 +29,7 @@ class ActivitySync(BaseSync):
name = "sync-activity"
description = "Sync activities."

def update_ride_basic(self, strava_activity: Activity, ride: Ride):
def update_ride_basic(self, strava_activity: DetailedActivity, ride: Ride):
"""
Set basic ride properties from the Strava Activity object.
Expand Down Expand Up @@ -92,7 +92,7 @@ def update_ride_basic(self, strava_activity: Activity, ride: Ride):
)
)

def write_ride_efforts(self, strava_activity: Activity, ride: Ride):
def write_ride_efforts(self, strava_activity: DetailedActivity, ride: Ride):
"""
Writes out all effort associated with a ride to the database.
Expand Down Expand Up @@ -217,7 +217,7 @@ def _make_photo_from_native(

return p

def write_ride_photo_primary(self, strava_activity: Activity, ride: Ride):
def write_ride_photo_primary(self, strava_activity: DetailedActivity, ride: Ride):
"""
Store primary photo for activity from the main detail-level activity.
Expand Down Expand Up @@ -400,7 +400,7 @@ def fetch_and_store_activity_detail(
)
raise

def update_ride_complete(self, strava_activity: Activity, ride: Ride):
def update_ride_complete(self, strava_activity: DetailedActivity, ride: Ride):
"""
Updates all ride data from a fully-populated Strava `Activity`.
Expand Down Expand Up @@ -442,7 +442,7 @@ def update_ride_complete(self, strava_activity: Activity, ride: Ride):

def check_activity(
self,
activity: Activity,
activity: DetailedActivity,
*,
start_date: datetime,
end_date: datetime,
Expand Down Expand Up @@ -478,7 +478,7 @@ def check_activity(
)
)

if activity.type not in (Activity.RIDE, Activity.EBIKERIDE):
if activity.type not in (DetailedActivity.RIDE, DetailedActivity.EBIKERIDE):
raise IneligibleActivity(
"Skipping ride {0} ({1!r}) because it is not a RIDE or EBIKERIDE.".format(
activity.id, activity.name
Expand Down Expand Up @@ -513,7 +513,7 @@ def list_rides(
start_date: datetime,
end_date: datetime,
exclude_keywords: List[str] = None,
) -> List[Activity]:
) -> List[DetailedActivity]:
"""
List all of the rides for individual athlete.
Expand Down Expand Up @@ -546,12 +546,12 @@ def is_excluded(activity):

activities = client.get_activities(
after=start_date, limit=None
) # type: List[Activity]
) # type: List[DetailedActivity]
filtered_rides = [
a
for a in activities
if (
(a.type == Activity.RIDE or a.type == Activity.EBIKERIDE)
(a.type == DetailedActivity.RIDE or a.type == DetailedActivity.EBIKERIDE)
and not a.manual
and not a.trainer
and not is_excluded(a)
Expand All @@ -560,7 +560,7 @@ def is_excluded(activity):

return filtered_rides

def write_ride(self, activity: Activity) -> Ride:
def write_ride(self, activity: DetailedActivity) -> Ride:
"""
Takes the specified activity and writes it to the database.
Expand Down
4 changes: 2 additions & 2 deletions freezing/sync/data/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy import and_, or_, update
from sqlalchemy.orm import joinedload
from stravalib.exc import ObjectNotFound
from stravalib.model import Activity, Stream
from stravalib.model import Activity, StreamSet

from freezing.sync.config import config
from freezing.sync.exc import ActivityNotFound, ConfigurationError
Expand Down Expand Up @@ -122,7 +122,7 @@ def fetch_and_store_activity_streams(
)
raise

def write_ride_streams(self, streams: List[Stream], ride: Ride):
def write_ride_streams(self, streams: StreamSet, ride: Ride):
"""
Store GPS track for activity as geometry (linesring) and json types in db.
Expand Down
6 changes: 3 additions & 3 deletions freezing/sync/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from freezing.model.orm import Ride
from stravalib.client import Client
from stravalib.exc import ObjectNotFound
from stravalib.model import Activity, IdentifiableEntity, Stream
from stravalib.model import DetailedActivity, IdentifiableEntity, Stream


class CachingAthleteObjectFetcher(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -185,7 +185,7 @@ def fetch(
object_id: int,
use_cache: bool = True,
only_cache: bool = False
) -> Optional[Activity]:
) -> Optional[DetailedActivity]:
"""
Fetches activity and returns it.
Expand All @@ -202,7 +202,7 @@ def fetch(
only_cache=only_cache,
)
if activity_json:
return Activity.deserialize(activity_json, bind_client=self.client)
return DetailedActivity.deserialize(activity_json, bind_client=self.client)


class CachingStreamFetcher(CachingAthleteObjectFetcher):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ freezing-model @ https://github.com/freezingsaddles/freezing-model/archive/0.11.
greenstalk==2.0.2
pytz==2024.2
requests==2.32.3
stravalib==1.2.0
stravalib==2.1.0

0 comments on commit 1eb7207

Please sign in to comment.