Skip to content

Commit 1968d35

Browse files
committed
Add ability to sync more than one workout with number_of_workouts option
1 parent bf8baf7 commit 1968d35

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

config.yaml.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ recovery_pace_adjust: [0, 0]
88
very_easy_pace_adjust: [0, 0]
99
easy_pace_adjust: [0, 0]
1010
fast_pace_adjust: [0, 0]
11-
extreme_pace_adjust: [0, 0]
11+
extreme_pace_adjust: [0, 0]
12+
number_of_workouts: 1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "trainaspower"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
description = "Convert TrainAsOne plans to power and upload to Final Surge for use with Stryd pod."
55
authors = ["Chase Sterling <[email protected]>"]
66
license = "MIT"

trainaspower/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from itertools import islice
12
from pathlib import Path
23
import sys
34

@@ -54,14 +55,14 @@ def main():
5455
finalsurge.login(config.finalsurge_email, config.finalsurge_password)
5556
stryd.login(config.stryd_email, config.stryd_password)
5657
try:
57-
wo = trainasone.get_next_workout(config)
58+
for wo in islice(trainasone.get_next_workouts(config), config.number_of_workouts):
59+
finalsurge.add_workout(wo)
5860
except trainasone.FindWorkoutException as exc:
5961
with open(directory / exc.filename, "w", encoding="utf-8") as f:
6062
f.write(exc.html)
6163
logger.opt(exception=True).debug("Error")
6264
logger.error(f"Could not load next Train as One workout. Created {exc.filename} for debugging.")
6365
sys.exit(1)
64-
finalsurge.add_workout(wo)
6566

6667

6768
if __name__ == "__main__":

trainaspower/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Config(BaseModel):
2525
easy_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0)
2626
fast_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0)
2727
extreme_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0)
28+
number_of_workouts: int = 1
2829

2930
class Config:
3031
extra = 'forbid'

trainaspower/trainasone.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import re
23
from typing import Generator
34

@@ -35,23 +36,27 @@ def login(email, password) -> None:
3536
raise Exception("Failed to login to Train as One")
3637

3738

38-
def get_next_workout(config) -> models.Workout:
39+
def get_next_workouts(config) -> Generator[models.Workout, None, None]:
3940
logger.info("Fetching next TrainAsOne workout.")
4041
r = tao_session.get("https://beta.trainasone.com/calendarView")
42+
found = False
4143
try:
4244
upcoming = r.html.find(".today, .future")
4345
for day in upcoming:
4446
if day.find(".workout"):
45-
break
46-
else:
47+
date = dateparser.parse(day.find(".title", first=True).text)
48+
workout_url = day.find(".workout a", first=True).absolute_links.pop()
49+
yield get_workout(workout_url, date, config)
50+
found = True
51+
if not found:
4752
raise Exception("Next tao workout not found.")
48-
date = dateparser.parse(day.find(".title", first=True).text)
49-
workout_url = day.find(".workout a", first=True).absolute_links.pop()
5053
except Exception as exc:
5154
raise FindWorkoutException(
5255
f"Error finding next TaO workout: {exc.args[0]}", "taocalendar.html", r.text
5356
) from exc
5457

58+
59+
def get_workout(workout_url: str, date: datetime.date, config: models.Config) -> models.Workout:
5560
r = tao_session.get(workout_url)
5661
try:
5762
workout_html = r.html

0 commit comments

Comments
 (0)