padel/padel/__main__.py

60 lines
1.7 KiB
Python
Raw Permalink Normal View History

2021-08-11 13:55:39 +02:00
from tennis import get_time_slots, get_club_address
import argparse
from configparser import ConfigParser
from pathlib import Path
from weather import WeatherAPI
2021-08-11 15:48:03 +02:00
import asyncio as aio
import time
2021-08-11 16:28:28 +02:00
from combo import get_decent_timeslots
2021-08-11 13:55:39 +02:00
def existing_path(path_str):
path = Path(path_str)
if not path.exists() or not path.is_file():
raise argparse.ArgumentTypeError("Config file doesn't exist or isn't a file.")
2021-08-11 16:42:34 +02:00
2021-08-11 13:55:39 +02:00
return path
2021-08-11 15:48:03 +02:00
async def main():
2021-08-11 13:55:39 +02:00
parser = argparse.ArgumentParser()
2021-08-11 16:42:34 +02:00
parser.add_argument(
"-c",
"--config-file",
help="Path to config file.",
default="padel.ini",
type=existing_path,
)
parser.add_argument(
"-d", "--days", help="How many days in advance to look.", default=1, type=int
)
2021-08-11 13:55:39 +02:00
parser.add_argument("club_id", help="ID of the club to check.", type=int)
args = parser.parse_args()
# Read config file
config = ConfigParser()
# TODO check if config file can be read
config.read(args.config_file)
2021-08-11 16:42:34 +02:00
res = await get_decent_timeslots(
args.club_id, config["DEFAULT"]["weather_api_key"], args.days
)
2021-08-11 13:55:39 +02:00
2021-08-11 17:27:11 +02:00
for field_name, start_time, duration, weather_names in res:
2021-08-11 17:43:49 +02:00
# Very satisfying, create a set & sort it using the original list's index
2021-08-11 17:27:11 +02:00
pruned_weather_names = sorted(set(weather_names),
key=lambda x: weather_names.index(x))
2021-08-11 17:43:49 +02:00
2021-08-11 17:27:11 +02:00
print("{} ({}, {}m): {}".format(
field_name,
2021-08-11 17:43:49 +02:00
start_time.strftime("%Y-%m-%d %H:%M"),
2021-08-11 17:27:11 +02:00
int(duration.total_seconds() // 60),
" -> ".join(pruned_weather_names)
))
2021-08-11 13:55:39 +02:00
2021-08-11 16:42:34 +02:00
2021-08-11 13:55:39 +02:00
if __name__ == "__main__":
2021-08-11 15:48:03 +02:00
aio.run(main())