From e60ae30668399fcee6cb944d2c2a98974e5e05f3 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Tue, 12 Dec 2023 15:10:31 +1100 Subject: [PATCH] feat: Alert via discord if the nb token expires --- main.py | 14 +++++++++++--- requirements.txt | 3 ++- server.py | 7 +++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index f92b2b3..53f5bdf 100644 --- a/main.py +++ b/main.py @@ -174,15 +174,23 @@ def update_address(): cookies = {"namebase-main": nbcookie} headers = {"Accept": "application/json", "Content-Type": "application/json"} r = requests.post('https://www.namebase.io/api/v0/deposit/address', data=json.dumps(payload), headers=headers, cookies=cookies) + if 'address' not in r.json(): + print("Error: " + r.text,flush=True) + # Send alert via discord + webhook = os.getenv('webhook') + if webhook != None: + payload = { + "content": "Error: " + r.text + } + r = requests.post(webhook, data=json.dumps(payload), headers=headers) + return + address = r.json()['address'] print("Address updated: " + address,flush=True) update_address() -# Schedule address update every hour -schedule.every(1).hour.do(update_address) - if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt index 07a29eb..9d41042 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ requests pyotp schedule email-validator -py3dns \ No newline at end of file +py3dns +apscheduler \ No newline at end of file diff --git a/server.py b/server.py index 0f521b8..d8de7e7 100644 --- a/server.py +++ b/server.py @@ -7,6 +7,7 @@ import os import dotenv import sys import json +from apscheduler.schedulers.background import BackgroundScheduler class GunicornApp(BaseApplication): @@ -24,6 +25,12 @@ class GunicornApp(BaseApplication): return self.application if __name__ == '__main__': + # Every hour + scheduler = BackgroundScheduler() + scheduler.add_job(main.update_address, 'cron', hour='*') + scheduler.start() + + workers = os.getenv('WORKERS') threads = os.getenv('THREADS') if workers is None: