Skip to content

Commit 0debff2

Browse files
Update main.py to coding standard from pyling
1 parent d3a949a commit 0debff2

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

main.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
CF_ZONE_ID = os.getenv("CF_ZONE_ID")
2727
DNS_RECORD_COMMENT_KEY = os.getenv('DNS_RECORD_COMMENT_KEY')
2828
DOMAINS_FILE_PATH = os.getenv('DOMAINS_FILE_PATH')
29-
SCHEDULE_MINUTES = int(os.getenv('SCHEDULE_MINUTES', 60))
29+
SCHEDULE_MINUTES = int(os.getenv('SCHEDULE_MINUTES', '60'))
3030

3131
# Define API endpoints
3232
BASE_URL = 'https://api.cloudflare.com/client/v4/'
@@ -69,7 +69,7 @@ def create_logger(level=logging.INFO):
6969

7070
# Get current DNS record for the specified domain
7171
def get_dns_record(zone_id, domain_name):
72-
LOGGER.info(f"Fetching record for '{domain_name}' in zone '{zone_id}.")
72+
LOGGER.info(f"Fetching record for '%s' in zone '%s'.", domain_name, zone_id)
7373

7474
headers = {
7575
'Authorization': 'Bearer ' + CF_API_TOKEN,
@@ -80,16 +80,16 @@ def get_dns_record(zone_id, domain_name):
8080
'name': domain_name,
8181
}
8282

83-
response = requests.get(f'{BASE_URL}zones/{zone_id}/dns_records', headers=headers, params=params)
83+
response = requests.get(f'{BASE_URL}zones/{zone_id}/dns_records', headers=headers, params=params, timeout=60)
8484

8585
if response.status_code == 200:
8686
records = response.json()['result']
8787

8888
if records:
89-
LOGGER.info(f"Successfully fetched data for '{domain_name}'.")
89+
LOGGER.info(f"Successfully fetched data for '%s'.", domain_name)
9090
return records[0]
9191
else:
92-
LOGGER.error(f"Failed to fetch data for '{domain_name}'. Response: {response.json()}")
92+
LOGGER.error(f"Failed to fetch data for '%s'. Response: %s", domain_name, response.json())
9393

9494
return None
9595

@@ -108,18 +108,19 @@ def update_dns_record(record, content):
108108
response = requests.patch(
109109
f"{BASE_URL}zones/{record['zone_id']}/dns_records/{record['id']}",
110110
json=data,
111-
headers=headers
111+
headers=headers,
112+
timeout=30
112113
)
113114

114115
if response.status_code == 200:
115-
LOGGER.info(f"DNS record updated successfully: {record['name']} ({record['type']}) -> {content}")
116+
LOGGER.info(f"DNS record updated successfully: %s (%s) -> %s", record['name'], record['type'], content)
116117
else:
117-
LOGGER.error(f"Failed to update DNS record: {response.json()}")
118+
LOGGER.error(f"Failed to update DNS record: %s", response.json())
118119

119120

120121
# Loads static wishlist of domains in json format along with their metadata
121122
def read_zones_from_file(json_file_path, zone_id):
122-
with open(json_file_path, 'r') as file:
123+
with open(json_file_path, 'r', encoding="utf-8") as file:
123124
data = json.load(file)
124125

125126
zones = data['zones']
@@ -131,7 +132,7 @@ def read_zones_from_file(json_file_path, zone_id):
131132
for domain in zone['domains']:
132133
domain['zone_id'] = zone['id']
133134

134-
LOGGER.info(f"Sucessfully read zone {zone}.")
135+
LOGGER.info(f"Sucessfully read zone %s.", zone)
135136

136137
return zones
137138

@@ -140,7 +141,7 @@ def read_zones_from_file(json_file_path, zone_id):
140141
def get_dns_records_by_name(zones):
141142
records = []
142143

143-
LOGGER.info(f"Trying to fetch records for {len(zones)} zones.")
144+
LOGGER.info(f"Trying to fetch records for %s zones.", len(zones))
144145

145146
for zone in zones:
146147
for domain in zone['domains']:
@@ -163,18 +164,17 @@ def get_dns_records_by_comment(zone_id, comment_key):
163164
'comment.contains': comment_key,
164165
}
165166

166-
LOGGER.info(f"Fetching DNS record with comment key: {comment_key}")
167-
response = requests.get(f'{BASE_URL}zones/{zone_id}/dns_records', headers=headers, params=params)
167+
LOGGER.info(f"Fetching DNS record with comment key: %s", comment_key)
168+
response = requests.get(f'{BASE_URL}zones/{zone_id}/dns_records', headers=headers, params=params, timeout=60)
168169

