From c5486984e180c8d60832edd666191a5a632dc04b Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Mon, 22 Jan 2024 16:40:02 +0100 Subject: [PATCH] Environment variable & venv --- .dockerignore | 6 +++++ .gitignore | 7 ++++-- dockerfile | 2 +- main.py | 64 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..7a14add --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +token +/__pycache__/ +/bin +/lib +/lib64 +pyvenv.cfg \ No newline at end of file diff --git a/.gitignore b/.gitignore index 7a79f2e..7a14add 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ -config.py token -/__pycache__/ \ No newline at end of file +/__pycache__/ +/bin +/lib +/lib64 +pyvenv.cfg \ No newline at end of file diff --git a/dockerfile b/dockerfile index 6c5e118..dde3856 100644 --- a/dockerfile +++ b/dockerfile @@ -11,4 +11,4 @@ RUN pip install --no-cache-dir pip && \ COPY main.py . -CMD ["python", "main.py"] +CMD ["python", "-u", "main.py"] diff --git a/main.py b/main.py index 3f98fea..11f6189 100644 --- a/main.py +++ b/main.py @@ -10,26 +10,37 @@ import datetime import requests import random -initial_waiting_time = 60 + +def get_env(name, default=None): + content = os.environ.get(name, default) + + if type(default) == bool: + return content.lower() in ('true', '1') + if type(default) == int: + return int(content) + if type(default) == float: + return float(content) + return content + + +initial_waiting_time = get_env('INITIAL_WAITING_TIME', 60) waiting_time = initial_waiting_time -waiting_time_limit = 60 * 60 * 24 -waiting_time_increase = 2 +waiting_time_limit = get_env('WAITING_TIME_LIMIT', 60 * 60 * 24) +waiting_time_increase = get_env('WAITING_TIME_FACTOR', 2) -randomness = 0.1 +randomness = get_env('RANDOMNESS', 0.1) -interval = 60 +interval = get_env('INTERVAL', 60) # Reduce frequency of polling to avoid rate limiting -tgtg.POLLING_WAIT_TIME = 30 -tgtg.MAX_POLLING_TRIES = 10 +tgtg.POLLING_WAIT_TIME = get_env('LOGIN_POLLING_WAIT_TIME', 30) +tgtg.MAX_POLLING_TRIES = get_env('LOGIN_MAX_POLLING_TRIES', 10) client = None telegram_bot = None -removal_notification = os.environ.get('REMOVAL_NOTIFICATION') - -if removal_notification is None: - removal_notification = False +removal_notification = get_env('REMOVAL_NOTIFICATION', False) +TOKEN_PATH = get_env('TOKEN_PATH', '/data/token') def parse_duration(seconds): seconds = int(seconds) @@ -47,13 +58,13 @@ def parse_duration(seconds): def check_env(): - if os.environ.get('TGTG_EMAIL') is None: + if get_env('TGTG_EMAIL') is None: return False - if os.environ.get('TELEGRAM') is None and os.environ.get('MATRIX') is None: + if get_env('TELEGRAM') is None and os.environ.get('MATRIX') is None: return False - if os.environ.get('TELEGRAM') is not None and (os.environ.get('TELEGRAM_TOKEN') is None or os.environ.get('TELEGRAM_ID') is None): + if get_env('TELEGRAM') is not None and (os.environ.get('TELEGRAM_TOKEN') is None or os.environ.get('TELEGRAM_ID') is None): return False - if os.environ.get('MATRIX') is not None and os.environ.get('MATRIX_URL') is None: + if get_env('MATRIX') is not None and os.environ.get('MATRIX_URL') is None: return False return True @@ -85,39 +96,39 @@ def get_credentials(): def load_creds(): global client, telegram_bot - if not os.path.exists('/data/token'): - client = TgtgClient(email=os.environ.get('TGTG_EMAIL')) + if not os.path.exists(TOKEN_PATH): + client = TgtgClient(email=get_env('TGTG_EMAIL')) print('Waiting for credentials ...') credentials = get_credentials() - with open('/data/token', 'w') as file: + with open(TOKEN_PATH, 'w') as file: file.write(str(credentials)) print('Credentials stored in file') else: - with open('/data/token', 'r') as file: + with open(TOKEN_PATH, 'r') as file: credentials = json.loads(file.read().replace('\'', '"')) print('Credentials loaded from file') client = TgtgClient(**credentials) - if os.environ.get('TELEGRAM') is not None: + if get_env('TELEGRAM').lower() == 'true': telegram_bot = telegram.Bot(os.environ['TELEGRAM_TOKEN']) async def send_message(text): - if os.environ.get('TELEGRAM') is not None: + if get_env('TELEGRAM').lower() == 'true': async with telegram_bot: - await telegram_bot.send_message(chat_id=os.environ.get('TELEGRAM_ID'), text='\n'.join(['Too good to Go'] + text)) - if os.environ.get('MATRIX') is not None: + await telegram_bot.send_message(chat_id=get_env('TELEGRAM_ID'), text='\n'.join(['Too good to Go'] + text)) + if get_env('MATRIX').lower() == 'true': dic = { 'title': 'Too Good to Go', 'list': text, } response = requests.post( - url=os.environ.get('MATRIX_URL'), + url=get_env('MATRIX_URL'), headers={ 'Content-Type': 'application/json', }, - auth=(os.environ.get('MATRIX_BASIC_AUTH_USER'), os.environ.get('MATRIX_BASIC_AUTH_PASS')), + auth=(get_env('MATRIX_BASIC_AUTH_USER'), os.environ.get('MATRIX_BASIC_AUTH_PASS')), json=dic ) @@ -175,7 +186,8 @@ async def main(): print('-', end='', flush=True) last = next - time.sleep(interval + randomness * interval * (2 * random.random() - 1)) + real_interval = interval + randomness * interval * (2 * random.random() - 1) + time.sleep(interval) waiting_time = initial_waiting_time