From 4d0cad509e47afb73349ec74bacc8700d33bb0f3 Mon Sep 17 00:00:00 2001 From: ching Date: Tue, 14 May 2024 14:48:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gen_homepage.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 gen_homepage.py diff --git a/gen_homepage.py b/gen_homepage.py new file mode 100644 index 0000000..6c736d4 --- /dev/null +++ b/gen_homepage.py @@ -0,0 +1,105 @@ +import requests +import json + +# Cloudflare API credentials +API_TOKEN = 'YOUR_API_TOKEN' +ZONE_ID = 'YOUR_ZONE_ID' +BASE_URL = f'https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records' +PATH = '' +# Headers for the API request +headers = { + 'Authorization': f'Bearer {API_TOKEN}', + 'Content-Type': 'application/json' +} + +def get_dns_records(page=1): + params = { + 'page': page, + 'per_page': 100 + } + response = requests.get(BASE_URL, headers=headers, params=params) + response.raise_for_status() + return response.json() + +def extract_a_and_cname_records(records): + filtered_records = [record for record in records if record['type'] in ['A', 'CNAME']] + return filtered_records + +def get_record(): + all_records = [] + page = 1 + while True: + result = get_dns_records(page) + all_records.extend(result['result']) + if result['result_info']['total_pages'] == page: + break + page += 1 + + a_and_cname_records = extract_a_and_cname_records(all_records) + + with open(f'{PATH}/records.txt', 'w') as f: + for record in a_and_cname_records: + f.write(f"{record['name']}\n") + +def read_domains(file_path): + with open(file_path, 'r') as file: + domains = file.readlines() + return [domain.strip() for domain in domains] + +def extract_subdomains(domain): + parts = domain.split('.') + if len(parts) > 2: + subdomain = '.'.join(parts[:-2]) + else: + subdomain = parts[0] + return subdomain + +def generate_html(domains): + domains.sort() + html_content = ''' + + + + + + Domain Navigation + + + + +
+
+''' + for domain in domains: + subdomain = extract_subdomains(domain) + html_content += f''' + +''' + html_content += ''' +
+
+ + +''' + return html_content + +def write_html(html_content): + with open(f'{PATH}/index.html', 'w') as file: + file.write(html_content) + +def main(): + get_record() + domains = read_domains(f'{PATH}/records.txt') + html_content = generate_html(domains) + write_html(html_content) + +if __name__ == '__main__': + main()