Skip to content

Commit

Permalink
GH-17: Remember download position
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-ueding committed Feb 3, 2024
1 parent 1606d9d commit 1a18b5e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions geo_activity_playground/importers/strava_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from geo_activity_playground.core.activities import ActivityRepository
from geo_activity_playground.core.config import get_config
from geo_activity_playground.core.paths import cache_dir


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -213,16 +214,25 @@ def get_detailed_activity(activity_id: int, client: Client):
def download_missing_calories(repository: ActivityRepository) -> None:
client = Client(access_token=get_current_access_token())

latest_timestamp_path = cache_dir() / "strava-detailed-activity-last.json"
if latest_timestamp_path.exists():
with open(latest_timestamp_path) as f:
latest_timestamp = json.load(f)
else:
latest_timestamp = "2000-01-01T00:00:00Z"

try:
for activity in tqdm(
client.get_activities(after="2000-01-01T00:00:00Z"),
client.get_activities(after=latest_timestamp),
desc="Downloading calories from Strava",
total=len(repository),
):
if repository.has_activity(activity.id):
calories = get_detailed_activity(activity.id, client).calories
repository.meta.loc[activity.id, "calories"] = calories
latest_timestamp = activity.start.isoformat().replace("+00:00", "Z")
except RateLimitExceeded:
pass
finally:
repository.save()
with open(latest_timestamp_path, "w") as f:
json.dump(latest_timestamp, f)

0 comments on commit 1a18b5e

Please sign in to comment.