Added first functioning algorithm!

main
Jef Roosens 2021-08-11 16:40:23 +02:00
parent 4da1c98977
commit d1c6eaa602
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
1 changed files with 33 additions and 3 deletions

View File

@ -1,5 +1,6 @@
from tennis import get_time_slots, get_club_address
from weather import WeatherAPI
from datetime import timedelta
import asyncio as aio
@ -23,7 +24,36 @@ async def get_decent_timeslots(club_id, weather_api_key, days=1):
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)
# Convert weather_forecasts to dict for faster lookups
weather_forecasts = {date_obj: weather_str for date_obj, weather_str, _ in
weather_forecasts}
print(weather_forecasts)
return timeslots
# Filter out non-free timeslots
timeslots = list(filter(lambda x: x[1] == 0, timeslots))
print(timeslots)
output = []
for field_name, _, start_time, duration in timeslots:
start_hour = start_time.replace(minute=0)
weather_names = []
valid = True
while start_hour < start_time + duration:
if (weather_name := weather_forecasts.get(start_hour)):
weather_names.append(weather_name)
# If we can't find any information about the timeslot, we assume
# it's bad
else:
valid = False
break
start_hour += timedelta(hours=1)
if valid:
output.append((field_name, start_time, duration, weather_names))
return output