#! /usr/env python3

# Up until now, this script only handles type A records.
# More may come on request

import requests
import json
import sys







# IMPORTANT
# THIS SCRIPT IS OLD
# THERE IS A NEW VERIONS AVAILABLE:
# https://lugico.de/one_com_ddns.py











DOMAIN="DOMAIN.NO"
# looks like: domain.com
# no www

SUBDOMAIN="play"
# looks like "play"
# NOT play.example.com

TIMEOUT=3600
#TTL

ID="11223344"
# You can find the DNS ID like this:
# In Firefox, Press F12 to open the Developer Console
# Select the Network tab
# Go to the dns page of one.com
# Change anything to one of the domains
# Click on the second element in the list in the console (counted from the button)
# You can identify it by the "PATCH" in the second column
# A window should pop up on the left
# At the very top it says "Request URL:"
# At the end of that domain, there should be a 8 Digit long number
# That is your ID
# Copy it into the variable above
# KEEP THE QUOTATION MARKS!!!

# CREDENTIALS
USERNAME="typically@email.com"
PASSWORD="somebeautifulpassword"

GET_IP = requests.get("https://api.ipify.org/")
if (GET_IP.status_code == 200):
    IP = GET_IP.text
    print("GOT RESULT FROM IPIFY.ORG - Your ip is: " + IP)
else:
    sys.exit("Could not get IP address from api.ipify.org - Check your internet connection, or check that https://api.ipify.org is up.")



# INITIATE SESSION
session=requests.Session()

loginurl = "https://www.one.com/admin/login.do"

# CREATE DATA FOR LOGIN
logindata = {'loginDomain': True,'displayUsername': USERNAME, 'password1': PASSWORD, 'username': USERNAME, 'targetDomain': '', 'loginTarget': ''}
session.post(loginurl, data=logindata)

# CREATE DATA FOR DNS CHANGE

tosend = {"type":"dns_service_records","id":ID,"attributes":{"type":"A","prefix":SUBDOMAIN,"content":IP,"ttl":TIMEOUT}}

dnsurl="https://www.one.com/admin/api/domains/" + DOMAIN + "/dns/custom_records/" + ID
sendheaders={'Content-Type': 'application/json'}

# PATCH DATA
r2=session.patch(dnsurl, data=json.dumps(tosend), headers=sendheaders)

print(r2.text)
print(IP)
