Add download to selected anime options

master
Vishnunarayan K I 2018-06-09 20:54:42 +05:30
parent c200378c41
commit 52be7b2fef
4 changed files with 54 additions and 28 deletions

View File

@ -1,5 +1,4 @@
import click
import subprocess
import sys
import os
@ -55,9 +54,13 @@ def cli():
'--log-level', '-ll', 'log_level',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'ERROR']),
help='Sets the level of logger')
@click.option(
'--name-fmt', '-nf',
help='Format for how the files to be downloaded be named.'
)
@click.pass_context
def dl(ctx, anime_url, episode_range, url, player, skip_download, quality,
force_download, log_level, download_dir):
force_download, log_level, download_dir, name_fmt):
""" Download the anime using the url or search for it.
"""
@ -81,9 +84,23 @@ def dl(ctx, anime_url, episode_range, url, player, skip_download, quality,
logging.info('Found anime: {}'.format(anime.title))
anime = util.split_anime(anime, episode_range)
util.process_anime(anime, player=player, force_download=force_download,
download_dir=download_dir, url=url,
skip_download=skip_download)
if url or player:
skip_download = True
if download_dir and not skip_download:
logging.info('Downloading to {}'.format(os.path.abspath(download_dir)))
for episode in anime:
if url:
util.print_episodeurl(episode)
if player:
util.play_episode(episode)
if not skip_download:
util.download_episode(episode)
@cli.command()
@ -108,11 +125,12 @@ def watch(anime_name, new, _list, quality, log_level, remove):
"""
With watch you can keep track of any anime you watch.
Available Commands after selection of an anime:
set : set episodes_done and title. Ex: set episodes_done=3
remove : remove selected anime from watch list
update : Update the episodes of the currrent anime
watch : Watch selected anime
Available Commands after selection of an anime:\n
set : set episodes_done and title. Ex: set episodes_done=3\n
remove : remove selected anime from watch list\n
update : Update the episodes of the currrent anime\n
watch : Watch selected anime\n
download : Download episodes of selected anime
"""
util.setup_logger(log_level)
util.print_info(__version__)
@ -188,6 +206,8 @@ def list_animes(watcher, quality):
inp = click.prompt('Press q to exit', default='q').strip()
# TODO: A better way to handle commands. Use regex. Refractor to class?
# Decorator?
if inp == 'q':
break
elif inp == 'remove':
@ -199,6 +219,17 @@ def list_animes(watcher, quality):
anime.quality = quality
watch_anime(watcher, anime)
sys.exit(0)
elif inp.startswith('download'):
try:
inp = inp.split('download ')[1]
except IndexError:
inp = ':'
inp = str(anime.episodes_done+1)+inp if inp.startswith(':') else inp
inp = inp+str(len(anime)) if inp.endswith(':') else inp
anime = util.split_anime(anime, inp)
for episode in anime:
util.download_episode(episode, force_download=False,
download_dir=Config['dl']['download_dir'])
elif inp.startswith('set '):
inp = inp.split('set ')[-1]
key, val = [v.strip() for v in inp.split('=')]

View File

@ -44,6 +44,9 @@ class _Config:
default_map=self._CONFIG
)
def __getitem__(self, attr):
return self._CONFIG[attr]
def _write_config(self, config_dict):
with open(self.CONFIG_FILE, 'w') as configfile:
json.dump(config_dict, configfile, indent=4, sort_keys=True)

View File

@ -96,6 +96,7 @@ class BaseEpisode:
if quality not in self.QUALITIES:
raise AnimeDLError('Incorrect quality: "{}"'.format(quality))
self.ep_no = ep_no
self.episode_id = episode_id
self.quality = quality
logging.debug("Extracting stream info of id: {}".format(self.episode_id))
@ -134,7 +135,7 @@ class BaseEpisode:
r = requests.get(self.stream_url, stream=True)
total_size = int(r.headers['Content-length'])
downloaded, chunksize = 0, 16384
downloaded, chunksize = 0, 2048
start_time = time.time()
if os.path.exists(path):

View File

@ -5,7 +5,6 @@ import click
from anime_downloader.sites.nineanime import NineAnime
import subprocess
import platform
import os
def setup_logger(log_level):
@ -76,26 +75,18 @@ def split_anime(anime, episode_range):
return anime
def process_anime(anime, *, url, player, force_download, download_dir,
skip_download):
if url or player:
skip_download = True
def print_epiosdeurl(episode):
print(episode.stream_url)
if download_dir and not skip_download:
logging.info('Downloading to {}'.format(os.path.abspath(download_dir)))
for episode in anime:
if url:
print(episode.stream_url)
continue
def download_episode(episode, *, force_download, download_dir):
episode.download(force=force_download, path=download_dir)
print()
if player:
p = subprocess.Popen([player, episode.stream_url])
p.wait()
if not skip_download:
episode.download(force=force_download, path=download_dir)
print()
def play_epiosde(episode, *, player):
p = subprocess.Popen([player, episode.stream_url])
p.wait()
def print_info(version):