diff --git a/padel/__main__.py b/padel/__main__.py index 725743e..5830ebc 100644 --- a/padel/__main__.py +++ b/padel/__main__.py @@ -5,6 +5,7 @@ from pathlib import Path from weather import WeatherAPI import asyncio as aio import time +from combo import get_decent_timeslots def existing_path(path_str): @@ -32,18 +33,11 @@ async def main(): # TODO check if config file can be read config.read(args.config_file) - 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, timeslots = await aio.gather( - weather_api.get_hourly_conditions(club_address, days=args.days), - get_time_slots(args.club_id, days=args.days) - ) + res = await get_decent_timeslots(args.club_id, + config["DEFAULT"]["weather_api_key"], + args.days) + print(res) if __name__ == "__main__": aio.run(main()) diff --git a/padel/combo.py b/padel/combo.py index e69de29..8039f0e 100644 --- a/padel/combo.py +++ b/padel/combo.py @@ -0,0 +1,29 @@ +from tennis import get_time_slots, get_club_address +from weather import WeatherAPI +import asyncio as aio + + +# Provided by my code monkey Lander +GOOD_CODES = [1000,1003, 1006, 1009,1150, 1153] + + +async def get_decent_timeslots(club_id, weather_api_key, days=1): + club_address = await get_club_address(club_id) + + if club_address is None: + return None + + weather_api = WeatherAPI(weather_api_key) + weather_forecasts, timeslots = await aio.gather( + weather_api.get_hourly_conditions(club_address, days=days), + get_time_slots(club_id, days=days) + ) + + # Filter out bad weather forecasts & sort them according to date & time + weather_forecasts = sorted(filter(lambda x: x[2] in GOOD_CODES, + weather_forecasts), key=lambda x: x[0]) + + # Filter out non-free timeslots + timeslots = filter(lambda x: x[1] == 0, timeslots) + + return timeslots diff --git a/padel/tennis.py b/padel/tennis.py index 14462ae..4a6693f 100644 --- a/padel/tennis.py +++ b/padel/tennis.py @@ -74,8 +74,15 @@ async def get_time_slots(club_id: int, days=1): return extract_calendar(soup, date_obj) - return sum(await aio.gather(*[get_calendar(date_obj) for date_obj in - dates]), []) + output = [] + + for coro in aio.as_completed([ + get_calendar(date_obj) for date_obj in dates + ]): + res = await coro + output.extend(res) + + return output async def get_club_address(club_id: int): r = requests.get(BASE_URL, params={ diff --git a/padel/weather.py b/padel/weather.py index 18692c9..31ec822 100644 --- a/padel/weather.py +++ b/padel/weather.py @@ -1,4 +1,5 @@ import requests +from datetime import datetime class WeatherAPI: @@ -23,4 +24,11 @@ class WeatherAPI: return None data = r.json() - return sum([obj["hour"] for obj in data["forecast"]["forecastday"]], []) + hour_forecasts = sum([obj["hour"] for obj in data["forecast"]["forecastday"]], []) + return [ + ( + datetime.fromtimestamp(forecast["time_epoch"]), + forecast["condition"]["text"], + forecast["condition"]["code"], + ) for forecast in hour_forecasts + ]