Fix: create auth file if no exists

This commit is contained in:
localhost_frssoft 2022-11-09 13:11:36 +03:00
parent 9181923e52
commit 3ea5a1fcb9
1 changed files with 21 additions and 3 deletions

View File

@ -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,