diff --git a/ddns.py b/ddns.py index c136f9e..e7cdebf 100755 --- a/ddns.py +++ b/ddns.py @@ -9,6 +9,7 @@ import argparse import requests import os import time +from datetime import datetime from string import ascii_letters, digits from rich import print from argparse import RawTextHelpFormatter @@ -16,7 +17,7 @@ from argparse import RawTextHelpFormatter homefilepath = Path.home() filepath = homefilepath.joinpath('.config/ddns') database = filepath.joinpath('ddns.db') -app_version = '0.1' +app_version = '0.2' def get_ip(): @@ -53,7 +54,7 @@ def connect_database(): api text NOT NULL)''') c.execute('''CREATE TABLE IF NOT EXISTS ipservers (id integer NOT NULL PRIMARY KEY, - ip4_server text, + ip4_server text NOT NULL, ip6_server text)''') c.execute('''CREATE TABLE IF NOT EXISTS domains (id integer PRIMARY KEY, @@ -61,7 +62,7 @@ def connect_database(): c.execute('''CREATE TABLE IF NOT EXISTS subdomains (id integer PRIMARY KEY, main_id integer NOT NULL, - name text, + name text NOT NULL, current_ip4 text NOT NULL, current_ip6 text NULL)''') @@ -358,6 +359,7 @@ def updateip(): cursor = conn.cursor() cursor.execute('SELECT COUNT(*) FROM subdomains') count = cursor.fetchone()[0] + now = datetime.now() if count == 0: print('[red]Error: [/red]There are no dynamic domains active.'\ ' Start by adding a new domain with [i]ddns -s test.example.com[/i]') @@ -377,6 +379,7 @@ def updateip(): print('[red]Error: ' + str(response.json)) else: cursor.execute('UPDATE subdomains SET current_ip4=? WHERE id = ?',(current_ip,subdomain_id,)) + cursor.execute('UPDATE subdomains SET last_updated=? WHERE id = ?',(now.strftime("%d/%m/%Y_%H:%M:%S"),subdomain_id,)) conn.commit() @@ -416,9 +419,23 @@ def local_add_subdomain(domain,domainid): +def updatedb(): + # Update DB with new column 20.03.23 + # Add last updated field for subdomains + new_table = 'last_updated' + cursor = conn.cursor() + info = conn.execute("PRAGMA table_info('subdomains')").fetchall() + if not any(new_table in word for word in info): + add_column = "ALTER TABLE subdomains ADD COLUMN last_updated text" + conn.execute(add_column) + conn.commit() + + + + # Commandline arguments conn = connect_database() - +updatedb() parser = argparse.ArgumentParser(prog='ddns', description='Application to use domains from DigitalOcean account as dynamic '\ 'DNS domain(s).\nThe app only supports IP4. IPv6 is planned for a later release!'\ @@ -447,7 +464,7 @@ parser.add_argument('-t', '--top', help='Add a new domain from your DigitalOcean parser.add_argument('-s', '--sub', help='Add a new subdomain to your DigitalOcean account and use as dynamic DNS.\n', required=False, nargs=1, metavar=('domain'), action='append') -parser.add_argument('-k', '--local', help='Add an existing DigitalOcean domain to your ddns DB and use as dynamic DNS. Get ', +parser.add_argument('-k', '--local', help='Add an existing DigitalOcean domain to your ddns DB and use as dynamic DNS.', required=False, nargs=2, metavar=('domain','domainid'), action='append') parser.add_argument('-r', '--remove', help='Remove a subdomain from your DigitalOcean account and ddns.',