<commit_before>#!/usr/bin/env python
#
# CloudFlare DDNS script.
#
# usage:
#   cloudflare_ddns.py [config]
#
# See README for details
#

import requests
import json
import time
import yaml
import os
import sys
from subprocess import Popen, PIPE

# CloudFlare api url.
CLOUDFLARE_URL = 'https://api.cloudflare.com/client/v4'

# Time-to-live for your A record. This should be as small as possible to ensure
# changes aren't cached for too long and are propogated quickly.  CloudFlare's
# api docs set a minimum of 120 seconds.
TTL = '120'

# DNS record type for your DDNS host. Probably an A record.
RECORD_TYPE = 'A'

# Location of this script.
SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__))

# If a command-line argument is provided, use that as the config file.
if len(sys.argv) == 1:
    CONFIG_FILE = os.path.join(SCRIPT_ROOT, "config.yaml")
else:
    CONFIG_FILE = os.path.join(SCRIPT_ROOT, sys.argv[1])


def main():
    if not os.path.isfile(CONFIG_FILE):
        die(
            "Configuration file not found. Please review the README and try "
            "again."
        )

    # Read config file
    with open(CONFIG_FILE, 'r') as f:
        config = yaml.load(f)

    cf_key = config.get('cf_key')
    cf_email = config.get('cf_email')
    cf_domain = config.get('cf_domain')
    cf_subdomain = config.get('cf_subdomain')
    cf_service_mode = config.get('cf_service_mode')
    quiet = 'true' == config.get('quiet')
    aws_use_ec2metadata = config.get('aws_use_ec2metadata')

    auth_headers = {
        'X-Auth-Key': cf_key,
        'X-Auth-Email': cf_email,
    }

    ### Discover your public IP address.
    if aws_use_ec2metadata:
      try:
        p = Popen(
            ["ec2metadata", "--public-ip"],
            stdin=PIPE,
            stdout=PIPE,
            stderr=PIPE,
        )
        output, err = p.communicate()
        rc = p.returncode
        public_ip = output.rstrip('\n')
      except:
        die("Failed to query AWS ec2metadata for public IP")
    else:
        public_ip = requests.get("http://ipv4.icanhazip.com/").text.strip()
        # public_ip = '127.0.0.100'

    ### Get zone id for the dns record we want to update
    results = get_paginated_results(
        'GET',
        CLOUDFLARE_URL + '/zones',
        auth_headers,
    )
    cf_zone_id = None
    for zone in results:
        zone_name = zone['name']
        zone_id = zone['id']
        if zone_name == cf_domain:
            cf_zone_id = zone_id
            break
    if cf_zone_id is None:
        raise Exception("Snap, can't find zone '{}'".format(cf_domain))

    ### Get record id for the record we want to update
    if cf_subdomain == '':
        target_name = cf_domain
    else:<commit_msg>Change http for https when getting the public ip<commit_after>#!/usr/bin/env python
#
# CloudFlare DDNS script.
#
# usage:
#   cloudflare_ddns.py [config]
#
# See README for details
#

import requests
import json
import time
import yaml
import os
import sys
from subprocess import Popen, PIPE

# CloudFlare api url.
CLOUDFLARE_URL = 'https://api.cloudflare.com/client/v4'

# Time-to-live for your A record. This should be as small as possible to ensure
# changes aren't cached for too long and are propogated quickly.  CloudFlare's
# api docs set a minimum of 120 seconds.
TTL = '120'

# DNS record type for your DDNS host. Probably an A record.
RECORD_TYPE = 'A'

# Location of this script.
SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__))

# If a command-line argument is provided, use that as the config file.
if len(sys.argv) == 1:
    CONFIG_FILE = os.path.join(SCRIPT_ROOT, "config.yaml")
else:
    CONFIG_FILE = os.path.join(SCRIPT_ROOT, sys.argv[1])


def main():
    if not os.path.isfile(CONFIG_FILE):
        die(
            "Configuration file not found. Please review the README and try "
            "again."
        )

    # Read config file
    with open(CONFIG_FILE, 'r') as f:
        config = yaml.load(f)

    cf_key = config.get('cf_key')
    cf_email = config.get('cf_email')
    cf_domain = config.get('cf_domain')
    cf_subdomain = config.get('cf_subdomain')
    cf_service_mode = config.get('cf_service_mode')
    quiet = 'true' == config.get('quiet')
    aws_use_ec2metadata = config.get('aws_use_ec2metadata')

    auth_headers = {
        'X-Auth-Key': cf_key,
        'X-Auth-Email': cf_email,
    }

    ### Discover your public IP address.
    if aws_use_ec2metadata:
      try:
        p = Popen(
            ["ec2metadata", "--public-ip"],
            stdin=PIPE,
            stdout=PIPE,
            stderr=PIPE,
        )
        output, err = p.communicate()
        rc = p.returncode
        public_ip = output.rstrip('\n')
      except:
        die("Failed to query AWS ec2metadata for public IP")
    else:
        public_ip = requests.get("https://ipv4.icanhazip.com/").text.strip()
        # public_ip = '127.0.0.100'

    ### Get zone id for the dns record we want to update
    results = get_paginated_results(
        'GET',
        CLOUDFLARE_URL + '/zones',
        auth_headers,
    )
    cf_zone_id = None
    for zone in results:
        zone_name = zone['name']
        zone_id = zone['id']
        if zone_name == cf_domain:
            cf_zone_id = zone_id
            break
    if cf_zone_id is None:
        raise Exception("Snap, can't find zone '{}'".format(cf_domain))

    ### Get record id for the record we want to update
    if cf_subdomain == '':
        target_name = cf_domain
    else:
{'fraction_non_alphanumeric': 0.11119212541013489, 'fraction_numerical': 0.008384979948960992, 'mean_word_length': 2.515054452274183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5959214', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1796, 'n_words': 509}