From 1eb72070f894bb452f9abbbc07793c59fb9767de Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Wed, 1 Jan 2025 10:33:27 -0500 Subject: [PATCH] Upgrade stravalib from 1.2 to 2.1 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). --- freezing/sync/data/activity.py | 22 +++++++++++----------- freezing/sync/data/streams.py | 4 ++-- freezing/sync/utils/cache.py | 6 +++--- requirements.txt | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/freezing/sync/data/activity.py b/freezing/sync/data/activity.py index 28a5429..cce1540 100644 --- a/freezing/sync/data/activity.py +++ b/freezing/sync/data/activity.py @@ -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 ( @@ -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. @@ -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. @@ -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. @@ -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`. @@ -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, @@ -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 @@ -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. @@ -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) @@ -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. diff --git a/freezing/sync/data/streams.py b/freezing/sync/data/streams.py index 9efc18e..270d05c 100644 --- a/freezing/sync/data/streams.py +++ b/freezing/sync/data/streams.py @@ -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 @@ -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. diff --git a/freezing/sync/utils/cache.py b/freezing/sync/utils/cache.py index 3d7c61b..57e92d6 100644 --- a/freezing/sync/utils/cache.py +++ b/freezing/sync/utils/cache.py @@ -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): @@ -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. @@ -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): diff --git a/requirements.txt b/requirements.txt index 7536a11..14323b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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