Skip to content

Commit

Permalink
GH-17: Refactor calory downloading a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-ueding committed Feb 3, 2024
1 parent 927a9b0 commit 7bb584b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
4 changes: 1 addition & 3 deletions geo_activity_playground/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def make_activity_repository(
apply_cache_migrations()
config = get_config()

if "strava" in config and not skip_strava:
download_missing_calories()

repository = ActivityRepository()

if pathlib.Path("Activities").exists():
Expand All @@ -116,6 +113,7 @@ def make_activity_repository(
import_from_strava_checkout(repository)
if "strava" in config and not skip_strava:
import_from_strava_api(repository)
download_missing_calories(repository)

embellish_time_series(repository)
compute_tile_visits(repository)
Expand Down
7 changes: 5 additions & 2 deletions geo_activity_playground/core/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ def commit(self) -> None:
self.meta.index = self.meta["id"]
self.meta.index.name = "index"
self.meta.sort_values("start", inplace=True)
activity_path.parent.mkdir(exist_ok=True, parents=True)
self.meta.to_parquet(activity_path)
self.save()
self._loose_activities = []

def save(self) -> None:
activity_path.parent.mkdir(exist_ok=True, parents=True)
self.meta.to_parquet(activity_path)

def has_activity(self, activity_id: int) -> bool:
if len(self.meta):
if activity_id in self.meta["id"]:
Expand Down
16 changes: 5 additions & 11 deletions geo_activity_playground/importers/strava_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,18 @@ def get_detailed_activity(activity_id: int, client: Client):
return detailed_activity


def download_missing_calories() -> None:
activity_meta_path = pathlib.Path("Cache/activities.parquet")
if not activity_meta_path.exists():
return

activity_meta = pd.read_parquet(activity_meta_path)
activity_meta.index = activity_meta["id"]

def download_missing_calories(repository: ActivityRepository) -> None:
client = Client(access_token=get_current_access_token())

try:
for activity in tqdm(
client.get_activities(after="2000-01-01T00:00:00Z"),
desc="Downloading calories from Strava",
):
calories = get_detailed_activity(activity.id, client).calories
activity_meta.loc[activity.id, "calories"] = calories
if repository.has_activity(activity.id):
calories = get_detailed_activity(activity.id, client).calories
repository.meta.loc[activity.id, "calories"] = calories
except RateLimitExceeded:
pass
finally:
activity_meta.to_parquet(activity_meta_path)
repository.save()

0 comments on commit 7bb584b

Please sign in to comment.