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

View File

@ -1,9 +1,27 @@
from src.mpv_control import set_http_header from src.mpv_control import set_http_header
import requests, json, time import requests, json, time
import os
from loguru import logger from loguru import logger
with open('.auth.json', 'rt') as f: auth_file = '.auth.json'
if os.path.exists(auth_file):
with open('.auth.json', 'rt') as f:
auth = json.loads(f.read()) 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() s = requests.Session()
instance = 'fw.ponychord.rocks' instance = 'fw.ponychord.rocks'
@ -24,7 +42,7 @@ else:
def select_instance(new_instance=None): def select_instance(new_instance=None):
global instance global instance
instance = new_instance instance = new_instance
with open('.auth.json', 'rt') as f: with open(auth_file, 'rt') as f:
auth = json.loads(f.read()) auth = json.loads(f.read())
new_token = auth.get(instance) new_token = auth.get(instance)
s.headers.update({"Authorization": None, s.headers.update({"Authorization": None,