anime-downloader/anime_downloader/config.py

88 lines
2.3 KiB
Python
Raw Normal View History

2018-05-31 04:04:47 -07:00
import click
import os
import errno
2018-06-02 11:18:05 -07:00
import json
from anime_downloader import util
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
APP_NAME = 'anime downloader'
APP_DIR = click.get_app_dir(APP_NAME)
2018-05-31 04:04:47 -07:00
DEFAULT_CONFIG = {
'dl': {
'url': False,
'player': None,
2018-06-02 11:18:05 -07:00
'skip_download': False,
2018-05-31 04:04:47 -07:00
'download_dir': '.',
'quality': '720p',
2018-08-06 08:46:58 -07:00
'fallback_qualities': ['720p', '480p', '360p'],
2018-06-02 11:18:05 -07:00
'force_download': False,
'file_format': '{anime_title}/{anime_title}_{ep_no}',
'provider': '9anime',
2018-06-29 08:30:04 -07:00
'external_downloader': '',
2018-06-02 12:11:43 -07:00
},
'watch': {
'quality': '720p',
'log_level': 'INFO',
'provider': '9anime',
2019-05-22 10:04:27 -07:00
},
"nineanime": {
"server": "mp4upload",
2018-05-31 04:04:47 -07:00
}
}
2018-06-02 11:18:05 -07:00
class _Config:
CONFIG_FILE = os.path.join(APP_DIR, 'config.json')
def __init__(self):
try:
os.makedirs(APP_DIR)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not os.path.exists(self.CONFIG_FILE):
self._write_default_config()
self._CONFIG = DEFAULT_CONFIG
else:
self._CONFIG = self._read_config()
def update(gkey):
2019-05-22 10:04:27 -07:00
if gkey not in self._CONFIG:
self._CONFIG[gkey] = {}
for key, val in DEFAULT_CONFIG[gkey].items():
if key not in self._CONFIG[gkey].keys():
self._CONFIG[gkey][key] = val
2019-05-22 10:04:27 -07:00
for key in DEFAULT_CONFIG.keys():
update(key)
self.write()
2018-06-02 11:18:05 -07:00
@property
def CONTEXT_SETTINGS(self):
return dict(
default_map=self._CONFIG
)
2018-06-09 08:24:42 -07:00
def __getitem__(self, attr):
return self._CONFIG[attr]
def write(self):
self._write_config(self._CONFIG)
2018-06-02 11:18:05 -07:00
def _write_config(self, config_dict):
with open(self.CONFIG_FILE, 'w') as configfile:
json.dump(config_dict, configfile, indent=4, sort_keys=True)
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
def _read_config(self):
with open(self.CONFIG_FILE, 'r') as configfile:
conf = json.load(configfile)
return conf
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
def _write_default_config(self):
if util.check_in_path('aria2c'):
DEFAULT_CONFIG['dl']['external_downloader'] = '{aria2}'
2018-06-02 11:18:05 -07:00
self._write_config(DEFAULT_CONFIG)
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
Config = _Config()