169170
if response.status_code == 200:
170171
records = response.json()['result']
171172
if records and len(records) > 0:
172173
return records
173-
else:
174-
LOGGER.warning(f"Request was successful but no valid domains were found: {response.json()}")
175-
return []
174+
LOGGER.warning(f"Request was successful but no valid domains were found: %s", response.json())
175+
return []
176176
else:
177-
LOGGER.error(f"Failed to get dns_records with comment key: {response.json()}")
177+
LOGGER.error(f"Failed to get dns_records with comment key: %s", response.json())
178178

179179
return []
180180

@@ -197,7 +197,8 @@ def is_connected():
197197
host = socket.gethostbyname("www.cloudflare.com")
198198
socket.create_connection((host, 80), 2)
199199
return True
200-
except Exception:
200+
except socket.error as exc:
201+
LOGGER.error("Socket error: %s", exc)
201202
pass
202203
return False
203204

@@ -212,32 +213,32 @@ def check_and_update_dns():
212213
if CF_ZONE_ID is None:
213214
LOGGER.error("CF_ZONE_ID: At least one zone id must be set.")
214215
return
215-
elif CF_API_TOKEN is None:
216+
if CF_API_TOKEN is None:
216217
LOGGER.error("CF_API_TOKEN Missing: You have to provide your Cloudflare API Token.")
217218
return
218-
elif DNS_RECORD_COMMENT_KEY is None and DOMAINS_FILE_PATH is None:
219+
if DNS_RECORD_COMMENT_KEY is None and DOMAINS_FILE_PATH is None:
219220
LOGGER.error("DNS_RECORD_COMMENT_KEY and DOMAINS_FILE_PATH are missing, don't know which domains to update")
220221
return
221222

222223
public_ip = get_public_ip()
223224
domain_records = []
224225

225226
if DNS_RECORD_COMMENT_KEY is not None:
226-
LOGGER.info(f"Using DNS_RECORD_COMMENT_KEY='{DNS_RECORD_COMMENT_KEY}' to find DNS records to update.")
227+
LOGGER.info(f"Using DNS_RECORD_COMMENT_KEY='%s' to find DNS records to update.", DNS_RECORD_COMMENT_KEY)
227228
domain_records = get_dns_records_by_comment(CF_ZONE_ID, DNS_RECORD_COMMENT_KEY)
228229
else:
229-
LOGGER.info(f"Using DOMAINS_FILE_PATH='{DOMAINS_FILE_PATH}' to find DNS records to update.")
230+
LOGGER.info(f"Using DOMAINS_FILE_PATH='%s' to find DNS records to update.", DOMAINS_FILE_PATH)
230231
domain_records = get_dns_records_by_name(read_zones_from_file(DOMAINS_FILE_PATH, CF_ZONE_ID))
231232

232233
valid_domains = [x['name'] for x in domain_records if x is not None]
233-
LOGGER.info(f"Found {len(valid_domains)} valid domains for update: [{','.join(valid_domains)}]")
234+
LOGGER.info(f"Found %s valid domains for update: [%s]", len(valid_domains), ','.join(valid_domains))
234235

235236
if public_ip:
236237
for record in domain_records:
237238
domain_name = record['name']
238239

239240
if record is None:
240-
LOGGER.error(f"DNS record for {domain_name} not found.")
241+
LOGGER.error(f"DNS record for %s not found.", domain_name)
241242
continue
242243

243244
if public_ip != record['content']:
@@ -246,12 +247,12 @@ def check_and_update_dns():
246247
public_ip
247248
)
248249
else:
249-
LOGGER.info(f"IP addresses are the same for {domain_name}. No update needed.")
250+
LOGGER.info(f"IP addresses are the same for %s. No update needed.", domain_name)
250251
else:
251252
LOGGER.error("Failed to retrieve public IP. Skipping check and update.")
252253

253254

254-
LOGGER.info(f"Schedule is set at {SCHEDULE_MINUTES}")
255+
LOGGER.info(f"Schedule is set at %s minutes", SCHEDULE_MINUTES)
255256

256257
# Schedule the check and update process to run every X minutes
257258
schedule.every(SCHEDULE_MINUTES).minutes.do(check_and_update_dns).run()

0 commit comments

Comments
 (0)