| import requests |
| import time |
| import subprocess |
| from pathlib import Path |
|
|
| def get_current_ngrok_url(): |
| """Get current ngrok URL from ngrok API""" |
| try: |
| response = requests.get("http://localhost:4040/api/tunnels") |
| if response.status_code == 200: |
| data = response.json() |
| tunnels = data.get("tunnels", []) |
| for tunnel in tunnels: |
| if tunnel.get("proto") == "https": |
| return tunnel.get("public_url") |
| except: |
| pass |
| return None |
|
|
| def update_env_file(url): |
| """Update .env file with new URL""" |
| env_file = Path(".env") |
| if env_file.exists(): |
| with open(env_file, "r") as f: |
| lines = f.readlines() |
| |
| with open(env_file, "w") as f: |
| for line in lines: |
| if line.startswith("OLLAMA_HOST="): |
| f.write(f"OLLAMA_HOST={url}\n") |
| else: |
| f.write(line) |
|
|
| def main(): |
| """Monitor ngrok URL changes""" |
| print("Monitoring ngrok URL changes...") |
| last_url = None |
| |
| while True: |
| try: |
| current_url = get_current_ngrok_url() |
| if current_url and current_url != last_url: |
| print(f"Ngrok URL changed: {current_url}") |
| update_env_file(current_url) |
| last_url = current_url |
| print("Environment updated!") |
| |
| time.sleep(30) |
| except KeyboardInterrupt: |
| print("Monitoring stopped.") |
| break |
| except Exception as e: |
| print(f"Error: {e}") |
| time.sleep(30) |
|
|
| if __name__ == "__main__": |
| main() |
|
|