-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·73 lines (66 loc) · 2.12 KB
/
app.py
File metadata and controls
executable file
·73 lines (66 loc) · 2.12 KB
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
70
71
72
73
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import logging
import schedule
import requests
from dotenv import load_dotenv
from datetime import datetime
# Set up logging
DEBUG = os.getenv('ENABLE_DEBUG')
logging_level = logging.DEBUG if DEBUG == "1" or DEBUG.lower() == "true" else logging.INFO
logging.basicConfig(format="%(asctime)s\t%(levelname)s\t%(message)s", datefmt='%Y-%m-%d %H:%M:%S', level=logging_level)
logger = logging.getLogger(__name__)
# Load environments from .env
load_dotenv()
# Update the IP address on No-IP
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:
logger.error('No-IP credentials are not set.')
return
# Log
logger.debug('No-IP Auth: ' + HOSTNAME + '/' + USER)
# Try to get the IP and update the data on No-IP
try:
# Get the public ip
r = requests.get('http://ipv4.iplocation.net')
ip = r.json()['ip']
# Update
r = requests.get("https://{}:{}@dynupdate.no-ip.com/nic/update?hostname={}&myip={}".format(USER, PASSWORD, HOSTNAME, ip))
# Log
logger.info('IP (' + str(ip) + ') updated.')
except Exception as e:
logger.error('Error: ' + e)
# Main function
def main():
# Get the frequency
MINUTES = os.getenv('FREQUENCY_MINUTES')
# Set default frequency if custom is not set
if not MINUTES:
MINUTES = 15
logger.warning('Custom frequency is not set. Setting default (15).')
# Convert to integer
MINUTES = int(MINUTES)
# Log the frequency
logger.info('Ready to update every ' + str(MINUTES) + ' minute(s).')
# Cron Tab
schedule.every(MINUTES).minutes.do(update_ip)
# App started
logger.debug('App started.')
# First run at startup
update_ip()
# Run the script
while True:
schedule.run_pending()
time.sleep(1)
# Run the script
if __name__ == "__main__":
main()