import requests
import shutil
import subprocess

def download_latest_rustdesk():
    # URL zum RustDesk-Repository
    url = 'https://api.github.com/repos/rustdesk/rustdesk/releases/latest'

    # Anfrage an die Github-API, um das neueste Release zu finden
    response = requests.get(url)
    release_data = response.json()

    # URL der .exe-Datei des neuesten Releases finden
    download_url = next((asset['browser_download_url'] for asset in release_data['assets'] if asset['name'].endswith('x86_64.exe')), None)

    if download_url:
        # Herunterladen der .exe-Datei
        exe_response = requests.get(download_url, stream=True)
        rustdesk_exe = "Rustdesk-host=remote.stines.de,key=QViLr1r9LsKiEWoMgA26pNGqgkyDXcZpvXqepqoa934=.exe"
        with open(rustdesk_exe, 'wb') as file:
            shutil.copyfileobj(exe_response.raw, file)
        subprocess.run(f"{rustdesk_exe} --slient-install")
        print(f'RustDesk {release_data["tag_name"]} wurde erfolgreich als RustDesk.exe heruntergeladen.')
    else:
        print('Die neueste Version von RustDesk konnte nicht gefunden werden.')

download_latest_rustdesk()
