used more elegant library features

This commit is contained in:
Kristian Krsnik 2023-07-26 22:31:05 +02:00
parent 8beb0a9fb3
commit 2511d6ae4d

View File

@ -27,7 +27,7 @@ def log(logline: str, path: str = './log.txt') -> None:
# Go through resolvers until one retuns an IP # Go through resolvers until one retuns an IP
def getCurrentIP(resolvers: list[str], log: callable) -> None | str: def getCurrentIP(resolvers: list[str], log: callable) -> None | str:
for resolver in resolvers: for resolver in resolvers:
response = requests.request('GET', resolver) response = requests.get(resolver)
if response.status_code == 200: if response.status_code == 200:
current_ip = response.text.strip() current_ip = response.text.strip()
is_ipv4 = re.search('^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\\b){4}$', current_ip) is not None is_ipv4 = re.search('^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\\b){4}$', current_ip) is not None
@ -47,11 +47,11 @@ def checkRecords(api: dict, current_ip: str, log: callable) -> dict:
for key, domains in api.items(): for key, domains in api.items():
headers = { 'authorization': f"Apikey {key}" } headers = { 'authorization': f"Apikey {key}" }
for domain, records in domains.items(): for domain, records in domains.items():
response = requests.request('GET', f"https://api.gandi.net/v5/livedns/domains/{domain}/records?rrset_type=A", headers = headers) response = requests.get(f"https://api.gandi.net/v5/livedns/domains/{domain}/records?rrset_type=A", headers = headers)
if response.status_code == 200: if response.status_code == 200:
records_to_renew[key][domain] = list(set(records_to_renew[key][domain]) - set(record['rrset_name'] for record in (filter(lambda x: current_ip in x['rrset_values'], json.loads(response.text))))) records_to_renew[key][domain] = list(set(records_to_renew[key][domain]) - set(record['rrset_name'] for record in (filter(lambda x: current_ip in x['rrset_values'], json.loads(response.json())))))
else: else:
log(f"[ERROR][{domain}][{response.status_code}] {response.text}") log(f"[ERROR][{domain}][{response.status_code}] {response.json()}")
return records_to_renew return records_to_renew
@ -73,11 +73,11 @@ def renewRecords(records, current_ip, log, ttl = 3600):
for domain, records in domains.items(): for domain, records in domains.items():
for record in records: for record in records:
response = requests.request('PUT', f"https://api.gandi.net/v5/livedns/domains/{domain}/records/{record}", data = payload, headers = headers) response = requests.put(f"https://api.gandi.net/v5/livedns/domains/{domain}/records/{record}", data = payload, headers = headers)
if response.status_code == 201: if response.status_code == 201:
log(f"[OK][{domain}][{record}] Renewed with IP '{current_ip}'") log(f"[OK][{domain}][{record}] Renewed with IP '{current_ip}'")
else: else:
log(f"[ERROR][{domain}][{record}][{response.status_code}] {response.text}") log(f"[ERROR][{domain}][{record}][{response.status_code}] {response.json()}")
def main(): def main():
settings = loadSettings() settings = loadSettings()