anime-downloader/anime_downloader/config.py

143 lines
4.1 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': '1080p',
'chunk_size': '10',
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}',
2020-03-18 04:42:05 -07:00
'provider': 'twist.moe',
2018-06-29 08:30:04 -07:00
'external_downloader': '',
2018-06-02 12:11:43 -07:00
},
'watch': {
'quality': '1080p',
'fallback_qualities': ['720p', '480p', '360p'],
2018-06-02 12:11:43 -07:00
'log_level': 'INFO',
2020-03-18 04:42:05 -07:00
'provider': 'twist.moe',
2019-05-22 10:04:27 -07:00
},
2019-07-12 04:55:21 -07:00
"siteconfig": {
"nineanime": {
"server": "mp4upload",
},
'anistream.xyz': {
"version": "subbed",
2019-07-12 09:40:16 -07:00
},
'animeflv': {
"version": "subbed",
"server": "natsuki",
2019-10-23 01:38:26 -07:00
},
'gogoanime': {
"server": "cdn",
},
'animerush':{
"server": "Mp4uploadHD Video",
"fallback_servers": ["MP4Upload", "Mp4upload Video", "Youruploads Video"]
},
'kickass': {
"server": "A-KICKASSANIME",
"fallback_servers": ["ORIGINAL-QUALITY-V2","HTML5-HQ","HTML5","A-KICKASSANIME","BETAPLAYER","KICKASSANIME","DEVSTREAM"],
"ext_fallback_servers": ["Mp4Upload","Vidcdn","Vidstreaming"],
},
'animesimple': {
"version": "subbed",
"server": "trollvid",
},
'dreamanime': {
"version": "subbed",
"server": "trollvid",
},
'ryuanime': {
"version": "subbed",
"server": "trollvid",
},
'animekisa': {
"server": "gcloud",
"fallback_servers": ["mp4upload","vidstream"]
},
'watchmovie': {
"server": "gcloud",
"fallback_servers": ["fembed","yourupload","mp4upload"],
},
'animeflix': {
"server": "AUEngine",
"fallback_servers": ["FastStream"],
"version": "sub",
},
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()
2019-07-12 09:40:16 -07:00
def update(gkey, to_be, from_dict):
if gkey not in to_be:
to_be[gkey] = {}
for key, val in from_dict[gkey].items():
if key not in to_be[gkey].keys():
to_be[gkey][key] = val
elif isinstance(from_dict[gkey][key], dict):
update(key, to_be[gkey], from_dict[gkey])
2019-05-22 10:04:27 -07:00
for key in DEFAULT_CONFIG.keys():
2019-07-12 09:40:16 -07:00
update(key, self._CONFIG, DEFAULT_CONFIG)
self.write()
# Expand environment variables in download_dir (#222)
download_dir = self._CONFIG['dl']['download_dir']
download_dir = os.path.expandvars(download_dir)
self._CONFIG['dl']['download_dir'] = download_dir
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()