import asyncio from requests_html import AsyncHTMLSession from pythonosc import udp_client async def fetch_tide_height(osc_client, previous_tide_height): try: session = AsyncHTMLSession() url = "https://www.tide-forecast.com/locations/Texel-Noordzee-Netherlands/tides/latest" response = await session.get(url, timeout=10) await response.html.arender(timeout=20) # Find the tide height live_tide_height = response.html.find("span.tide-header-current__height-value", first=True) if live_tide_height is None: print("Could not find tide height element.") return tide_height_float = float(live_tide_height.text.replace('m', '')) # Send OSC message if the tide height changes if tide_height_float != previous_tide_height[0]: previous_tide_height[0] = tide_height_float osc_client.send_message("/tide/height", tide_height_float) print(f"Tide height changed: {tide_height_float} m (sent via OSC)") except Exception as e: print(f"Error in fetch_tide_height: {e}") async def periodic_fetch(osc_client): previous_tide_height = [None] # Mutable container for the previous tide height while True: await fetch_tide_height(osc_client, previous_tide_height) await asyncio.sleep(5) # Fetch every 5 seconds def main(): osc_client = udp_client.SimpleUDPClient("127.0.0.1", 5005) # Update with your Max patch IP/port asyncio.run(periodic_fetch(osc_client)) if __name__ == "__main__": main()