Started comparison system

main
Jef Roosens 2021-08-11 16:28:28 +02:00
parent 4038d79d75
commit 4da1c98977
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
4 changed files with 52 additions and 14 deletions

View File

@ -5,6 +5,7 @@ from pathlib import Path
from weather import WeatherAPI from weather import WeatherAPI
import asyncio as aio import asyncio as aio
import time import time
from combo import get_decent_timeslots
def existing_path(path_str): def existing_path(path_str):
@ -32,18 +33,11 @@ async def main():
# TODO check if config file can be read # TODO check if config file can be read
config.read(args.config_file) config.read(args.config_file)
club_address = await get_club_address(args.club_id) res = await get_decent_timeslots(args.club_id,
config["DEFAULT"]["weather_api_key"],
if club_address is None: args.days)
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)
)
print(res)
if __name__ == "__main__": if __name__ == "__main__":
aio.run(main()) aio.run(main())

View File

@ -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

View File

@ -74,8 +74,15 @@ async def get_time_slots(club_id: int, days=1):
return extract_calendar(soup, date_obj) return extract_calendar(soup, date_obj)
return sum(await aio.gather(*[get_calendar(date_obj) for date_obj in output = []
dates]), [])
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): async def get_club_address(club_id: int):
r = requests.get(BASE_URL, params={ r = requests.get(BASE_URL, params={

View File

@ -1,4 +1,5 @@
import requests import requests
from datetime import datetime
class WeatherAPI: class WeatherAPI:
@ -23,4 +24,11 @@ class WeatherAPI:
return None return None
data = r.json() 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
]