import requests
import re

# API-endpoint voor configuratiebeheer via YDM API
api_url = "http://IP_ADDRESS_OF_YEALINK_PHONE/cgi-bin/configman/exec"

# Vervang dit met de juiste autorisatiegegevens
auth = ("USERNAME", "PASSWORD")

# Lijst met BLF-waarden om te identificeren en aan te passen
blf_values = ["blf1@example.com", "blf2@example.com"]

# Regex-patroon om BLF-waarden te identificeren
blf_pattern = re.compile(r"(key\d+.type=34\nkey\d+.value=)(.*?)(\n)", re.DOTALL)

# Haal de huidige configuratie op
response = requests.post(api_url, auth=auth, data="command=get_cfg")

if response.status_code == 200:
    config_data = response.text

    # Loop door de BLF-waarden en pas deze aan naar Speed Dial
    for value in blf_values:
        config_data = blf_pattern.sub(fr"\1{value}\3", config_data)

    # Sla de bijgewerkte configuratie op
    response = requests.post(api_url, auth=auth, data=f"command=put_cfg&data={config_data}")

    if response.status_code == 200:
        print("Configuratie bijgewerkt.")
    else:
        print("Fout bij het bijwerken van de configuratie.")
else:
    print("Fout bij het ophalen van de configuratie.")