Tried to make stuff async
parent
699097345c
commit
4038d79d75
|
@ -3,6 +3,8 @@ import argparse
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from weather import WeatherAPI
|
from weather import WeatherAPI
|
||||||
|
import asyncio as aio
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
def existing_path(path_str):
|
def existing_path(path_str):
|
||||||
|
@ -14,7 +16,7 @@ def existing_path(path_str):
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
def main():
|
async def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-c", "--config-file", help="Path to config file.",
|
parser.add_argument("-c", "--config-file", help="Path to config file.",
|
||||||
default="padel.ini", type=existing_path)
|
default="padel.ini", type=existing_path)
|
||||||
|
@ -30,17 +32,18 @@ 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 = get_club_address(args.club_id)
|
club_address = await get_club_address(args.club_id)
|
||||||
|
|
||||||
if club_address is None:
|
if club_address is None:
|
||||||
print("Couldn't get club address.")
|
print("Couldn't get club address.")
|
||||||
return
|
return
|
||||||
|
|
||||||
weather_api = WeatherAPI(config["DEFAULT"]["weather_api_key"])
|
weather_api = WeatherAPI(config["DEFAULT"]["weather_api_key"])
|
||||||
weather_forecasts = weather_api.get_hourly_conditions(club_address,
|
weather_forecasts, timeslots = await aio.gather(
|
||||||
days=args.days)
|
weather_api.get_hourly_conditions(club_address, days=args.days),
|
||||||
timeslots = get_time_slots(args.club_id, days=args.days)
|
get_time_slots(args.club_id, days=args.days)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
aio.run(main())
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from datetime import date, time, timedelta, datetime
|
from datetime import date, time, timedelta, datetime
|
||||||
|
import asyncio as aio
|
||||||
|
|
||||||
|
|
||||||
BASE_URL = "https://www.tennisvlaanderen.be/terreinreservatie-dagplanning"
|
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]
|
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)]
|
dates = [date.today() + timedelta(days=i) for i in range(days)]
|
||||||
params = {"clubId": club_id}
|
|
||||||
|
|
||||||
output = []
|
async def get_calendar(date_obj):
|
||||||
|
r = requests.get(BASE_URL, params={
|
||||||
for planning_date in dates:
|
"clubId": club_id,
|
||||||
params["planningDay"] = planning_date.strftime("%d-%m-%Y")
|
"planningDay": date_obj.strftime("%d-%m-%Y")
|
||||||
|
})
|
||||||
r = requests.get(BASE_URL, params=params)
|
|
||||||
soup = BeautifulSoup(r.content, "html.parser")
|
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={
|
r = requests.get(BASE_URL, params={
|
||||||
"clubId": club_id,
|
"clubId": club_id,
|
||||||
"tab": "club",
|
"tab": "club",
|
||||||
|
|
|
@ -11,7 +11,7 @@ class WeatherAPI:
|
||||||
params["key"] = self._api_key
|
params["key"] = self._api_key
|
||||||
return requests.get(f"{self.BASE_URL}{endpoint}", params=params)
|
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", {
|
r = self._get("/forecast.json", {
|
||||||
"q": query,
|
"q": query,
|
||||||
"days": days,
|
"days": days,
|
||||||
|
|
Loading…
Reference in New Issue