From c1e7d767c06f79439ddd34fdb059c591ae4f8dca Mon Sep 17 00:00:00 2001 From: Luke Cotton Date: Wed, 7 Feb 2024 13:15:34 +0000 Subject: [PATCH 1/3] Sync repeat interval when TTL is 1 (auto) --- cloudflare-ddns.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 1d56863..fc434fa 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -282,29 +282,30 @@ def updateIPs(ips): try: ttl = int(config["ttl"]) except: - ttl = 300 # default Cloudflare TTL + ttl = 1 # default Cloudflare TTL print( - "⚙️ No config detected for 'ttl' - defaulting to 300 seconds (5 minutes)") + "⚙️ No config detected for 'ttl' - defaulting to 1 (auto)") if ttl < 30: - ttl = 1 # + ttl = 1 print("⚙️ TTL is too low - defaulting to 1 (auto)") if (len(sys.argv) > 1): if (sys.argv[1] == "--repeat"): + delay = 300 if ttl == 1 else ttl if ipv4_enabled and ipv6_enabled: print( - "🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every " + str(ttl) + " seconds") + "🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every " + str(delay) + " seconds") elif ipv4_enabled and not ipv6_enabled: print("🕰️ Updating IPv4 (A) records every " + - str(ttl) + " seconds") + str(delay) + " seconds") elif ipv6_enabled and not ipv4_enabled: print("🕰️ Updating IPv6 (AAAA) records every " + - str(ttl) + " seconds") + str(delay) + " seconds") next_time = time.time() killer = GracefulExit() prev_ips = None while True: updateIPs(getIPs()) - if killer.kill_now.wait(ttl): + if killer.kill_now.wait(delay): break else: print("❓ Unrecognized parameter '" + From 3f277239f56c3632d2a128158fb5ee2aeca2d842 Mon Sep 17 00:00:00 2001 From: Luke Cotton Date: Wed, 7 Feb 2024 13:28:05 +0000 Subject: [PATCH 2/3] Reduce code repetition --- cloudflare-ddns.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index fc434fa..4275d5f 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -292,14 +292,12 @@ def updateIPs(ips): if (sys.argv[1] == "--repeat"): delay = 300 if ttl == 1 else ttl if ipv4_enabled and ipv6_enabled: - print( - "🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every " + str(delay) + " seconds") + ip_config = "IPv4 (A) & IPv6 (AAAA)" elif ipv4_enabled and not ipv6_enabled: - print("🕰️ Updating IPv4 (A) records every " + - str(delay) + " seconds") + ip_config = "IPv4 (A)" elif ipv6_enabled and not ipv4_enabled: - print("🕰️ Updating IPv6 (AAAA) records every " + - str(delay) + " seconds") + ip_config = "IPv6 (AAAA)" + print(f"🕰️ Updating {ip_config} records every {str(delay)} seconds") next_time = time.time() killer = GracefulExit() prev_ips = None From faf55a30636eab9242d7a2aa4fff36eb46009156 Mon Sep 17 00:00:00 2001 From: Luke Cotton Date: Wed, 7 Feb 2024 13:35:33 +0000 Subject: [PATCH 3/3] Use array joining instead of excessive `if` statements --- cloudflare-ddns.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 4275d5f..160af65 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -291,13 +291,13 @@ def updateIPs(ips): if (len(sys.argv) > 1): if (sys.argv[1] == "--repeat"): delay = 300 if ttl == 1 else ttl - if ipv4_enabled and ipv6_enabled: - ip_config = "IPv4 (A) & IPv6 (AAAA)" - elif ipv4_enabled and not ipv6_enabled: - ip_config = "IPv4 (A)" - elif ipv6_enabled and not ipv4_enabled: - ip_config = "IPv6 (AAAA)" - print(f"🕰️ Updating {ip_config} records every {str(delay)} seconds") + configured_ipvs = [] + if ipv4_enabled: + configured_ipvs.append("IPv4 (A)") + if ipv6_enabled: + configured_ipvs.append("IPv6 (AAAA)") + configured_ipvs = ' & '.join(configured_ipvs) + print(f"🕰️ Updating {configured_ipvs} records every {str(delay)} seconds") next_time = time.time() killer = GracefulExit() prev_ips = None