2022-12-18 01:02:53 +00:00
|
|
|
import json
|
2023-07-08 09:14:16 +00:00
|
|
|
import os
|
2022-11-09 23:23:18 +00:00
|
|
|
from os.path import exists
|
2022-11-09 18:51:20 +00:00
|
|
|
from loguru import logger
|
2022-11-10 15:55:36 +00:00
|
|
|
from pyfzf.pyfzf import FzfPrompt
|
|
|
|
|
|
|
|
fzf = FzfPrompt()
|
2022-11-09 18:51:20 +00:00
|
|
|
|
2022-11-09 23:23:18 +00:00
|
|
|
conf_file = 'config.json'
|
|
|
|
|
|
|
|
default_conf = {
|
2022-12-18 01:02:53 +00:00
|
|
|
'instance': 'fw.ponychord.rocks',
|
2023-06-26 11:26:20 +00:00
|
|
|
'public_list_instances_extended':
|
|
|
|
{
|
|
|
|
"open.audio": None,
|
|
|
|
"audio.liberta.vip": None,
|
2023-06-28 21:03:34 +00:00
|
|
|
"dance.with.tewi.and.reisen": None,
|
2023-06-26 11:26:20 +00:00
|
|
|
"tanukitunes.com": None,
|
|
|
|
"funkwhale.juniorjpdj.pl": None,
|
|
|
|
"audio.securetown.in.ua": None,
|
|
|
|
"funkwhale.thurk.org": None,
|
|
|
|
"buzzworkers.com": None,
|
|
|
|
"soundship.de": None,
|
|
|
|
"funkwhale.kameha.click": None,
|
|
|
|
"music.chosto.me": None,
|
|
|
|
"zik.goe.land": None,
|
|
|
|
"music.humanoids.be": None,
|
|
|
|
"music.hempton.us": None,
|
|
|
|
"mizik.o-k-i.net": None,
|
|
|
|
"klh.radiolivre.org": None,
|
|
|
|
"hudba.feildel.fr": None,
|
|
|
|
"funk.deko.cloud": None,
|
|
|
|
"audio.graz.social": None,
|
|
|
|
"funkwhale.desmu.fr": None,
|
|
|
|
"listen.knsm.cc": None,
|
|
|
|
"funkwhale.gegeweb.eu": None,
|
|
|
|
},
|
|
|
|
'automatic_fetch_new_instances': False,
|
2022-12-18 01:02:53 +00:00
|
|
|
'enable_server_transcoding': False,
|
2023-01-02 01:19:09 +00:00
|
|
|
'external_transcoder_http_proxy_path': "",
|
2023-07-30 13:33:05 +00:00
|
|
|
'share_to_fediverse_token': "",
|
|
|
|
'share_to_fediverse_instance': "",
|
2022-12-18 01:02:53 +00:00
|
|
|
'prefetch_playlist': True,
|
2023-03-02 17:58:22 +00:00
|
|
|
'enable_persistent_cache': False,
|
2022-12-18 01:02:53 +00:00
|
|
|
'mpv_volume': 100,
|
|
|
|
'show_like_button': True,
|
2023-08-03 15:31:07 +00:00
|
|
|
'show_artist_name_in_albums': False,
|
|
|
|
'termux_handle_track_switch_by_volume': False
|
2022-11-09 18:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-09 23:23:18 +00:00
|
|
|
def set_defaults(corrected_config=None):
|
|
|
|
conf_rewrite = default_conf
|
|
|
|
if corrected_config:
|
|
|
|
conf_rewrite = corrected_config
|
2023-07-08 09:14:16 +00:00
|
|
|
descriptor = os.open(
|
|
|
|
path=conf_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:
|
2022-11-09 23:23:18 +00:00
|
|
|
f.write(json.dumps(conf_rewrite, indent=4))
|
|
|
|
|
|
|
|
|
|
|
|
def check_config():
|
2022-11-10 00:23:46 +00:00
|
|
|
'''Check config and remove or add keys if not found in default config'''
|
2022-11-09 23:23:18 +00:00
|
|
|
with open(conf_file, 'rt') as f:
|
|
|
|
loaded_config = json.loads(f.read())
|
|
|
|
correct_conf = {}
|
|
|
|
for k, v in loaded_config.items():
|
|
|
|
if k in default_conf.keys():
|
|
|
|
correct_conf[k] = v
|
|
|
|
else:
|
|
|
|
logger.warning(f'{k} from config will be removed. Value: {v}')
|
2022-11-10 00:23:46 +00:00
|
|
|
for k, v in default_conf.items():
|
|
|
|
if k not in correct_conf.keys():
|
|
|
|
correct_conf[k] = v
|
|
|
|
logger.warning(f'{k} added in config. Value: {v}')
|
2022-11-09 23:23:18 +00:00
|
|
|
set_defaults(correct_conf)
|
2022-11-10 15:55:36 +00:00
|
|
|
return correct_conf
|
2022-12-18 01:02:53 +00:00
|
|
|
|
2022-11-09 23:23:18 +00:00
|
|
|
|
|
|
|
if not exists(conf_file):
|
|
|
|
set_defaults()
|
|
|
|
else:
|
|
|
|
check_config()
|
|
|
|
|
|
|
|
|
2022-11-10 00:23:46 +00:00
|
|
|
def get_config(key):
|
|
|
|
'''return value from config by key'''
|
|
|
|
with open(conf_file, 'rt') as f:
|
|
|
|
loaded_config = json.loads(f.read())
|
|
|
|
return loaded_config.get(key)
|
|
|
|
|
|
|
|
|
2023-06-05 21:51:54 +00:00
|
|
|
def set_config(key, value):
|
|
|
|
'''set value new value config by key'''
|
|
|
|
read_conf = check_config()
|
|
|
|
with open(conf_file, 'wt') as f:
|
|
|
|
read_conf[key] = value
|
|
|
|
f.write(json.dumps(read_conf, indent=4))
|