Skip to content

Commit

Permalink
bugfix (#32)
Browse files Browse the repository at this point in the history
- fix loop and case-sensitive names
  • Loading branch information
martimlobao authored Jan 18, 2024
1 parent 26a0263 commit 4f1698f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scheduled_holiday_runs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
run: |
echo "CLASS_DATE=$(date -d '+2 days' +%F)" >> $GITHUB_ENV
echo "CLASS_TIME='10:00'" >> $GITHUB_ENV
echo "CLASS_TYPE='WEEKEND WOD RATO'" >> $GITHUB_ENV
echo "CLASS_TYPE='Weekend WOD Rato'" >> $GITHUB_ENV
- name: Regibox enroll
run: pants run regibox:main -- ${{ env.CLASS_DATE }} ${{ env.CLASS_TIME }} ${{ env.CLASS_TYPE }}
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scheduled_runs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
echo "CLASS_DATE=$(date -d '+3 days' +%F)" >> $GITHUB_ENV
echo "CLASS_TIME='06:30'" >> $GITHUB_ENV
echo "CLASS_TYPE='WOD RATO'" >> $GITHUB_ENV
echo "CLASS_TYPE='WOD Rato'" >> $GITHUB_ENV
- name: Regibox enroll
run: pants run regibox:main -- ${{ env.CLASS_DATE }} ${{ env.CLASS_TIME }} ${{ env.CLASS_TYPE }}
env:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "regibox"
version = "1.0.0"
version = "1.0.1"
description = "Automatically enroll in Regibox Crossfit classes"
authors = ["Martim Lobao <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion regibox/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0"
__version__ = "1.0.1"
12 changes: 9 additions & 3 deletions regibox/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,17 @@ def get_classes(year: int, month: int, day: int) -> list[Class]:
return [Class(tag) for tag in soup.find_all("div", attrs={"class": "filtro0"})]


def pick_class(classes: list[Class], class_time: str, class_type: str) -> Class:
def pick_class(
classes: list[Class], *, class_time: str, class_type: str, class_date: str
) -> Class:
for class_ in classes:
if class_.start != class_time or class_.name != class_type:
if (
class_.start != class_time
or class_.name.upper() != class_type.upper()
or class_.date != class_date
):
continue
return class_
raise ClassNotFoundError(
f"Unable to find enroll button for class '{class_type}' at {class_time}."
f"Unable to find class '{class_type}' at {class_time} on {class_date}."
)
28 changes: 15 additions & 13 deletions regibox/regibox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from regibox.classes import Class, get_classes, pick_class
from regibox.common import LOGGER, TIMEZONE
from regibox.exceptions import ClassNotFoundError

START: datetime.datetime = datetime.datetime.now(TIMEZONE)

Expand All @@ -22,7 +21,7 @@ def get(cls, now_seconds: int) -> int:


def main(
class_date: str | None = None, class_time: str = "06:30", class_type: str = "WOD RATO"
*, class_date: str | None = None, class_time: str = "06:30", class_type: str = "WOD Rato"
) -> None:
class_time = class_time.zfill(5) # needs leading zeros
LOGGER.info(f"Started at {START.isoformat()}")
Expand All @@ -34,18 +33,21 @@ def main(

while (datetime.datetime.now(TIMEZONE) - START).total_seconds() < timeout:
classes: list[Class] = get_classes(date.year, date.month, date.day)
try:
class_: Class = pick_class(classes, class_time, class_type)
except ClassNotFoundError:
# seconds between calls
wait: int = WaitTime.get(datetime.datetime.now(TIMEZONE).second)
LOGGER.info(
f"No button found for {class_type} on {date.date().isoformat()} at"
f" {class_time}, retrying in {wait} seconds."
)
time.sleep(wait)
else:
class_: Class = pick_class(
classes,
class_time=class_time,
class_type=class_type,
class_date=date.date().isoformat(),
)
if class_.is_open:
break

wait: int = WaitTime.get(datetime.datetime.now(TIMEZONE).second) # seconds between calls
LOGGER.info(
f"Waiting for {class_type} on {date.date().isoformat()} at {class_time} to be"
f" available, ETA in {class_.time_to_enroll}. Retrying in {wait} seconds."
)
time.sleep(wait)
else:
raise RuntimeError(
f"Timed out waiting for class {class_type} on {date.date().isoformat()} at"
Expand Down

0 comments on commit 4f1698f

Please sign in to comment.