Redesign config

master
Vishnunarayan K I 2018-06-02 23:48:05 +05:30
parent 7ab97c5bf4
commit 50bdcd2313
4 changed files with 60 additions and 47 deletions

View File

@ -1,6 +1,7 @@
import click
import subprocess
import sys
import os
import logging
@ -9,17 +10,14 @@ from anime_downloader.sites.exceptions import NotFoundError
from anime_downloader.players.mpv import mpv
from anime_downloader import config, util
from anime_downloader import util
from anime_downloader.config import Config
from anime_downloader import watch as _watch
echo = click.echo
CONTEXT_SETTINGS = dict(
default_map=config.DEFAULT_CONFIG
)
@click.group(context_settings=CONTEXT_SETTINGS)
@click.group(context_settings=Config.CONTEXT_SETTINGS)
def cli():
"""Anime Downloader
@ -33,30 +31,32 @@ def cli():
@click.argument('anime_url')
@click.option('--episodes', '-e', 'episode_range', metavar='<int>:<int>',
help="Range of anime you want to download in the form <start>:<end>")
@click.option('--save-playlist', '-p', 'playlist', type=bool, is_flag=True,
@click.option('--save-playlist', '-p', 'save_playlist', type=bool, is_flag=True,
help="If flag is set, saves the stream urls in an m3u file instead of downloading")
@click.option('--url', '-u', type=bool, is_flag=True,
help="If flag is set, prints the stream url instead of downloading")
@click.option('--play', 'player', metavar='PLAYER',
help="Streams in the specified player")
@click.option('--no-download', is_flag=True,
@click.option('--skip-download', is_flag=True,
help="Retrieve without downloading")
@click.option('--download-dir', help="Specifiy the directory to download to")
@click.option('--quality', '-q', type=click.Choice(['360p', '480p', '720p']),
help='Specify the quality of episode. Default-720p')
@click.option('--force', '-f', is_flag=True,
@click.option('--force-download', '-f', is_flag=True,
help='Force downloads even if file exists')
@click.option('--log-level', '-ll', 'log_level',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'ERROR']),
help='Sets the level of logger')
@click.pass_context
def dl(ctx, anime_url, episode_range, playlist, url, player, no_download, quality,
force, log_level, download_dir):
def dl(ctx, anime_url, episode_range, save_playlist, url, player, skip_download, quality,
force_download, log_level, download_dir):
""" Download the anime using the url or search for it.
"""
util.setup_logger(log_level)
config.write_default_config()
if url or player:
skip_download = True
cls = get_anime_class(anime_url)
@ -69,15 +69,14 @@ def dl(ctx, anime_url, episode_range, playlist, url, player, no_download, qualit
except NotFoundError as e:
echo(e.args[0])
return
logging.info('Found anime: {}'.format(anime.title))
if url or player:
no_download = True
if episode_range is None:
episode_range = '1:'+str(len(anime)+1)
if download_dir:
logging.info('Downloading to {}'.format(os.path.abspath(download_dir)))
logging.info('Found anime: {}'.format(anime.title))
try:
start, end = [int(x) for x in episode_range.split(':')]
anime._episodeIds = anime._episodeIds[start-1:end-1]
@ -94,8 +93,8 @@ def dl(ctx, anime_url, episode_range, playlist, url, player, no_download, qualit
p = subprocess.Popen([player, episode.stream_url])
p.wait()
if not no_download:
episode.download(force)
if not skip_download:
episode.download(force_download)
print()

View File

@ -1,39 +1,57 @@
import click
import os
import errno
import yaml
import json
APP_NAME = 'anime downloader'
APP_DIR = click.get_app_dir(APP_NAME)
DEFAULT_CONFIG = {
'dl': {
'anime_url': None,
'episode_range': None,
'playlist': False,
'save_playlist': False,
'url': False,
'player': None,
'no_download': False,
'skip_download': False,
'download_dir': '.',
'quality': '720p',
'force': False,
'force_download': False,
'log_level': 'INFO'
}
}
APP_NAME = 'anime downloader'
APP_DIR = click.get_app_dir(APP_NAME)
CONFIG_FILE = os.path.join(APP_DIR, 'config.yml')
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()
@property
def CONTEXT_SETTINGS(self):
return dict(
default_map=self._CONFIG
)
def _write_config(self, config_dict):
with open(self.CONFIG_FILE, 'w') as configfile:
json.dump(config_dict, configfile, indent=4, sort_keys=True)
def _read_config(self):
with open(self.CONFIG_FILE, 'r') as configfile:
conf = json.load(configfile)
return conf
def _write_default_config(self):
self._write_config(DEFAULT_CONFIG)
def write_config(config_dict):
with open(CONFIG_FILE, 'w') as configfile:
yaml.dump(config_dict, configfile, default_flow_style=False)
def write_default_config():
try:
os.makedirs(APP_DIR)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not os.path.exists(CONFIG_FILE):
write_config(DEFAULT_CONFIG)
Config = _Config()

View File

@ -25,9 +25,6 @@ class BaseAnime:
self.url = url
self.path = path
if path:
logging.info('Downloading to {}'.format(os.path.abspath(path)))
if quality in self.QUALITIES:
self.quality = quality
else:

View File

@ -21,7 +21,6 @@ setup(
'requests>=2.18.4',
'Click>=6.7',
'fuzzywuzzy[speedup]>=0.16.0',
'PyYAML>=3.12'
],
extras_require={
'cloudflare': ['cfscrape>=1.9.5']