diff --git a/padel/__main__.py b/padel/__main__.py index 8ada0b1..725743e 100644 --- a/padel/__main__.py +++ b/padel/__main__.py @@ -3,6 +3,8 @@ import argparse from configparser import ConfigParser from pathlib import Path from weather import WeatherAPI +import asyncio as aio +import time def existing_path(path_str): @@ -14,7 +16,7 @@ def existing_path(path_str): return path -def main(): +async def main(): parser = argparse.ArgumentParser() parser.add_argument("-c", "--config-file", help="Path to config file.", default="padel.ini", type=existing_path) @@ -30,17 +32,18 @@ def main(): # TODO check if config file can be read config.read(args.config_file) - club_address = get_club_address(args.club_id) + club_address = await get_club_address(args.club_id) if club_address is None: print("Couldn't get club address.") return weather_api = WeatherAPI(config["DEFAULT"]["weather_api_key"]) - weather_forecasts = weather_api.get_hourly_conditions(club_address, - days=args.days) - timeslots = get_time_slots(args.club_id, days=args.days) + weather_forecasts, timeslots = await aio.gather( + weather_api.get_hourly_conditions(club_address, days=args.days), + get_time_slots(args.club_id, days=args.days) + ) if __name__ == "__main__": - main() + aio.run(main()) diff --git a/padel/combo.py b/padel/combo.py new file mode 100644 index 0000000..e69de29 diff --git a/padel/tennis.py b/padel/tennis.py index 441bd64..14462ae 100644 --- a/padel/tennis.py +++ b/padel/tennis.py @@ -1,6 +1,7 @@ import requests from bs4 import BeautifulSoup from datetime import date, time, timedelta, datetime +import asyncio as aio BASE_URL = "https://www.tennisvlaanderen.be/terreinreservatie-dagplanning" @@ -61,23 +62,22 @@ def extract_calendar(soup: BeautifulSoup, reservation_date): return [(col, status, datetime.combine(reservation_date, start), duration) for col, status, start, duration in timeslots] -def get_time_slots(club_id: int, days=1): +async def get_time_slots(club_id: int, days=1): dates = [date.today() + timedelta(days=i) for i in range(days)] - params = {"clubId": club_id} - output = [] - - for planning_date in dates: - params["planningDay"] = planning_date.strftime("%d-%m-%Y") - - r = requests.get(BASE_URL, params=params) + async def get_calendar(date_obj): + r = requests.get(BASE_URL, params={ + "clubId": club_id, + "planningDay": date_obj.strftime("%d-%m-%Y") + }) soup = BeautifulSoup(r.content, "html.parser") - output.extend(extract_calendar(soup, planning_date)) + return extract_calendar(soup, date_obj) - return output + return sum(await aio.gather(*[get_calendar(date_obj) for date_obj in + dates]), []) -def get_club_address(club_id: int): +async def get_club_address(club_id: int): r = requests.get(BASE_URL, params={ "clubId": club_id, "tab": "club", diff --git a/padel/weather.py b/padel/weather.py index fe4f607..18692c9 100644 --- a/padel/weather.py +++ b/padel/weather.py @@ -11,7 +11,7 @@ class WeatherAPI: params["key"] = self._api_key return requests.get(f"{self.BASE_URL}{endpoint}", params=params) - def get_hourly_conditions(self, query, days=1): + async def get_hourly_conditions(self, query, days=1): r = self._get("/forecast.json", { "q": query, "days": days,