Use if __name_ == "__main__"

This commit is contained in:
2022-07-20 21:48:41 +01:00
parent 236f8fcfe6
commit d7ff9743f8

View File

@@ -68,27 +68,28 @@ def updateCloudflare(zone, email, apiKey, host, ip):
else: else:
print("Updating failed, errors were: " + str(result["errors"])) print("Updating failed, errors were: " + str(result["errors"]))
print( def main():
print(
''' '''
------------------------------ ------------------------------
Cloudflare updater Cloudflare updater
www.jakecharman.co.uk www.jakecharman.co.uk
------------------------------ ------------------------------
''' '''
) )
# Get the directory of the script. # Get the directory of the script.
scriptDir = os.path.dirname(os.path.realpath(__file__)) + "/" scriptDir = os.path.dirname(os.path.realpath(__file__)) + "/"
# Read in parameters from the config file. # Read in parameters from the config file.
try: try:
configFile = open(scriptDir + "updateCloudflare.conf", "r") configFile = open(scriptDir + "updateCloudflare.conf", "r")
except FileNotFoundError: except FileNotFoundError:
print("Configuration file does not exist.") print("Configuration file does not exist.")
exit(1) exit(1)
configLines = configFile.readlines() configLines = configFile.readlines()
for line in configLines: for line in configLines:
if line[0] == "#": if line[0] == "#":
continue continue
# Remove any spaces from the line then split it to an array on the = sign. # Remove any spaces from the line then split it to an array on the = sign.
@@ -106,28 +107,28 @@ for line in configLines:
print("Unknown config: " + splitLn[0]) print("Unknown config: " + splitLn[0])
exit(1) exit(1)
# Get the Zone ID for the given hostname. # Get the Zone ID for the given hostname.
zoneID = getZone(authEmail, apiKey, hostToUpdate) zoneID = getZone(authEmail, apiKey, hostToUpdate)
# Open the temp file. # Open the temp file.
try: try:
file = open(scriptDir + 'updateCloudflare.lastip', 'r') file = open(scriptDir + 'updateCloudflare.lastip', 'r')
lastip = file.read() lastip = file.read()
except FileNotFoundError: except FileNotFoundError:
lastip = "" lastip = ""
if lastip == "": if lastip == "":
storedIP = False storedIP = False
print("No stored IP... checking Cloudflare API") print("No stored IP... checking Cloudflare API")
cloudflareIP = checkCloudflare(zoneID, authEmail, apiKey, hostToUpdate) cloudflareIP = checkCloudflare(zoneID, authEmail, apiKey, hostToUpdate)
else: else:
storedIP = lastip storedIP = lastip
print("Stored IP is " + lastip) print("Stored IP is " + lastip)
# Get our external IP # Get our external IP
ip = requests.get('https://api.ipify.org').text ip = requests.get('https://api.ipify.org').text
print("Our current IP is " + ip) print("Our current IP is " + ip)
if storedIP: if storedIP:
if ip == storedIP: if ip == storedIP:
print("IP has not changed since last run... Exiting") print("IP has not changed since last run... Exiting")
exit(0) exit(0)
@@ -135,7 +136,7 @@ if storedIP:
print("IP has changed since last run... Updating Cloudflare") print("IP has changed since last run... Updating Cloudflare")
storeIP(ip) storeIP(ip)
updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip) updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip)
else: else:
storeIP(ip) storeIP(ip)
if ip == cloudflareIP: if ip == cloudflareIP:
print("Cloudflare matches our current IP... Exiting") print("Cloudflare matches our current IP... Exiting")
@@ -143,3 +144,6 @@ else:
else: else:
print("Cloudflare IP does not match our current IP... Updating Cloudflare.") print("Cloudflare IP does not match our current IP... Updating Cloudflare.")
updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip) updateCloudflare(zoneID, authEmail, apiKey, hostToUpdate, ip)
if __name__ == "__main__":
main()