Package: domains-api
Google Domains DDNS API ClientTo facilitate running a home web server behind a router without a static IP, this package checks to see if your external IP has changed and automatically updates your Dynamic DNS rules on Google Domains, via the API; also notifies user by email if required.Installation (Python 3.8+):pip install domains-apiUsage:Can be run from the command line like so:domains [opt]or imported into your projects:from domains_api.ip_changer import IPChanger

changer = IPChanger()Windows/Mac or Linux, it will ask for your credentials on first run and then shouldn't need any input after that. I added command line options/arguments (seedomains --help) for loading/deleting a profile and changing credentials/settings more easily.You will need your Dynamic DNS autogenerated username and password as described inthis documentation.If you choose to receive email notifications, you will be asked to input your gmail email address and password which will then be encoded before being saved as part of a User instance. (The notification is sent from the user's own email address via the gmail smtp server, you might need to allow less secure apps on your Google account to use.)OnWindowsyou can use Task Scheduler; onLinux/Mac, add a line to your crontab and you can choose the frequency of the checks. An example hourly cron job would look like this:0 * * * * domains >> /dev/null 2>&1assumes package is installed in global scope; otherwise, use commandpath/to/env/bin/python3 -m domains_apiIf reducing downtime is essential, you could increase the frequency of checks to every 1 minute, or even less, like this:*/1 * * * * ...etcOn Google Domains the default TTL for Dynamic DNS is 1 minute, so checks should never need to be more frequent than this, but unless you expect your external IP to change very frequently, such regular cron jobs might be a slight waste of resources; even so, the script is very light-weight and usually only takes just over a second to run normally on a Raspberry Pi 3 Ubuntu server.A daemon version of the program is currently in development with v0.4, that allows you to run the program on a schedule by runningdomainsd 30(check every 30 minutes) anddomainsd stopto cancel.The logs are written to both~/.domains/domains.log(posix) or%LOCALAPPDATA%/domains/domains.log(win), and stdout, so that they also appear in the terminal & crontab log.After initial setup, the script takes care of everything: if your IP has changed since you last ran it, it will update your Dynamic DNS rule on domains.google.com.If you forget your IP or need to check it for any reason, running:domains -i...will log your current external IP to the terminal without doing anything else.Checkdomains -hfor more command line options.Example in Django application:In your Django environment:pip install domains-api apschedulerYou must first run the script from the command line to set up credentials, or set them manually via theUserclass:from domains_api import IPChanger, User

ip_changer = IPChanger()

ip_changer.user = User(
    domain="example.com",
    api_key="eXamPleBlahbLAhBlah",
    api_sec="eXamPleBlahbLAhBlee",
)Then, in your project you can create a new module called ip_changer in your project's root directory,
with an empty__init__.pyfile and anip_changer.pyfile.ip_changer.pyshould look something like this:from apscheduler.schedulers.background import BackgroundScheduler
from domains_api import IPChanger, User


def start():
    scheduler = BackgroundScheduler()
    scheduler.add_job(IPChanger, 'interval', minutes=10)
    scheduler.start()Then you will need to add the following to your main app'sapps.pyfile:class MainConfig(AppConfig):
    name = 'main'

    def ready(self):
        from ip_changer import ip_changer
        ip_changer.start()Check/var/log/apache2/error.logor/var/log/nginx/error.logor your webserver log location (might be logged as wsgi errors) and/or~/.domains/domains.logto check everything is working as expected.Change Log0.4.2 (13/02/2022)fix complex command line parsing0.4.1 (07/02/2022)move user file intofiledir to allow for multiple envsown daemon script (background runner)0.3.10 (01/02/2022)add test email functionimprove IP parsing for --forcefix email erroradd error logging to email funcs0.3.6 (29/01/2022)fix force and and regex for valid ipfix ignored email setup optionimprove logging and refactor response parsing func0.3.3 (24/01/2022)refactored and optimisedpassword encryptionrotating log file handler0.2.14 (23/01/2022)optimise sizeshow IP only (for easier use in bash)0.2.12 (22/01/2022)better fix for version file not foundadd version option to argsadd -D --domain opt to view domain0.2.10.1 (16/01/2022)hotfix for version file not found0.2.10 (16/01/2022)getopt --> argparserversion constant for setup.py0.2.9 (16/01/2022)re-add -l --load-user optskip user creation for --help --ip & --load_user optsstop double check bugadd script to pathdelete user logging bugfix0.2.6 (29/12/2021)format coderefactor0.2.5.4 (27/08/2021)fixed bug causing -f opt not to fire0.2.5 (27/06/2021)added '-f' or '--force' option to force api call even if recorded IP hasn't changed.0.2.4 (25/06/2021)reinstated check if ip changed before API call since refactoring.fixed logging bug re failed emails0.2.2.1 (12/06/2021)fixed inheritance bug (domains api not being called)relative imports0.2.1 (15/05/2021)more modular: added IPChecker parent class and BaseUser parent classcan be used independent of a domain name to send email notifications.updated README0.1.6 (02/03/2021)Fixed twitchy email notification bug0.1.5.2 (09/01/2021)Fixed email outbox (too many emails being sent)Updated READMEConsistent exit codesClear logs after 100 lines (keep the last 10 lines)Try/except for permissions (instead of if/else)Other bugfixes/refactoring0.1.4.8 (08/01/2021)Fixed relative importChanged directory to/var/www/domains-api/Updated readmeFixed duplicate loggers0.1.4.4 (07/01/2021)Refactored all file/log handling methods into file_handlers.FileHandlersFixed permissions issues (better than before) - run package with sudo the first time to initialize files.Changed posix directory for logs and user files todomains_api/.domains/save_user/load_useranddelete_usermethods are now part of the FileHandlers class.0.1.4.2 (06/01/2021)Added email outbox for offline messages0.1.4.1 (05/01/2021)Fixed PermissionError when working with Apache2 & Django (must run script first as www-data e.g: "sudo -u www-data /venv/bin/python -m domains_api" - this will allow the apache server to be the owner of the log/user files when they are set up with least hassle. Missing this set the server will fail with an EOF error, as input is needed to initialize the first User instance. - bear in mind that running the script as a different user in the same environment will fail unless you change permissions on the log/user file located in the package directory)Fixed absolute paths inUser.load_user()andUser().save_user()functions.Updated README with Django/Apache2 example.0.1.3.6 (04/01/2021)Updated GitHub url and long description / readme.Added option to show ip (only) from command line (domains_api -i --ip).Added test function to master branch: simulate change in ip (commented inmain.py)Minor bugfixes0.1 - 0.1.2 (03/01/2021)First ReleaseCode review from:https://codereview.stackexchange.com/questions/254272/dynamic-dns-ip-checker-changer-for-google-domains-in-python