From d8fea630f3446976ff3a63209d149f7cbf76bea5 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 11 Jun 2024 21:58:37 -0400 Subject: [PATCH] python annoying --- main.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 4cb15fa..5bd2f0c 100644 --- a/main.py +++ b/main.py @@ -103,8 +103,22 @@ def fetch_ip(provider): def fetch_zone_id(zone_name, key): - headers = "Authorization: Bearer" - return "hello" + headers = { + "Authorization": f"Bearer {key}", + "Content-Type": "application/json" + } + r = requests.get(f"https://api.cloudflare.com/client/v4/zones", headers=headers) + if r.status_code == 200: + data = json.loads(r.text) + results = data["result"] + for result in results: + if result["name"] == zone_name: + return result["id"] + print("Failed to find zone id, does this zone exist?") + return None + print(f"Error has occurred, status code: {r.status_code}") + print(r.text) + return None def run(scheduler, r_args, r_envs, r_zones): @@ -114,7 +128,7 @@ def run(scheduler, r_args, r_envs, r_zones): for zone in r_zones.zones: user = zone.env if zone.zone_id is None: - zone.zone_id = fetch_zone_id(zone.zone_name) + zone.zone_id = fetch_zone_id(zone.zone_name, user.api_key) if __name__ == '__main__': @@ -123,8 +137,10 @@ if __name__ == '__main__': if args.install: run_install() else: - user_envs = get_env(args.env) - zones = get_conf(args.config, user_envs) - runner = sched.scheduler(time.time, time.sleep) - runner.enter(args.time, 1, run, (runner, args, user_envs, zones)) - runner.run() + m_user_envs = get_env(args.env) + m_zones = get_conf(args.config, m_user_envs) + for v_zone in m_zones.zones: + print(fetch_zone_id(v_zone.zone_name, v_zone.env.api_key)) + #runner = sched.scheduler(time.time, time.sleep) + #runner.enter(args.time, 1, run, (runner, args, user_envs, zones)) + #runner.run()