-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
executable file
·69 lines (62 loc) · 1.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import schedule
import requests
from dotenv import load_dotenv
from datetime import datetime
# Load environments from .env
load_dotenv()
def debug(*args):
# Get debug value from .env
DEBUG = os.getenv('ENABLE_DEBUG')
# Check if debug is enabled
if DEBUG == "1" or DEBUG.lower() == "true":
# Log
print("[DEBUG]", *args)
def update_ip():
# Get current time
date = datetime.now()
# Get the No-IP credentials
USER = os.getenv('NOIP_USER')
PASSWORD = os.getenv('NOIP_PASSWORD')
HOSTNAME = os.getenv('NOIP_HOSTNAME')
# Check if the credentials are set
if not USER or not PASSWORD or not HOSTNAME:
print('[ERROR] No-IP credentials are not set.')
return
# Log
debug('Auth: ', HOSTNAME, '/', USER)
# Try to get the IP and update the data on No-IP
try:
# Get the public ip
r = requests.get('https://api.ipify.org/?format=json')
ip = r.json()['ip']
# Update
r = requests.get("https://{}:{}@dynupdate.no-ip.com/nic/update?hostname={}&myip={}".format(USER, PASSWORD, HOSTNAME, ip))
# Log
print('[INFO] IP (' + str(ip) + ') updated at ' + str(date))
except e:
print('[ERROR] ', e)
def main():
# Get the frequency
MINUTES = os.getenv('FREQUENCY_MINUTES')
# Set default frequency if custom is not set
if not MINUTES:
MINUTES = 15
print('[WARN] Custom frequency is not set. Setting default (15).')
# Convert to integer
MINUTES = int(MINUTES)
# Log the frequency
print('[INFO] Ready to update every ', str(MINUTES), ' minute(s).')
# Cron Tab
schedule.every(MINUTES).minutes.do(update_ip)
# App started
print('[INFO] App started.')
# Run the script
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
main()