From 3ea5a1fcb906402e00d5271fcc8809dbb5d9e9ee Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Wed, 9 Nov 2022 13:11:36 +0300 Subject: [PATCH] Fix: create auth file if no exists --- src/fw_api.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/fw_api.py b/src/fw_api.py index 3dcae95..c631dac 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -1,9 +1,27 @@ from src.mpv_control import set_http_header import requests, json, time +import os from loguru import logger -with open('.auth.json', 'rt') as f: - auth = json.loads(f.read()) +auth_file = '.auth.json' +if os.path.exists(auth_file): + with open('.auth.json', 'rt') as f: + auth = json.loads(f.read()) +else: + # The default umask is 0o22 which turns off write permission of group and others + os.umask(0) + + descriptor = os.open( + path=auth_file, + flags=( + os.O_WRONLY # access mode: write only + | os.O_CREAT # create if not exists + | os.O_TRUNC # truncate the file to zero + ), + mode=0o600) + with open(descriptor, 'wt') as f: + f.write('{}') + auth = {} s = requests.Session() instance = 'fw.ponychord.rocks' @@ -24,7 +42,7 @@ else: def select_instance(new_instance=None): global instance instance = new_instance - with open('.auth.json', 'rt') as f: + with open(auth_file, 'rt') as f: auth = json.loads(f.read()) new_token = auth.get(instance) s.headers.update({"Authorization": None,